设为首页 加入收藏

TOP

Java入门5(多态)
2023-07-25 21:32:40 】 浏览:22
Tags:Java 入门 多态

多态

编译时的多态:方法重载

运行时的多态:动态绑定

多态的三大前提

  1. 类之间要有继承关系
  2. 要出现方法重写
  3. 父类的引用指向了子类的对象

测试样例

// 定义Person类
public class Person {
    public String name;
    public String sex;
    public int age;
    public Person(String name, String sex, int age) {
        this.name = name;
        this.sex = sex;
        this.age = age;
    }
    public void eat(){
        System.out.println("Person => eat()");
    }
}
// 定义Student类,继承Person类
public class Student extends Person{
    public int stuNum;
    public int score;
    public Student(String name, String sex, int age) {
        super(name, sex, age);
    }
    public void study(){
        System.out.println("Student => study()");
    }
	// 重写父类方法
    @Override
    public void eat() {
        System.out.println("Student => eat()");
    }
}
// 测试类(Application)
public class Application {
    public static void main(String[] args) {
        Person robot01 = new Student("robot01","female",18);
        Person robot02 = new Person("robot02","male",18);
        robot01.eat(); // Student => eat()
        robot02.eat(); // Person => eat()
        // robot01.study(),爆红
    }
}

多态的转型

为什么需要转型

? 当使用多态方式调用方法时,首先检查父类中是否有该方法,如果没有,则编译错误,也就是说,不能调用子类拥有 而父类没有的方法,编译都错误,更别说运行了,所以,想要调用子类特有的方法,必须做向下转型

向上转型(父类类型 变量名 = 子类对象

? 多态本身是子类类型向父类类型向上转换的过程,这个过程是默认的,当父类引用指向一个子类对象时,便是向上转型

向下转型(子类类型 变量名 = (子类类型) 父类变量名

? 父类类型向子类类型向下转换的过程,这个过程是强制的,一个已经向上转型的子类对象,将父类引用转为子类引用,可以使用强制类型转换的格式,便是向下转型。

// 定义父类Draw
public class Draw {
    public void drawPicture(){
        System.out.println("Draw => draw()");
    }
}
// 定义子类Circle
public class Circle extends Draw{
    // 重写父类方法
    @Override
    public void drawPicture() {
        System.out.println("Circle => DrawCircle()");
    }
    // 子类独有的方法
    public void roll(){
        System.out.println("Circle => roll()");
    }
}
// 测试类
public class Application {
    public static void main(String[] args) {
        // 向上转型
        Draw picture = new Circle();
        // 只能使用被重写的方法
        picture.drawPicture(); // Circle => DrawCircle()
        
        // 向下转型
        Circle circlePic = (Circle)picture;
        // 可以使用子类自己的方法
        picture.drawPicture(); // Circle => DrawCircle()
        circlePic.roll(); // Circle => roll()
    }
}

instance of 运算符

作用:判断左面的对象是否是右面的类的实例,返回值为bool类型

// 定义父类Father
public class Father {
    public void say(){
        System.out.println("I'm father!");
    }
}
// 定义接口Eat
public interface Eat {
    public void eat();
}
// 定义子类Son1,继承父类Father以及接口Eat
public class Son1 extends Father implements Eat{
    // 重写父类方法
    @Override
    public void say() {
        System.out.println("I'm son1!");
    }
	// 实现接口方法
    @Override
    public void eat() {
        System.out.println("Son1 => eat()");
    }
}
// 测试类(Application)
public class Application {
    public static void main(String[] args) {
        Father robot01 = new Son1();
        Eat robot02 = new Son1();
        robot01.say(); // I'm son1!
        robot02.eat(); // Son1 => eat()
        System.out.println(robot01 instanceof Father); // true
        System.out.println(robot01 instanceof Son1); // true
        System.out.println(robot02 instanceof Father); // true
        System.out.println(robot02 instanceof Son1); // true
    }
}

多态的好处

  1. 限制类的使用者只能使用父类定义的方法,无法使用子类新定义的方法
  2. 可以扩展数据存储的范围
  3. 可以拓展方法的参数类型和返回值类型
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇万字干货_JDK动态代理及其源码解.. 下一篇Java 重写(重点,一定要掌握!)

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目