admin 管理员组

文章数量: 1184232


2024年1月17日发(作者:windows2008激活密钥)

简述super关键字的用法

uper可以理解为是指向自己超(父)类对象的一个指针,而这个超类指的是离自己最近的一个父类。

super也有三种用法:

1.普通的直接引用

与this类似,super相当于是指向当前对象的父类的引用,这样就可以用来引用父类的成员。

2.子类中的成员变量或方法与父类中的成员变量或方法同名时,用super进行区分

class Person{

protected String name;

public Person(String name) {

= name;

}

}

class Student extends Person{

private String name;

public Student(String name, String name1) {

super(name);

= name1;

}

public void getInfo(){

n(); //Child

n(); //Father

}

}

public class Test {

public static void main(String[] args) {

Student s1 = new Student("Father","Child");

o();

}

}

3.引用父类构造函数

3、引用父类构造函数

super(参数):调用父类中的某一个构造函数(应该为构造函数中的第一条语句)。

this(参数):调用本类中另一种形式的构造函数(应该为构造函数中的第一条语句)。


本文标签: 父类 成员 变量