Java入门5(多态)

打印 上一主题 下一主题

主题 926|帖子 926|积分 2778

多态

编译时的多态:方法重载
运行时的多态:动态绑定
多态的三大前提


  • 类之间要有继承关系
  • 要出现方法重写
  • 父类的引用指向了子类的对象
测试样例
  1. // 定义Person类
  2. public class Person {
  3.     public String name;
  4.     public String sex;
  5.     public int age;
  6.     public Person(String name, String sex, int age) {
  7.         this.name = name;
  8.         this.sex = sex;
  9.         this.age = age;
  10.     }
  11.     public void eat(){
  12.         System.out.println("Person => eat()");
  13.     }
  14. }
复制代码
  1. // 定义Student类,继承Person类
  2. public class Student extends Person{
  3.     public int stuNum;
  4.     public int score;
  5.     public Student(String name, String sex, int age) {
  6.         super(name, sex, age);
  7.     }
  8.     public void study(){
  9.         System.out.println("Student => study()");
  10.     }
  11.         // 重写父类方法
  12.     @Override
  13.     public void eat() {
  14.         System.out.println("Student => eat()");
  15.     }
  16. }
复制代码
  1. // 测试类(Application)
  2. public class Application {
  3.     public static void main(String[] args) {
  4.         Person robot01 = new Student("robot01","female",18);
  5.         Person robot02 = new Person("robot02","male",18);
  6.         robot01.eat(); // Student => eat()
  7.         robot02.eat(); // Person => eat()
  8.         // robot01.study(),爆红
  9.     }
  10. }
复制代码
多态的转型

为什么需要转型

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

​        多态本身是子类类型向父类类型向上转换的过程,这个过程是默认的,当父类引用指向一个子类对象时,便是向上转型
向下转型(子类类型 变量名 = (子类类型) 父类变量名)

​        父类类型向子类类型向下转换的过程,这个过程是强制的,一个已经向上转型的子类对象,将父类引用转为子类引用,可以使用强制类型转换的格式,便是向下转型。
  1. // 定义父类Draw
  2. public class Draw {
  3.     public void drawPicture(){
  4.         System.out.println("Draw => draw()");
  5.     }
  6. }
复制代码
  1. // 定义子类Circle
  2. public class Circle extends Draw{
  3.     // 重写父类方法
  4.     @Override
  5.     public void drawPicture() {
  6.         System.out.println("Circle => DrawCircle()");
  7.     }
  8.     // 子类独有的方法
  9.     public void roll(){
  10.         System.out.println("Circle => roll()");
  11.     }
  12. }
复制代码
  1. // 测试类
  2. public class Application {
  3.     public static void main(String[] args) {
  4.         // 向上转型
  5.         Draw picture = new Circle();
  6.         // 只能使用被重写的方法
  7.         picture.drawPicture(); // Circle => DrawCircle()
  8.         
  9.         // 向下转型
  10.         Circle circlePic = (Circle)picture;
  11.         // 可以使用子类自己的方法
  12.         picture.drawPicture(); // Circle => DrawCircle()
  13.         circlePic.roll(); // Circle => roll()
  14.     }
  15. }
复制代码
instance of 运算符

作用:判断左面的对象是否是右面的类的实例,返回值为bool类型
  1. // 定义父类Father
  2. public class Father {
  3.     public void say(){
  4.         System.out.println("I'm father!");
  5.     }
  6. }
复制代码
  1. // 定义接口Eat
  2. public interface Eat {
  3.     public void eat();
  4. }
复制代码
  1. // 定义子类Son1,继承父类Father以及接口Eat
  2. public class Son1 extends Father implements Eat{
  3.     // 重写父类方法
  4.     @Override
  5.     public void say() {
  6.         System.out.println("I'm son1!");
  7.     }
  8.         // 实现接口方法
  9.     @Override
  10.     public void eat() {
  11.         System.out.println("Son1 => eat()");
  12.     }
  13. }
复制代码
  1. // 测试类(Application)
  2. public class Application {
  3.     public static void main(String[] args) {
  4.         Father robot01 = new Son1();
  5.         Eat robot02 = new Son1();
  6.         robot01.say(); // I'm son1!
  7.         robot02.eat(); // Son1 => eat()
  8.         System.out.println(robot01 instanceof Father); // true
  9.         System.out.println(robot01 instanceof Son1); // true
  10.         System.out.println(robot02 instanceof Father); // true
  11.         System.out.println(robot02 instanceof Son1); // true
  12.     }
  13. }
复制代码
多态的好处


  • 限制类的使用者只能使用父类定义的方法,无法使用子类新定义的方法
  • 可以扩展数据存储的范围
  • 可以拓展方法的参数类型和返回值类型

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

钜形不锈钢水箱

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表