文章目录
1. Java中的多态性理解java编程思想——Java中的动态、静态绑定(前期、后期绑定)
2.向上转型和向下转型一.向上转型二.向下转型
参考资料:
1. Java中的多态性理解
Java中除了static方法和final方法(private方法本质上属于final方法,因为不能被子类访问)之外,其它所有的方法都是动态绑定,这意味着通常情况下,我们不必判定是否应该进行动态绑定—它会自动发生。
java编程思想——Java中的动态、静态绑定(前期、后期绑定)
final方法会使编译器生成更有效的代码,这也是为什么说声明为final方法能在一定程度上提高性能(效果不明显)。如果某个方法是静态的,它的行为就不具有多态性:
class StaticSuper {
public static String
staticGet() {
return "Base staticGet()";
}
public String
dynamicGet() {
return "Base dynamicGet()";
}
}
class StaticSub extends StaticSuper {
public static String
staticGet() {
return "Derived staticGet()";
}
public String
dynamicGet() {
return "Derived dynamicGet()";
}
}
public class StaticPolymorphism {
public static void main(String
[] args
) {
StaticSuper sup
= new StaticSub();
System
.out
.println(sup
.staticGet());
System
.out
.println(sup
.dynamicGet());
}
}
输出:
Base
staticGet()
Derived
dynamicGet()
构造函数并不具有多态性,它们实际上是static方法,只不过该static声明是隐式的。因此,构造函数不能够被override。
在父类构造函数内部调用具有多态行为的函数将导致无法预测的结果,因为此时子类对象还没初始化,此时调用子类方法不会得到我们想要的结果。
class A {
void draw() { System
.out
.println("A.draw()"); }
A() {
System
.out
.println("A() before draw()");
draw();
System
.out
.println("A() after draw()");
}
}
class B extends A {
private int b
= 1;
B(int b
) {
this.b
= b
;
System
.out
.println("B(), b = " + this.b
);
}
void draw() {
System
.out
.println("B.draw(), b = " + this.b
);
}
}
public class Test {
public static void main(String
[] args
) {
new B(5);
}
}
本来以为会输出:
A() before
draw()
A
.draw()
A() after
draw()
B(), b
= 5
其实真正的输出为:
A() before
draw()
B
.draw(), b
= 0
A() after
draw()
B(), b
= 5
这是解释: 1、父类构造器先于子类构造器被调用,所以会先输出A() before draw(); 2、调用run()时,虽然子类对象还没生成,但是这里的draw()方法仍然是动态方法,即后期绑定 ,所以调用的还是子类的draw()方法。另外由于创建对象时,是分配空间+初始化空间的,所以此时b的值为0,于是输出:B.draw(), b=0; 除非这个b属性是静态属性而且是声明的时候就已经赋值过了,那么输出的b就不是0了,而是初始赋的那个值。 3、然后接着运行父类构造器,输出A() after draw(); 4、最后运行子类构造器,此时子类的各属性都已经被初始化,所以b被赋值,输出:B(), b=5
只有非private方法才可以被覆盖,但是还需要密切注意覆盖private方法的现象,这时虽然编译器不会报错,但是也不会按照我们所期望的来执行,即覆盖private方法对子类来说是一个新的方法而非重载方法。因此,在子类中,新方法名最好不要与基类的private方法采取同一名字(虽然没关系,但容易误解,以为能够覆盖基类的private方法)。Java类中属性域的访问操作都由编译器解析,因此不是多态的。父类和子类的同名属性都会分配不同的存储空间,如下:
class Super {
public int field
= 0;
public int getField() {
return field
;
}
}
class Sub extends Super {
public int field
= 1;
public int getField() {
return field
;
}
public int getSuperField() {
return super.field
;
}
}
public class FieldAccess {
public static void main(String
[] args
) {
Super sup
= new Sub();
System
.out
.println("sup.filed = " + sup
.field
+
", sup.getField() = " + sup
.getField());
Sub sub
= new Sub();
System
.out
.println("sub.filed = " + sub
.field
+
", sub.getField() = " + sub
.getField() +
", sub.getSuperField() = " + sub
.getSuperField());
}
}
输出:
sup
.filed
= 0, sup
.getField() = 1
sub
.filed
= 1, sub
.getField() = 1, sub
.getSuperField() = 0
Sub子类实际上包含了两个称为field的域,然而在引用Sub中的field时所产生的默认域并非Super版本的field域,因此为了得到Super.field,必须显式地指明super.field。
总而言之:注意多态只是对方法调用起作用的,对域(成员变量)是不起作用的,即对域的调用是编译器解析的, 类中域与静态方法的调用是在编译期进行解析的,不具有多态性。
2.向上转型和向下转型
一.向上转型
package com
.sheepmu
;
class Animal
{
public void eat()
{
System
.out
.println("父类的 eating...");
}
}
class Bird extends Animal
{
@Override
public void eat()
{
System
.out
.println("子类重写的父类的 eatting...");
}
public void fly()
{
System
.out
.println("子类新方法 flying...");
}
}
public class Sys
{
public static void main(String
[] args
)
{
Animal b
=new Bird();
b
.eat();
sleep(new Male());
sleep(new Female());
}
public static void sleep(Human h
)
{
h
.sleep();
}
}
package com
.sheepmu
;
public class Human
{
public void sleep()
{
System
.out
.println("父类人类 sleep..");
}
}
class Male extends Human
{
@Override
public void sleep()
{
System
.out
.println("男人 sleep..");
}
}
class Female extends Human
{
@Override
public void sleep()
{
System
.out
.println("女人 sleep..");
}
}
输出: 子类重写的父类的 eatting… 男人 sleep… 女人 sleep…
详解:
向上转型的实现
Animal b=new Bird(); //向上转型
b.eat(); // 调用的是子类的eat()方法
b.fly(); // 报错!!!!!-------b虽指向子类对象,但此时子类作为向上转型的代价丢失和父类不同的fly()方法------
2.为何不直接Bird b=new Bird();b.eat() 呢?
这样就没有体现出面向对象的抽象的编程思想呀,降低了代码的可扩展性.
3.向上转型的好处?
sleep(new Male());//调用方法时传入的参数是子类
sleep(new Female());
public static void sleep(Human h) //方法的参数是父类
{
h.sleep();
}
如上代码就是用的向上转型,若是不用向上转型,那么有多少个子类就得在这儿写多少种不同的睡觉方法~~~~~~
二.向下转型
package com
.sheepmu
;
class Fruit
{
public void myName()
{
System
.out
.println("我是父类 水果...");
}
}
class Apple extends Fruit
{
@Override
public void myName()
{
System
.out
.println("我是子类 苹果...");
}
public void myMore()
{
System
.out
.println("我是你的小呀小苹果~~~~~~");
}
}
public class Sys{
public static void main(String
[] args
) {
Fruit a
=new Apple();
a
.myName();
Apple aa
=(Apple
)a
;
aa
.myName();
aa
.myMore();;
Fruit f
=new Fruit();
Apple aaa
=(Apple
)f
;
aaa
.myName();
aaa
.myMore();
}
}
输出: 我是子类 苹果… 我是子类 苹果… 我是你的小呀小苹果~~~~~~ Exception in thread “main” java.lang.ClassCastException: com.sheepmu.Fruit cannot be cast to com.sheepmu.Apple at com.sheepmu.Sys.main(Sys.java:30)
详解:
1.正确的向下转型
Fruit a=new Apple(); //向上转型 a.myName(); Apple aa=(Apple)a; //向下转型,编译和运行皆不会出错(正确的) aa.myName(); aa.myMore();
a指向子类的对象,所以子类的实例aa也可以指向a啊~~
向下转型后因为都是指向子类对象,所以调用的当然全是子类的方法~~
2.不安全的向下转型
Fruit f=new Fruit(); Apple aaa=(Apple)f; //-不安全的—向下转型,编译无错但会运行会出错 aaa.myName(); aaa.myMore();
f是父类对象,子类的实例aaa肯定不能指向父类f啊~~~
3.Java为了解决不安全的向下转型问题,引入泛型的概念
4.为了安全的类型转换,最好先用 if(A instanceof B) 判断一下下~~
参考资料:
Java向上转型和向下转型(附详细例子)