继承
子类继承父类,就会拥有父类的全部方法!!!
关系:
//public
//protected
//default
//private
在Java中,所有的类,都默认直接或者间接继承Object
ctrl+H 显示树:
代码展示
package com.oop.demo05;
//Person 人:父类
public class Person {
//public
//protected
//default
//private
private int money=10_0000_0000;
public void say(){
System.out.println("说了一句话");
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
}
super
super和this对比着学
私有的东西无法被继承!
构造器生成无参选择:
隐藏代码:调用了父类的无参构造
调用了父类的无参构造,必须要在子类的第一行
super();
package com.oop.demo05;
//学生 is 人:派生类,子类
//子类继承父类,就会拥有父类的全部方法!
public class Student extends Person{
public Student() {
//隐藏代码:调用了父类的无参构造
//调用了父类的无参构造,必须要在子类的第一行
//super();
System.out.println("Student无参构造");
}
private String name="美女";
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);
System.out.println(super.name);
}
}
Super注意点:
1.super调用父类的构造器,必须在构造器的第一行
2.super必须只能出现在子类的方法或者构造器中!
3.super和this不能同时调用 (在子类里面调用构造器,只能调用一个,要么this,要么super)
Vs this:
代表的对象不同:
this:本身调用者这个对象
super:代表父类对象的应用
前提:
this:没有继承也可以使用
super:只能在继承条件下才可以使用
构造方法:
this():调用本类的构造
super():调用父类的构造
测试代码:
package com.oop.demo05;
public class Application {
public static void main(String[] args) {
Student student = new Student();
//student.test("习大大");
//student.test1();
}
}