码迷,mamicode.com
首页 > 其他好文 > 详细

面向对象 多态的理解

时间:2017-07-29 00:00:59      阅读:270      评论:0      收藏:0      [点我收藏+]

标签:代码   场景   cas   转换   通过   内存   对象   author   子类   

/**
* 面向对象的特征:多态性的使用
*
* 1.多态性:可以理解为一个事物的多种形态。
*
* 2.广义上多态性的体现:
* ①方法的重载 和 方法的重写
* ②子类对象的多态性
*
* 3.狭义上多态性的体现:子类对象的多态性
*
* 4.子类对象的多态性:父类的引用指向子类的对象。(子类的对象赋给父类的引用
*
* 5.多态的应用场景:虚拟方法调用:编译时,认为调用的方法是父类的,但是当运行时,实际执行的是子类重写父类的方法
*
* 说明:多态中方法的调用:编译看左边,运行看右边!
*
* 6.多态性,只适用于方法!
*
* 7.多态性使用的前提:①类的继承关系 ②方法的重写
*
* @author shkstart 邮箱:shkstart@126.com
* @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
}
}
***********************************************************************************************************

面向对象 多态的理解

标签:代码   场景   cas   转换   通过   内存   对象   author   子类   

原文地址:http://www.cnblogs.com/loushiqiang/p/7252918.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!