super 、 this

super.name,子类通过super访问父类的protected的属性。

私有的东西无法被继承。

this.name,访问自己的属性。

supe();调用父类的无参构造。

public Student()
{
    //隐藏代码super();一定要在第一行。
    super();
    System.out.println("i am here.");
}

super();调用父类的构造。

this();调用本类的构造。

调用父类构造器必须在第一行,this也一样。

方法重写override

多态

父类的引用指向子类。例如

Student s1=new Student();
Person s2=new Student();
Object s3=new Student();
package com.cat.oop;

import com.cat.oop.demo06.Person;
import com.cat.oop.demo06.Student;

public class Application {
    public static void main(String[] args) {
        Person p1 = new Person();
        Student s1 = new Student();
        p1.say();
        s1.say();
        Person p2 = new Student();//父类型,可以指向子类,但是不能调用子类独有的方法!
        System.out.println("-----------------");
        p2.say();
    }
}

对象能执行的方法主要看左边!!

父类型,可以指向子类,但是不能调用子类独有的方法!

多态存在条件:继承、方法重写、父类引用指向子类对象。

posted on 2021-04-11 22:12  猫客侠  阅读(63)  评论(0)    收藏  举报