Super详解
Super注意点:
1.Super调用父类的构造方法,必须在构造方法的第一个
2.Super必须只能出现在子类的方法或者构造方法中
3.Super和this不能同时调用构造方法
this:
代表的对象不同:
this:本身调用者这个对象
Super:代表父类对象的应用
前提:
this:没有继承也可以使用
Super:只有在继承条件下才可以使用
构造方法:
this():本类的构造
Super():父类的构造
package project;
/*四个修饰符
- public
- protected
- default 不写修饰符就是默认default
- private
/
public class Person {
public Person() {
System.out.println("Person无参执行了");
}
protected String name="lisi";
/private void print() {私有的东西无法被继承
System.out.println("Person");
}/
public void print() {
System.out.println("Person");
}
}
/public int money=10_0000_0000;
private int cash=20_0000_0000;
public void say(){
System.out.println("说话了");
}
public int getCash() {
return cash;
}
public void setCash(int cash) {
this.cash=cash;
}
}
/
===================================================================================================================================================
package project;
//学生 is 人:派生类,子类
public class Student extends Person{
public Student() {
//隐藏代码:调用了父类的无参构造
super();//调用父类的构造器,必须要在子类构造器的第一行
System.out.println("Student无参执行了");
}
private String name="linsi";
public void print() {
System.out.println("Student");
}
public void test1() {
print();//student
this.print();//student
super.print();//person
}
public void test(String name) {
/System.out.println(name);//李四
System.out.println(this.name);//linsi
System.out.println(super.name);//lisi
*/
}
}
==================================================================================================================================================
package project;
public class Application {
public static void main(String[] args) {
Student student=new Student();
//student.test("李四");
//student.test1();
}
}
//子类继承了父类,就会拥有父类的全部方法
/*public static void main(String[] args) {
// TODO Auto-generated method stub
Student student=new Student();
student.say();
System.out.println(student.money);
System.out.println(student.getCash());
}
}
*/
输出结果:
Person无参执行了
Student无参执行了

浙公网安备 33010602011771号