java -多态

面向对象的多态性:多态性是什么?事物的多种表现形态!
1.方法的重载与重写 2.子类对象的多态性
    使用前提:

              1.要有类的继承,

              2.要有子类对父类方法的重写。
2.程序运行时,分为编译状态和运行状态,
        对于多态性来说,编译时,“看左边”将此引用变量理解为父类的类型
                              运行时,“看右边”关注于真正的实体,子类的对象,那么执行的方法就是子类的重写。

 1 public class TestPerson {
 2   public static void main(String[] args){
 3       //子类对象的多态性,父类的引用指向子类对象或者说子类对象的实体赋给父类引用。
 4       Person p = new Man();
 5       //虚拟方法的调用:通过父类的引用指向子类的对象实体,当调用方法时,实际执行的是子类重写父类的方法。
 6       p.eat();
 7       p.walk();
 8       //此处执行的实体是Man的重写父类的方法。
 9       Man p1 = (Man)p;//强制类型转换,向下转型,使用强转符:()
10       p1.entertaiment();
11       
12 //      Woman p2 =(Woman)p;// java.lang.ClassCastException运行时错误
13 //      p2.shopping();
14       
15       //instanceof
16       //格式:对象a   instanceof  类 A:判断对象a是否是类A的一个实例。是则返回true,否则返回FALSE
17       //若a是A的实例,那么a也一定是A父类的实例。。
18       if(p instanceof Woman){
19           System.out.println("hello");
20           Woman w1 = (Woman)p;
21           w1.shopping();
22       }
23       if(p instanceof Man){
24           Man m1 = (Man)p;
25           m1.entertaiment();
26       }
27       if(p instanceof Person){
28           System.out.println("nihao!");
29       }
30     
31   }
32 } 
 1 package Polymorphism;
 2 
 3 public class Woman extends Person{
 4     private boolean isBeauty;
 5 
 6     public boolean isBeauty() {
 7         return isBeauty;
 8     }
 9     
10     public void setBeauty(boolean isBeauty) {
11         this.isBeauty = isBeauty;
12     }
13     public void walk(){
14         System.out.println("nvrenzoulu");
15     }
16     public void eat(){
17         System.out.println("nvrenxiaolouchifan");
18     }
19     public void shopping(){
20         System.out.println("nvrenmaidongxi");
21     }
22         
23    
24 }
 1 package Polymorphism;
 2 
 3 public class Man extends Person{
 4     private boolean smoking;
 5     
 6 
 7     public Man() {
 8         super();
 9     }
10     
11     public Man(boolean smoking) {
12         super();
13         this.smoking = smoking;
14     }
15 
16     public boolean isSmoking() {
17         return smoking;
18     }
19 
20     public void setSmoking(boolean smoking) {
21         this.smoking = smoking;
22     }
23     public void walk(){
24         System.out.println("zoulu");
25     }
26     public void eat(){
27         System.out.println("chifab");
28     }
29     public void entertaiment(){
30         System.out.println("entertaiment");
31     }
32 }
 1 package Polymorphism;
 2 
 3 public class Person {
 4    private String name;
 5    private int age;
 6    public Person(){
 7        super();
 8    }
 9    
10     public Person(String name, int age) {
11         super();
12         this.name = name;
13         this.age = age;
14     }
15     
16     public String getName() {
17         return name;
18     }
19     public void setName(String name) {
20         this.name = name;
21     }
22     public int getAge() {
23         return age;
24     }
25     public void setAge(int age) {
26         this.age = age;
27     }
28     public void walk(){
29         System.out.println("nanrenbizhidezoulu");
30     }
31     public void eat(){
32         System.out.println("nanrendakouchifab");
33     }
34        
35 }

多态在面向对象程序设计中是一个很重要的概念。仅以此文给大家做点参考。至于想要深入学习,另觅详细教程!

原文地址:https://www.cnblogs.com/Terminaling/p/4105804.html