Java09—super,多态

1. super

 

 2. 方法重写

 

 

public class Hello{
    public static void main(String[] args){
        Person person = new Person("zhushuqi", 25);
        System.out.println(person.say());
        Student zsq = new Student("zsq", 18, "123123", 100);
        System.out.println(zsq.say());

    }
}

class Person{
    private String name;
    private int age;

    public Person() {
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String say(){
        return "大家好,我叫" + name + "。我的年龄是" + age;
    }
}

class Student extends Person{
    private String id;
    private double score;

    public Student(){
    }

    public Student(String name, int age, String id, double score) {
        super(name, age);
        this.id = id;
        this.score = score;
    }

    @Override
    public String say() {
        return super.say() + "。我的id是" + id + "。我的分数是" + score;
    }
}

2. 多态

 主人喂食的问题得以解决!!!因为这两个形参可以接收他们分别的所有的子类,也就是如果没有多态机制,那么要写a*b个方法,有了多态,就只要1个方法。

3. 多态细节

细节1

 

 这里容易搞错,这边的eat,计算机会先找Cat类下面的方法,再去找父类的。。。

因为运行的时候已经编译完了,所以遵从右边的运行类型

 细节2

 第三句话

意思就是,本来animal指向cat,那强制类型转换,只能把它转为Cat类型,但是不能转为Dog

 细节3 (这个只能背规则)

 课堂练习

 

动态绑定机制,重要!!!!!!!

 

 

public class Hello{
    public static void main(String[] args){
        Person[] persons = new Person[3];
        persons[0] = new Person("zsq", 25);
        persons[1] = new Student("zhushuqi", 26, 100);
        persons[2] = new Teacher("zzq", 19, 9999);

        for(int i = 0; i < persons.length; i++){
            if(persons[i] instanceof Student){
                System.out.println(((Student)persons[i]).study());
            }else if(persons[i] instanceof Teacher){
                System.out.println(((Teacher)persons[i]).teach());
            }
            System.out.println(persons[i].say());
        }
        //所以怎么单独调用teach或者study呢?
        //如果直接用persons[1].study()是识别不出的,
        //因为编译器他会用编译类型去看,所以找不到这个方法
    }
}

class Person{
    private String name;
    private  int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String say(){
        return "名字:" + name + " 年龄" + age;
    }

    public String getName() {
        return name;
    }
}

class Student extends Person{
    private double score;

    public Student(String name, int age, double score){
        super(name, age);
        this.score = score;
    }

    public String say(){
        return super.say() + " 分数:" + score;
    }

    public String study(){
        return super.getName() + "正在学习";
    }

}

class Teacher extends Person{
    private double salary;
    public Teacher(String name, int age, double salary){
        super(name, age);
        this.salary = salary;
    }

    public String teach(){
        return super.getName() + "老师在教书";
    }
}

 

public class Hello{
    public static void showEmpAnnual(Employee e){
        System.out.println(e.getAnnual());
    }

    public static void testWork(Employee e){
        if (e instanceof PutongEmployee){
           System.out.println(((PutongEmployee)e).work());
        }else if(e instanceof  Jingli){
            System.out.println(((Jingli)e).manage());
        }
    }

    public static void main(String[] args){
        Employee e1 = new PutongEmployee("zhushuqi", 1000);
        Employee e2 = new Jingli("zsq", 20000, 30000);
        showEmpAnnual(e1);
        showEmpAnnual(e2);

        testWork(e1);
        testWork(e2);



    }


}

class Employee{
    private String name;
    private double salary;

    public Employee(String name, double salary) {
        this.name = name;
        this.salary = salary;
    }

    public double getAnnual(){
        return 12*salary;
    }

    public String getName() {
        return name;
    }
}

class PutongEmployee extends Employee{
    public PutongEmployee(String name, double salary) {
        super(name, salary);
    }
    public String work(){
        return super.getName()+"正在工作";
    }

    @Override
    public double getAnnual() {
        return super.getAnnual();
    }
}

class Jingli extends Employee{
    private double bonus;

    public Jingli(String name, double salary, double bonus) {
        super(name, salary);
        this.bonus = bonus;
    }

    public String manage(){
        return super.getName() + "经理正在管理";
    }

    @Override
    public double getAnnual() {
        return super.getAnnual() + bonus;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值