Java的多态——进阶

总括

Parent p = new Child();

反之,Child() c = new Parent(); 会报错。

当使用多态方式调用方法时,首先检查父类中是否有该方法。

如果没有,则编译错误;

如果有,再去调用子类的同名方法。

下面例子中的p3就是多态。 Person p3 = new Student(); 其中,Person是父类,Student是子类。

举个例子1——静态属性、非静态属性、静态方法

静态属性、非静态属性、静态方法(这三个,对多态调用而言没区别因为多态仅针对非静态方法(可重写)):

1. 非多态时(即等号左右两侧类型一致),p1和p2,调用的就是类自己的属性和方法。

2. 多态时(即等号左侧为父类,等号右侧为子类),p3,调用的就是父类的属性和方法。

1、main()方法

package com.WZ.Demo.Application;

import com.WZ.Demo.ClassSet.Person;
import com.WZ.Demo.ClassSet.Student;

public class Application {
   public static void main(String[] args) {
       Person p1 = new Person();
       Student p2 = new Student();

       Person p3 = new Student();
       //Student p4 = new Person(); //编译报错

       //测试非静态属性
       System.out.println(p1.num); //1
       System.out.println(p2.num); //3
       System.out.println(p3.num); //1
       //测试静态属性
       System.out.println(p1.num); //1
       System.out.println(p2.num); //3
       System.out.println(p3.num); //1
       //测试静态方法
       p1.test2();  //Person
       p2.test2();  //Student
       p3.test2();  //Person
  }
}

2、Person类

package com.WZ.Demo.ClassSet;
public class Person {
   public int num = 1;
   public static int num2 = 1;
   public static void test2()
  {
       System.out.println("Person");
  }
}

3、Student类

package com.WZ.Demo.ClassSet;
public class Student extends Person {
   public int num = 3;
   public static int num2 = 3;
   public static void test2()
  {
       System.out.println("Student");
  }
}

举个例子2——非静态方法

非静态方法(对多态调用而言有区别因为多态是针对非静态方法(可重写)的):

  1. 非多态时(即等号左右两侧类型一致),p1和p2,调用的就是类自己的非静态方法。

  1. 多态时(即等号左侧为父类,等号右侧为子类),p3,时, 若子类有与父类同名的非静态方法,就调用子类的非静态方法(即方法重写,多态)。

1、main()方法

package com.WZ.Demo.Application;
import com.WZ.Demo.ClassSet.Person;
import com.WZ.Demo.ClassSet.Student;
public class Application {
   public static void main(String[] args) {
       Person p1 = new Person();
       Student p2 = new Student();

       Person p3 = new Student();
       //Student p4 = new Person(); //编译报错
       
       //测试非静态方法
       p1.test();  //Person
       p2.test();  //Student
       p3.test();  //Student
  }
}

2、Person类

package com.WZ.Demo.ClassSet;
public class Person {
   public void test()
  {
       System.out.println("Person");
  }  
}

3、Student类

package com.WZ.Demo.ClassSet;
public class Student extends Person {
   public void test()
  {
       System.out.println("Student");
  }
}

举个例子3——非静态方法和静态方法时,父类有方法,子类没有

多态的时候:

  1. 父类有方法,子类没有,则用父类的方法;

  1. 若父类也没该方法,那就报错;

  1. 若子类有该同名的方法,则用子类的,即多态了。

1、main()方法

package com.WZ.Demo.Application;
import com.WZ.Demo.ClassSet.Person;
import com.WZ.Demo.ClassSet.Student;
public class Application {
   public static void main(String[] args) {
       Person p1 = new Person();
       Student p2 = new Student();

       Person p3 = new Student();
       //Student p4 = new Person(); //编译报错

       //测试非静态方法
       p1.test();  //Person
       p2.test();  //Person
       p3.test();  //Person
       //测试静态方法
       p1.test2();  //Person
       p2.test2();  //Person
       p3.test2();  //Person
  }
}

2、Person类

package com.WZ.Demo.ClassSet;
public class Person {
   public void test()
  {
       System.out.println("Person");
  }
   public static void test2()
  {
       System.out.println("Person");
  }
}

3、Student类

package com.WZ.Demo.ClassSet;
public class Student extends Person { }

最新文章

  1. Iterator用法
  2. try-catch 示例
  3. HDU 4036 存疑题目,数论 难度:1
  4. 4-3 yum命令
  5. 初识jQuery 2013-09-26
  6. Linker scripts之Intro
  7. windows下查看某个端口被哪个程序占用的方法
  8. Android消息推送完美解决方案全析
  9. 使用zipalign.exe工具优化APK安装包
  10. about Q&A in installing linux[centos6,7]
  11. [C#]异步委托使用小计
  12. android中的数据库操作(转)
  13. Ppthon基础学习之Dict
  14. JS计算字符串长度(中文算2个)
  15. js的搜索遍历精讲
  16. cxf的使用以及与SSH的整合
  17. java.lang.ClassCastException: net.sf.json.JSONNull cannot be cast to net.sf.json.JSONObject的解决方法
  18. C#常忘语法笔记(C#程序设计基础1-4章)
  19. 如何开启win10的上帝模式
  20. Zephyr的Time、Timer、sleep

热门文章

  1. C# 工具类LogHelper
  2. C#调用Crypto++库AES ECB CBC加解密
  3. vue 项目
  4. Codeforces Round #592 (Div. 2) D - Paint the Tree
  5. hdu 1087 Super Jumping!(类最长上升子序列)
  6. 精简Command版SqlHelper
  7. Dart单例模式最佳实践
  8. 题解【洛谷P1896】[SCOI2005]互不侵犯
  9. Mysql-从库只读设置
  10. Switch The LED Holiday Light To Illuminate The Cheerful Holidays