Java基础复习_day09_多态&抽象类&接口

多态

1. 概念

  • 同一个对象,在不同时刻,表现出不同的形态.(父类类型的变量,保存了子类类型的对象.)

2. 多态的前提

  • 要有继承或实现关系;
  • 要有方法的重写;
  • 要有父类引用指向子类对象;

例:

class Animal {
    public void eat() {
        System.out.println("动物吃东西");
    }
}
//1. 猫类继承动物类, 有继承关系
class Cat extends Animal {
	//2. 有方法重写
    @Override
    public void eat() {
        System.out.println("猫吃鱼");
    }
}

public class AnimalDemo {
    public static void main(String[] args) {
        //3. 有父类引用指向子类对象
        Animal a = new Cat();
        a.eat();
    }
}

3. 成员访问特点

① 成员变量

  • 编译看父类,运行看父类

② 成员方法

  • ​编译看父类,运行看子类

4. 多态的好处和弊端

① 好处

  • 提高程序的扩展性。定义方法时候,使用父类型作为参数,在使用的时候,使用具体的子类型参与操作

② 弊端

  • 不能使用子类的特有成员

5. 多态的案例

  • 请采用多态的思想实现饲养员饲养动物,并在测试类中进行测试
// 动物类
class Animal {
    private String name;
    private int 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 eat() {
        System.out.println("动物要吃饭");
    }
}
// 狗类, 继承动物类
class Dog extends Animal {
    @Override
    public void eat() {
        System.out.println("狗吃肉");
    }
}
// 猫类, 继承动物类
class Cat extends Animal {
    @Override
    public void eat() {
        System.out.println("猫吃鱼");
    }
}
// 饲养员类,
class Feeder {
    // 喂养动物的方法
    public void feed (Animal a, String food) {
        System.out.println("饲养员喂" + a.getName() + "吃" + food);
    }
}
// 测试类
public class Test {
    public static void main(String[] args) {
        // 创建动物对象, 多态的形式
        Animal dog = new Dog();
        dog.setName("旺财");

        Animal cat = new Cat();
        cat.setName("加菲");

        // 创建饲养员对象
        Feeder feeder = new Feeder();

        // 饲养动物
        feeder.feed(dog, "肉");
        dog.eat();
        feeder.feed(cat, "鱼");
        cat.eat();
    }
}

运行结果:
在这里插入图片描述

抽象类

1. 概念

  • 当我们在做子类共性功能抽取时,有些方法在父类中并没有具体的体现,这个时候就需要抽象类了!
  • 在Java中,一个没有方法体的方法应该定义为抽象方法,而类中如果有抽象方法,该类必须定义为抽象类!

2. 特点

① 抽象类和抽象方法必须使用 abstract 关键字修饰

//抽象类的定义
public abstract class 类名 {}

//抽象方法的定义
public abstract void eat();

② 抽象类中不一定有抽象方法,有抽象方法的类一定是抽象类

③ 抽象类不能实例化

  • 抽象类如何实例化呢?参照多态的方式,通过子类对象实例化,这叫抽象类多态

④ 抽象类的子类

  • 要么重写抽象类中的所有抽象方法
  • 要么是抽象类

3. 抽象类案例

abstract class Animal {
    private String name;
    private int age;

    public Animal() {
    }

    public Animal(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 abstract void eat();
}

class Cat extends Animal {
    public Cat() {
    }

    public Cat(String name, int age) {
        super(name, age);
    }

    @Override
    public void eat() {
        System.out.println("猫吃鱼");
    }
}

public class AnimalDemo {
    public static void main(String[] args) {
        //创建对象,按照多态的方式
        Animal a = new Cat();
        a.setName("加菲");
        a.setAge(5);
        System.out.println(a.getName()+","+a.getAge());
        a.eat();
        System.out.println("--------");

        a = new Cat("加菲",5);
        System.out.println(a.getName()+","+a.getAge());
        a.eat();
    }
}

接口

1. 概念

  • 接口就是一种公共的规范标准,只要符合规范标准,大家都可以通用。
  • Java中的接口更多的体现在对行为的抽象!

2. 特点

  • 接口用关键字interface修饰
public interface 接口名 {} 
  • 类实现接口用implements表示
public class 类名 implements 接口名 {}
  • 接口不能实例化
    接口如何实例化呢?参照多态的方式,通过实现类对象实例化,这叫接口多态。
    多态的形式:具体类多态,抽象类多态,接口多态。
  • 接口的子类
    要么重写接口中的所有抽象方法
    要么子类也是抽象类

3. 接口的案例

  • 创建动物类作为父类, 创建鸟类和鱼类, 创建游泳接口和飞翔接口, 让鸟类和鱼类继承动物类,并且鸟类实现飞翔接口, 鱼类实现游泳接口
// 创建动物类
abstract class Animal {
    private String name;
    private int age;

    public Animal() {
    }

    public Animal(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 abstract void eat();
}
// 飞翔接口
interface Fly {
    void fly();
}
// 游泳接口
interface Swim {
    void swim();
}
// 鸟类, 继承Animal, 实现Fly接口
class Bird extends Animal implements Fly {
    @Override
    public void eat() {
        System.out.println(getName() + "吃虫子");
    }

    @Override
    public void fly() {
        System.out.println(getName() + "会飞了");
    }
}
// 鱼类, 继承Animal, 实现Swim接口
class Fish extends Animal implements Swim {
    @Override
    public void eat() {
        System.out.println(getName() + "吃虾米");
    }

    @Override
    public void swim() {
        System.out.println(getName() + "会游泳了");
    }
}
// 测试类
public class Test {
    public static void main(String[] args) {
        // 创建动物对象, 多态形式
        Animal bird = new Bird();
        bird.setName("喜鹊");

        Animal fish = new Fish();
        fish.setName("座头鲸");

        // 调用方法
        bird.eat();
        ((Bird)bird).fly();
        System.out.println("----------");
        fish.eat();
        ((Fish) fish).swim();
    }
}

运行结果:
在这里插入图片描述

发布了22 篇原创文章 · 获赞 4 · 访问量 1282

猜你喜欢

转载自blog.csdn.net/weixin_42931689/article/details/104179081