1. 程式人生 > >面向對象 多態的理解

面向對象 多態的理解

count class pla 郵箱 判斷 是否 聲明 類的繼承 splay

/**
* 面向對象的特征:多態性的使用
*
* 1.多態性:可以理解為一個事物的多種形態。
*
* 2.廣義上多態性的體現:
* ①方法的重載 和 方法的重寫
* ②子類對象的多態性
*
* 3.狹義上多態性的體現:子類對象的多態性
*
* 4.子類對象的多態性:父類的引用指向子類的對象。(子類的對象賦給父類的引用
*
* 5.多態的應用場景:虛擬方法調用:編譯時,認為調用的方法是父類的,但是當運行時,實際執行的是子類重寫父類的方法
*
* 說明:多態中方法的調用:編譯看左邊,運行看右邊!
*
* 6.多態性,只適用於方法!
*
* 7.多態性使用的前提:①類的繼承關系 ②方法的重寫
*
* @author shkstart 郵箱:[email protected]

/* */
* @version 創建時間:2017年7月26日 上午9:44:59
*
*/
public class PersonTest {
public static void main(String[] args) {

Person p1 = new Person();
p1.eat();
p1.walk();

Man m1 = new Man();
m1.eat();
m1.walk();
m1.earnMoney();

System.out.println("**************************");
Person p2 = new Man();//子類對象的多態性。
//多態的應用場景:
//虛擬方法調用:編譯時,認為調用的方法是父類的,但是當運行時,實際執行的是子類重寫父類的方法
p2.eat();
p2.walk();
//不能通過父類的引用調用子類特的方法
// p2.earnMoney();
//總結:多態中方法的調用:編譯看左邊,運行看右邊!

//總結:屬性,不存在多態性的!聲明的引用的類型是什麽,調用的就是相應類中的屬性
System.out.println(p2.id);//1001


System.out.println("**************************");

Person p3 = new Woman();
p3.eat();
p3.walk();

// p3.goShopping();


System.out.println("**************************");
//由於子類對象賦給了父類的引用,導致我們在編譯時,只能調用父類中聲明的結構:屬性和方法。
//但是,我們在內存結構中,又確實存在子類特的屬性和方法。那麽,我們考慮如何才能調用子類所特的屬性、方法。
//向下轉型的使用:強制類型轉換! 強轉符:()
Woman w1 = (Woman)p3;
w1.goShopping();
System.out.println(w1.isBeauty);

//註意:使用強制類型轉換符,可能出現ClassCastException的異常。
// Woman w2 = (Woman)p2;
// w2.goShopping();

/*
* instanceof的使用:
*
* a instanceof A : 判斷對象a是否是類A的實例。如果a是類A的實例,返回true。否則,返回false.
*
*
* 如果 a instanceof A 返回true,則 a instanceof AA 返回也是true.
* 其中,AA是A的父類。
*/

if(p3 instanceof Woman){
System.out.println("****1********");
Woman w3 = (Woman)p3;
w3.goShopping();
}

if(p2 instanceof Woman){
System.out.println("****2********");
Woman w4 = (Woman)p2;
w4.goShopping();
}


if(p2 instanceof Object){
System.out.println("*****3*******");
}


//多態使用上的幾個問題:
//問題一:編譯能通過,運行不通過
// Person p = new Person();
// Man m = (Man)p;
// m.eat();
// m.earnMoney();
// m.isSmoking = true;

//問題二:編譯通過,運行通過
Creature c = new Man();
Person p4 = (Person)c;
p4.eat();

//問題:編譯不通過
//此時的Woman、Man;String、Date之間沒任何子父類的關系
// Woman w = (Woman)(new Man());
// String s = new Date();

//在問題的基礎上,可以通過代碼實現編譯通過,但是運行仍然不通過!
// Person p = new Man();
// Woman w = (Woman)p;

}
}
***************************************************************************************************
package com.atguigu.qustiontest;

/*class Base {
int count = 10;

public void display() {
System.out.println(this.count);
}
}

class Sub extends Base {
int count = 20;

public void display() {
System.out.println(this.count);
}
}

public class QustionTest {
public static void main(String[] args) {
Sub s = new Sub();
System.out.println(s.count); //20
s.display(); //20
Base b = s; //相當於 Base b = new Sub() 這裏相當於多態了
// 多態的用法就是 父類的引用指向子類的對象 ,編譯是認為是調用的方法時父類的 ,
// 但是運行時實際是調用子類對象重寫父類的方法
System.out.println(b == s); // true
System.out.println(b.count); //10

b.display(); //此時是調用子類對象重寫父類的方法,也就是調用的子類的方法 //20
}
}*/
class Base {
int count = 10;

public void display() {
System.out.println(this.count);
}
}

class Sub extends Base {
int count = 20;

// public void display() {
// System.out.println(this.count);
// }
}

public class QustionTest {
public static void main(String[] args) {
Sub s = new Sub();
System.out.println(s.count); //20
s.display(); //10
Base b = s; //
System.out.println(b == s); // true
System.out.println(b.count); //10

b.display(); //此時是調用子類對象重寫父類的方法,也就是調用的子類的方法 //10
}
}
***********************************************************************************************************

面向對象 多態的理解