(一)继承
1.java中的继承是单继承。
2.子类继承了父类除构造方法外的其他所有。
3.子类在构造的时候,要先帮助父类进行构造。
class Person {
protected String name;
protected int age;
protected String id;
public Person(String name, int age, String id) {
this.name = name;
this.age = age;
this.id = id;
}
}
class Student extends Person{
protected int score;
public Student(String name, int age, String id, int score) {
super(name, age, id);
this.score = score;
}
}
4.访问修饰限定符
private < default < protected < public
5.super的使用
1)super()//调用父类的构造方法
2)super.data//访问父类的属性
3)super.func()//访问父类的成员方法
(二)final
1.final int a = 10;//常量,只能被初始化一次,不能再修改
2.final修饰类:密封类,不能被继承
3.final修饰方法:密封方法
(三)多态
1.什么是多态:思想
父类引用 引用子类对象
父类和子类有同名的覆盖方法
通过父类引用调用这个重写的方法的时候
2.向上转型
Animal animal = new Cat();
animal.count //错误,只能访问父类的方法或属性
(四)发生向上转型时机
a.直接赋值
public static void main(String[] args) {
Person person = new Student();
person.play();
}
b.传参
public static void func(Person person) {
person.play();
}
public static void main(String[] args) {
Student student = new Student();
func(student);
}
c.返回值
public static Person func() {
return new Student();
}
public static void main(String[] args) {
func().eat();
}
(五)重写
a.重写的特性
1.方法名相同
2.参数列表相同
3.返回值相同
4.产生于不能的类(继承关系)
b.关于重写要注意的事项
1.需要重写的方法不能被final修饰。被final修饰之后,它是密封方法,不可以修改。
2.被重写的方法,访问修饰限定符一定不能是私有的。
3.被重写的方法,子类当中的访问修饰限定要大于等于父类的访问修饰限定。
4.被static修饰的方法是不可以被重写的。
(六)运行时绑定发生的时间
父类引用引用子类对象,同时通过父类引用调用同名的覆盖方法。