面向对象进阶-多态

发布时间:2023年12月31日

多态的优势

  • 在多态形式下,右边对象可以实现解耦合,便于扩展和维护。

  • 定义方法的时候,使用父类型作为参数,可以接收所有的子类对象,体现多态的扩展性与便利。

多态的弊端

  • 不能调用子类的特有的功能

强制类型转换能解决什么问题:

  • 可以转换成真正的子类类型,从而调用子类独有的功能。

  • 转换类型与真实对象类型不一致会报错。

  • 转换的时候用instanceof关键字进行判断

Person p =new Student();
Srudent s=new (Student)p;

多态的综和练习

package oop.polymorphism.a02polymorphismdemo02;

public class Animal {
    private int age;
  private  String color;

    public Animal() {
    }

    public Animal(int age, String color) {
        this.age = age;
        this.color = color;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
    public void eat(String something){
        System.out.println("动物在吃"+something);
    }
}
package oop.polymorphism.a02polymorphismdemo02;

public class Cat extends Animal{
    public Cat() {
    }

    public Cat(int age, String color) {
        super(age, color);
    }
    @Override
    public void eat(String something){
        System.out.println(getAge()+"岁"+getColor()+"颜色的猫咪眯着眼睛侧着头吃"+something);
    }
    public void catchMouse(){
        System.out.println("猫抓老鼠");
    }
}
package oop.polymorphism.a02polymorphismdemo02;

public class Dog extends Animal{
    public Dog() {
    }

    public Dog(int age, String color) {
        super(age, color);
    }
@Override
public void eat(String someting){
    System.out.println(getAge()+"岁"+getColor()+"颜色的狗两只前腿死死的抱住"+someting+"猛吃");
}
    public void lookHome(){
        System.out.println("狗在看家");
    }
}
package oop.polymorphism.a02polymorphismdemo02;

public class Person {
    private String name;
    private int age;

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    /*
    public  void keepPet(Dog dog,String something){
        System.out.println("年龄为"+age+"岁的"+name+"养了一只"+dog.getColor()+"颜色的"+dog.getAge()+"岁的狗");
        dog.eat(something);
    }
    public  void keepPet(Cat cat,String something){
        System.out.println("年龄为"+age+"岁的"+name+"养了一只"+cat.getColor()+"颜色的"+cat.getAge()+"岁的狗");
        cat.eat(something);
    }*/
    public  void keepPet(Animal animal,String something) {
        if (animal instanceof Dog) {
           Animal   a = new Dog();
            Dog d = (Dog) animal;
        System.out.println("年龄为" + age + "岁的" + name + "养了一只" + d.getColor() + "颜色的" + d.getAge() + "岁的狗");
        d.eat(something);

    }else if (animal instanceof Cat) {
         Animal   a = new Cat();
            Cat c = (Cat) animal;
            System.out.println("年龄为" + age + "岁的" + name + "养了一只" + c.getColor() + "颜色的" + c.getAge() + "岁的猫");
            c.eat(something);
        }else {
            System.out.println("没有这种动物");
        }
    }
}
package oop.polymorphism.a02polymorphismdemo02;

public class Test {
    public static void main(String[] args) {
        /*Person p=new Person("老王",30);
        Dog d=new Dog(2,"黑");
        p.keepPet(d,"骨头");
        Person p2=new Person("karry",24);
        Cat c=new Cat(3,"灰");
        p2.keepPet(c,"鱼");*/
        Person p=new Person("老王",30);
        Dog d=new Dog(2,"黑");
        p.keepPet(d,"骨头");
        Person p2=new Person("karry",24);
        Cat c=new Cat(3,"灰");
        p2.keepPet(c,"鱼");
    }
}

文章来源:https://blog.csdn.net/qq_52326978/article/details/135317796
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。