day04--面向对象--多态

package day04;

/*
 * 对象的多态性:多种形态,父类类型的引用指向子类对象
 * 
 * 前提:存在继承或实现关系
 * 
 * class 动物{
 *      public void eat(){}
 * }
 * 
 * class 猫 extends 动物 {
 * }
 * 
 * 常态:猫看成是猫    猫 c=new 猫();
 * 多态:猫是动物        动物 d=new 猫(); //第一种方式体现多态
 * 
 * 方法名(动物 d){//第二种方式体现多态
 *      d.eat();
 * }
 * 
 * 动物  方法名(int type){//第三种方式体现多态
 *      if(type==1){
 *          return new 猫();
 *      }
 *      return new 狗();
 * }
 * 
 * 多态的弊端:只能使用父类中定义的方法,并且子类必须重写父类中的方法
 *
 * 多态的好处:提高程序的扩展性,前期定义的功能(方法)可以被后期事物实现(使用)
 */

abstract class Animal3 {
    public abstract void eat();// 虚方法

}

class Cat3 extends Animal3 {
    public void eat() {
        System.out.println("猫吃鱼");

    }

    public void catchMouse() {
        System.out.println("猫抓老鼠");
    }
}

class Dog3 extends Animal3 {
    public void eat() {
        System.out.println("狗吃骨头");
    }

    public void kanjia() {
        System.out.println("看家");
    }
}

public class Demo7 {

    public static void main(String[] args) {

        Cat3 cat = new Cat3();
        cat.eat();
        cat.catchMouse();

        Animal3 a1 = new Cat3();
        // a1.eat();
        // a1.catchMouse();//出错,因为父类中没有声明此方法

        Animal3 a2 = new Dog3();// 多态
        // a2.eat();

        eat(a1);
        eat(a2);
    }

    public static void eat(Animal3 animal) {// 第二种方式体现多态性
        animal.eat();// 调用是Animal实际对象的方法,实际对象可能是cat、Dog的类对象

        // 通过instanceof关键字判断对象是哪一种类型的对象
        if (animal instanceof Cat3)
            System.out.println("小猫正在吃...");
        else
            System.out.println("小狗正在吃...");
    }

}

转型

package day04;

/*
 * 转型:
 *    向上转型:子类对象向父类类型转换
 *    向下转型:当子类中扩展的方法被调用时,需要将对象转成子类类型对象
 * 
 * 注意:为了减少错误,在强转之前,可以通过instanceof判断对象是否为某一种类型
 * 
 */

abstract class Animal4 {
    public static final int TYPE_CAT = 1;
    public static final int TYPE_DOG = 2;

    public abstract void eat();// 虚方法

}

class Cat4 extends Animal4 {
    public void eat() {
        System.out.println("猫吃鱼");

    }

    public void catchMouse() {
        System.out.println("猫抓老鼠");
    }
}

class Dog4 extends Animal4 {
    public void eat() {
        System.out.println("狗吃骨头");
    }

    public void kanjia() {
        System.out.println("看家");
    }
}

public class Demo8 {

    public static void main(String[] args) {

        Animal4 a1 = new Cat4();// 向上转型,子类对象转成父类对象
        eat(a1);

        Animal4 a2 = newAnimal4(Animal4.TYPE_DOG);
        eat(a2);

    }

    public static void eat(Animal4 animal) {// 第二种方式体现多态性
        animal.eat();// 调用是Animal实际对象的方法,实际对象可能是cat、Dog的类对象

        // 通过instanceof关键字判断对象是哪一种类型的对象
        if (animal instanceof Cat4) {
            System.out.println("小猫正在吃...");

            // 需要调用Cat中扩展的方法
            Cat4 c = (Cat4) animal;// 向下转型,父类对象向子类对象转型
            c.catchMouse();
        } else {
            System.out.println("小狗正在吃...");
            Dog4 d = (Dog4) animal;
            d.kanjia();
        }
    }

    // 第三种体现多态性,根据类型创建某一动物的对象
    public static Animal4 newAnimal4(int type) {
        if (type == Animal4.TYPE_CAT) {
            return new Cat4();
        } else if (type == Animal4.TYPE_DOG) {
            return new Dog4();
        }
        return null;
    }

}

接口

package day04;

/*
 *接口与多态
 * 接口是一种引用数据类型,定义接口的引用指向到接口实现类对象,则是接口体现多态的方式
 */
interface Drinking {
    public void drink();

    public void throwup();

}

class Student implements Drinking {

    public void drink() {
        System.out.println("意思意思--喝酒");
    }

    public void throwup() {
        System.out.println("往死里喝酒");
    }

}

class Teacher implements Drinking {
    public void drink() {
        System.out.println("慢慢地喝水");
    }

    public void throwup() {
        System.out.println("往死里喝水");
    }
}

public class Demo10 {

    public static void main(String[] args) {

        Drinking d1 = new Student();// 接口的多态

        d1.drink();
        d1.throwup();

        Drinking d2=new Teacher();
        d2.drink();
        d2.throwup();

    }

}
package day04;

interface OnClickListener {
    public void onClick();
}

class View {
    private String text;
    private OnClickListener listener;

    public void setOnClickListener(OnClickListener listener) {
        this.listener = listener;
    }

    public void run()// 模拟view被点击事件
    {
        if (listener != null) {
            listener.onClick();

        }
    }
}

class MyClickListener implements OnClickListener {
    public void onClick() {
        System.out.println("当前的View被点击了...");
    }
}

public class Demo11 {

    public static void main(String[] args) {
        View view = new View();
        view.setOnClickListener(new MyClickListener());// 体现了接口的多态性

        view.run();
        view.run();
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值