java多态

多态性:多种行为

学习于职坐标,以下代码全部边看视频边敲,应该不算抄袭别人知识产权

package ploy;

public class AnimalDemo {

    public static void main(String[] args) {
        //父类的引用变量可以引用其子类的对象
        Animal animal1=new Dog("wangcai");//向上转型//上是指子类转成父类,可能会丢失方法
        animal1.eat();
        //animal1.hh()//这就是丢失方法,,无法调用hh()
        //向上转型首先是安全的,当可能会导致子类方法的丢失
        //父类的引用变量,只能调用父类中有的方法或子类重写父类的方法
        Animal animal2=new Cat("zhaocaimiao");
        animal2.eat();
        /*多态存在的三个必要条件
         * 1.需要存在继承和实现关系
         * 2.同样的方法调用二执行不同操作,运行不同代码(重写操作)
         * 3.在运行时父类或者接口的引用变量可以引用其子类的对象
         * */



        //向下转型是不安全的
        //1./*Cat cat = (Cat)animal1;*/// java.lang.ClassCastException//类型转换错误
        /*instanceof
         * result = object instanceof class
         * 典型使用场合
         * 在对对象做向下转型之前,没有其他有关对象类型信息时,务必使用instanceof 来判断一下,避免抛出ClassCastException
         * 
         * 
        */
        //正确的做法
        if(animal1 instanceof Cat){//并没有执行
            System.out.println("hello1");
            Cat cat=(Cat)animal1;
        }

        if(animal2 instanceof Cat){
            System.out.println("hello2");
            Cat cat=(Cat)animal2;
            cat.hh();//此时可以执行子类特有的方法
        }

        //静态绑定:final,static,private和构造方法,在执行前已经绑定,这些方法是不可以修改的
        //动态绑定:Person p=new Teacher();p.sayHi();

    }

}

class Animal{
    private String name;

    public Animal(String name){
        this.name = name;
    }

    public void eat(){//这是一个通用的方法,通用方法实现没有太大的意义
    }

}

class Dog extends Animal{
    public Dog(String name){
        super(name);
    }
    public void hh(){
        System.out.println("nihai");
    }
    //对父类的方法的重写
    @Override
    public void eat(){
        System.out.println("啃骨头");
    }
}

class Cat extends Animal{
    public Cat(String name){
        super(name);
    }

    public void hh(){
        System.out.println("nihai");
    }
    //对父类的方法的重写
    @Override
    public void eat(){
        System.out.println("吃鱼肉");
    }
}

对上面的解释

1./*多态存在的三个必要条件      * 1.需要存在继承和实现关系
         * 2.同样的方法调用二执行不同操作,运行不同代码(重写操作)
         * 3.在运行时父类或者接口的引用变量可以引用其子类的对象
         * */
2./*分为向上向下转型
    (1)向上转型:Animal animal1=new Dog("wangcai");这样会丢失变量和方法,只能使用父类的已经存在的变量和被子类重写的方法.
    (2)向下转型:解决向上转型的确定,但是不安全的
    /*Cat cat = (Cat)animal1;*/比如这句话,明明animal是dog型的,这样强转就会报错
    instanceof
         * result = object instanceof class
         * 典型使用场合
         * 在对对象做向下转型之前,没有其他有关对象类型信息时,务必使用instanceof 来判断一下,避免抛出ClassCastException
         * 
    (3)向下转型时,可以具体实现子类中特有的方法(父类没有)
    //正确的做法
        if(animal1 instanceof Cat){//并没有执行
            System.out.println("hello1");
            Cat cat=(Cat)animal1;
        }

        if(animal2 instanceof Cat){
            System.out.println("hello2");
            Cat cat=(Cat)animal2;
            cat.hh();//此时可以执行子类特有的方法
        }
x

        */

具体实例

要求:

  1. 学校要安装打印机
  2. 打印机有很多种

public class PrinterDemo {

    public static void main(String[] args) {
//      ColorPrinter cp = new ColorPrinter("huipu");
//      School school = new School();
//      school.setColorPrinter(cp);
//  
        Printer p=new ColorPrinter("huipu");
        School school = new School();

        school.setPrinter(p);
        school.print("hello,java");
    }

}
//开闭原则:对修改是封闭的,对扩展是开放的

class School{/*
    private ColorPrinter cp=null;
    private BlackPrinter bp=null;

    //安装彩色打印机
    public void setColorPrinter(ColorPrinter cp){
        this.cp = cp;
    }
    //安装黑白打印机
    public void setBlackPrinter(BlackPrinter bp){
        this.bp=bp;
    }

    //print
    public void print(String content){
        //交给中心打印机打印
        cp.print(content);
    }*/
    private Printer p=null;
    //拿父类的引用变量作为参数,好处就是可以接受任何其他子类的对象
    //越是抽象的东西代表越稳定
    public void setPrinter(Printer p){
        this.p=p;
    } 
    public void print(String content){
        p.print(content);
    }


}
class Printer
{
    private String brand;

    public String getBrand(){
        return brand;
    }
    public Printer(String brand){
        this.brand=brand;
    }
    //让子类去具体实现
    public void print(String content){

    }
}

class ColorPrinter extends Printer{
    public ColorPrinter(String brand){
        super(brand);
    }

    @Override
    public void print(String content){
        System.out.println(getBrand()+"彩色打印:"+content);
    }
}

class BlackPrinter extends Printer{
    public BlackPrinter(String brand){
        super(brand);
    }

    @Override
    public void print(String content){
        System.out.println(super.getBrand()+"黑白打印:"+content);
    }
}

class zhenPrinter extends Printer{
    public zhenPrinter(String brand){
        super(brand);
    }

    @Override
    public void print(String content){
        System.out.println(super.getBrand()+"针式打印:"+content);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值