JAVA学习--面向对象的特征三:多态性

* 面向对象的特征三:多态性
 * 1.多态性指的是什么?多态性,可以理解为一个事物的多种表型形态。
 *   1)方法的重载与重写  2)子类对象的多态性
 *
 * 2.子类对象的多态性使用的前提:①要有类的继承②要有子类对父类方法的重写
 *
 * 3.程序运行分为编译状态和运行状态。
 *   对于多态性来说,编译时,"看左边",将此引用变量理解为父类的类型
 *   运行时,"看右边",关注于真正对象的实体:子类的对象。那么执行的方法就是子类重写的。
 *   
 * 4.子类对象的多态性,并不使用于属性。
 
  1 public class TestPerson {
  2     public static void main(String[] args) {
  3         Person p = new Person();
  4         p.eat();
  5         p.walk();
  6 
  7         Man m = new Man();
  8         m.eat();
  9         m.walk();
 10         System.out.println();
 11 
 12         // 子类对象的多态性:父类的引用指向子类对象
 13         Person p1 = new Man();// 向上转型
 14         // 虚拟方法调用:通过父类的引用指向子类的对象实体,当调用方法时,实际执行的是子类重写父类的方法
 15         p1.eat();
 16         p1.walk();
 17         System.out.println("$" + p1.id);//1001
 18 
 19 //        p1.smoking = null;
 20         // p1.entertainment();
 21 
 22         Person p2 = new Woman();
 23         p2.eat();
 24         p2.walk();
 25         // p2.shopping();
 26         Woman w = (Woman) p2;// 向下转型,使用强转符:()
 27         w.shopping();
 28 
 29         // java.lang.ClassCastException
 30         // Woman w1 = (Woman)p1;
 31         // w1.shopping();
 32 
 33         // Woman w2 = (Woman)new Man();
 34         // instanceof:
 35         // 格式: 对象a instanceof 类A:判断对象a是否是类A的一个实例.是的话,返回true;否则返回false
 36         // 若a是A类的实例,那么a也一定是A类的父类的实例。
 37         if (p1 instanceof Woman) {
 38             System.out.println("hello!");
 39             Woman w1 = (Woman) p1;
 40             w1.shopping();
 41         }
 42 
 43         if (p1 instanceof Man) {
 44             Man m1 = (Man) p1;
 45             m1.entertainment();
 46         }
 47 
 48         if (p1 instanceof Person) {
 49             System.out.println("你好!");
 50         }
 51 
 52     }
 53 
 54     public void show(Person p) {//Person p = new Man();
 55 
 56     }
 57 
 58 
 59 
 60 class Person {
 61     private String name;
 62     private int age;
 63     int id = 1001;
 64     public Person() {
 65         super();
 66     }
 67     public Person(String name, int age) {
 68         super();
 69         this.name = name;
 70         this.age = age;
 71     }
 72     public String getName() {
 73         return name;
 74     }
 75     public void setName(String name) {
 76         this.name = name;
 77     }
 78     public int getAge() {
 79         return age;
 80     }
 81     public void setAge(int age) {
 82         this.age = age;
 83     }
 84    
 85     public void walk(){
 86         System.out.println("人走路");
 87     }
 88     public void eat(){
 89         System.out.println("人吃饭");
 90     }
 91 
 92 }
 93 
 94 
 95 
 96 
 97 class Man extends Person{
 98      boolean smoking;
 99      int id = 1002;
100     public Man() {
101         super();
102     }
103    
104     public Man(boolean smoking) {
105         super();
106         this.smoking = smoking;
107     }
108 
109     public boolean isSmoking() {
110         return smoking;
111     }
112 
113     public void setSmoking(boolean smoking) {
114         this.smoking = smoking;
115     }
116    
117     public void walk(){
118         System.out.println("男人笔挺的走路");
119     }
120     public void eat(){
121         System.out.println("男人应该多吃肉!");
122     }
123    
124     public void entertainment(){
125         System.out.println("男人请客");
126     }
127 }
128 
129 
130 class Woman extends Person{
131     private boolean isBeauty;
132 
133     public boolean isBeauty() {
134         return isBeauty;
135     }
136 
137     public void setBeauty(boolean isBeauty) {
138         this.isBeauty = isBeauty;
139     }
140 
141     public Woman() {
142         super();
143     }
144 
145     public Woman(boolean isBeauty) {
146         super();
147         this.isBeauty = isBeauty;
148     }
149     public void walk(){
150         System.out.println("女人窈窕的走路。。。");
151     }
152    
153     public void eat(){
154         System.out.println("女人应该少吃,减肥");
155     }
156     public void shopping(){
157         System.out.println("女人爱购物");
158     }
159    
160 }
原文地址:https://www.cnblogs.com/zhangfan94/p/4263277.html