JAVA基础(多态的利弊)

1,多态的好处和弊端

【1】多态的好处

  • 提高了代码的维护性(继承保证)

  • 提高了代码的扩展性(由多态保证)

  • 可以当作形式参数,可以接收任意子类对象

【2】多态的弊端

  • 不能使用子类的特有属性和行为。

class Demo4_Animal {

    public static void main(String[] args) {

        //Cat c1 = new Cat();

        //c1.eat();

        method(new Cat());

        method(new Dog());





        //Animal a = new Cat();            开发的是很少在创建对象的时候用父类引用指向子类对象,直接创建子类对象更方便,可以使用子类中的特有属性和行为

    }

    

    //Cat c = new Dog();狗是一只猫,这是错误的

    /*public static void method(Cat c) {            

        c.eat();

    }





    public static void method(Dog d) {

        d.eat();

    }*/

    

    //如果把狗强转成猫就会出现类型转换异常,ClassCastException

    public static void method(Animal a) {    //当作参数的时候用多态最好,因为扩展性强

        //关键字 instanceof 判断前边的引用是否是后边的数据类型

        if (a instanceof Cat) {

            Cat c = (Cat)a;

            c.eat();

            c.catchMouse();

        }else if (a instanceof Dog) {

            Dog d = (Dog)a;

            d.eat();

            d.lookHome();

        }else {

            a.eat();

        }

    }

}





class Animal {

    public void eat() {

        System.out.println("动物吃饭");

    }

}





class Cat extends Animal {

    public void eat() {

        System.out.println("猫吃鱼");

    }





    public void catchMouse() {

        System.out.println("抓老鼠");

    }

}





class Dog extends Animal {

    public void eat() {

        System.out.println("狗吃肉");

    }





    public void lookHome() {

        System.out.println("看家");

    }

}

2,练习

class Test1_Polymorphic {

    public static void main(String[] args) {

        Fu f = new Zi();

        //f.method();        //编译不通过,会报错

        f.show();            //调用的是子类的show方法

    }

}

class Fu {

    public void show() {

        System.out.println("fu show");

    }

}





class Zi extends Fu {

    public void show() {

        System.out.println("zi show");

    }





    public void method() {

        System.out.println("zi method");

    }

}

* B:看下面程序是否有问题,如果没有,说出结果

class Test2_Polymorphic {

    public static void main(String[] args) {

        //A a = new B();

        //a.show();

        

        B b = new C();

        b.show();

    }

}

class A {

    public void show() {

        show2();

        System.out.println("我是父类中的构造");

    }

    public void show2() {

        System.out.println("我");

    }

}

class B extends A {

    public void show2() {

        System.out.println("爱");

    }

}

class C extends B {

    public void show() {

        super.show();

    }

    public void show2() {

        System.out.println("你");

    }

}

猜你喜欢

转载自blog.csdn.net/Cricket_7/article/details/92407334