栏目分类:
子分类:
返回
文库吧用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
文库吧 > IT > 软件开发 > 后端开发 > Java

【java特性

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【java特性

super 修饰属性 方法

在子类中,可以用super.显式的去调用父类中的属性和方法,一般情况下,super.可以省略不写。
特殊情况下,子类中访问属性和方法,子类与父类属性、方法重名时,访问父类必须加super.,访问子类this.(可省略)

super修饰构造器 空构造器

空构造器含有super(),作用调用父类的空构造器,一般省略不写。
一般构造器(有参)如果没有调用显式的调用父类构造器,有默认的super(),可写(显化)可不写

带参构造器
  1. 利用super调用父类构造器,已经调用显式的构造器,不需要调用默认的super();

  1. 在构造器中,super调用父类构造器和this调用子类构造器,只能选择一个,两者不能共存


理由:

相关代码

public class Student extends Person{
    int score;
    public Student(){
        
    }
    public Student(int score){
//        super(); //可写可不写,默认有(显化问题)
        this.score = score;
    }
    public Student(int age , String name , int score){
//        super(){}
       
        super(age,name);//利用super调用父类构造器,已经调用显式的构造器,不需要调用默认的super()
        
        
        this.score = score;


    }
}
public class Person {
    int age;
    String name;
    public Person(){

    }
    public Person(int age ,String name){
        this.age = age;
        this.name = name;
    }
}
转载请注明:文章转载自 www.wk8.com.cn
本文地址:https://www.wk8.com.cn/it/1040180.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 wk8.com.cn

ICP备案号:晋ICP备2021003244-6号