在java 中,什么叫向上转型?

Python08

在java 中,什么叫向上转型?,第1张

java 转型问题其实并不复杂,只要记住一句话:父类引用指向子类对象。\x0d\x0a\x0d\x0a什么叫父类引用指向子类对象,且听我慢慢道来.\x0d\x0a\x0d\x0a从2个名词开始说起:向上转型(upcasting) 、向下转型(downcasting).\x0d\x0a\x0d\x0a举个例子:有2个类,Father是父类,Son类继承自Father。\x0d\x0a\x0d\x0aFather f1 = new Son() // 这就叫 upcasting (向上转型)\x0d\x0a\x0d\x0a// 现在f1引用指向一个Son对象\x0d\x0a\x0d\x0aSon s1 = (Son)f1 // 这就叫 downcasting (向下转型)\x0d\x0a\x0d\x0a// 现在f1还是指向Son对象\x0d\x0a\x0d\x0a第2个例子:\x0d\x0a\x0d\x0aFather f2 = new Father()\x0d\x0a\x0d\x0aSon s2 = (Son)f2 // 出错,子类引用不能指向父类对象\x0d\x0a\x0d\x0a你或许会问,第1个例子中:Son s1 = (Son)f1问什么 是正确的呢。\x0d\x0a\x0d\x0a很简单因为f1指向一个子类对象,Father f1 = new Son()子类s1引用当然可以指向子类对象了。\x0d\x0a\x0d\x0a而f2 被传给了一个Father对象,Father f2 = new Father();子类s1引用不能指向父类对象。\x0d\x0a\x0d\x0a总结:\x0d\x0a\x0d\x0a1。父类引用指向子类对象,而子类引用不能指向父类对象。\x0d\x0a\x0d\x0a2。把子类对象直接赋给父类引用叫upcasting向上转型,向上转型不用强制转换。\x0d\x0a\x0d\x0a 如:Father f1 = new Son()\x0d\x0a\x0d\x0a3。把指向子类对象的父类引用赋给子类引用叫向下转型(downcasting),要强制转换。\x0d\x0a\x0d\x0a 如:f1 就是一个指向子类对象的父类引用。把f1赋给子类引用s1即 Son s1 = (Son)f1;\x0d\x0a\x0d\x0a 其中f1前面的(Son)必须加上,进行强制转换。\x0d\x0a\x0d\x0a一、向上转型。\x0d\x0a通俗地讲即是将子类对象转为父类对象。此处父类对象可以是接口。\x0d\x0a\x0d\x0a1,向上转型中的方法调用。\x0d\x0a\x0d\x0a看下面代码:\x0d\x0a\x0d\x0a[java] view plaincopyprint?\x0d\x0apackage com.wensefu.others \x0d\x0apublic class Animal { \x0d\x0a \x0d\x0apublic void eat(){ \x0d\x0aSystem.out.println("animal eatting...") \x0d\x0a} \x0d\x0a} \x0d\x0aclass Bird extends Animal{ \x0d\x0a \x0d\x0apublic void eat(){ \x0d\x0aSystem.out.println("bird eatting...") \x0d\x0a} \x0d\x0a \x0d\x0apublic void fly(){ \x0d\x0a \x0d\x0aSystem.out.println("bird flying...") \x0d\x0a} \x0d\x0a} \x0d\x0aclass Main{ \x0d\x0a \x0d\x0apublic static void main(String[] args) { \x0d\x0a \x0d\x0aAnimal b=new Bird()//向上转型 \x0d\x0ab.eat() \x0d\x0a//! error: b.fly()b虽指向子类对象,但此时丢失fly()方法 \x0d\x0adosleep(new Male()) \x0d\x0adosleep(new Female()) \x0d\x0a} \x0d\x0a \x0d\x0apublic static void dosleep(Human h) { \x0d\x0ah.sleep() \x0d\x0a} \x0d\x0a} \x0d\x0a\x0d\x0a[java] view plaincopyprint?\x0d\x0apackage com.wensefu.others \x0d\x0apublic class Human { \x0d\x0apublic void sleep() { \x0d\x0aSystem.out.println("Human sleep..") \x0d\x0a} \x0d\x0a} \x0d\x0aclass Male extends Human { \x0d\x0a@Override \x0d\x0apublic void sleep() { \x0d\x0aSystem.out.println("Male sleep..") \x0d\x0a} \x0d\x0a} \x0d\x0aclass Female extends Human { \x0d\x0a@Override \x0d\x0apublic void sleep() { \x0d\x0aSystem.out.println("Female sleep..") \x0d\x0a} \x0d\x0a} \x0d\x0a\x0d\x0a注意这里的向上转型:\x0d\x0aAnimal b=new Bird()//向上转型\x0d\x0ab.eat()\x0d\x0a\x0d\x0a此处将调用子类的eat()方法。原因:b实际指向的是Bird子类,故调用时会调用子类本身的方法。\x0d\x0a\x0d\x0a需要注意的是向上转型时b会遗失除与父类对象共有的其他方法。如本例中的fly方法不再为b所有。\x0d\x0a\x0d\x0a2,向上转型的好处。\x0d\x0a\x0d\x0a看上面的代码,\x0d\x0a\x0d\x0apublic static void dosleep(Human h) {\x0d\x0ah.sleep()\x0d\x0a}\x0d\x0a\x0d\x0a这里以父类为参数,调有时用子类作为参数,就是利用了向上转型。这样使代码变得简洁。不然的话,\x0d\x0a如果dosleep以子类对象为参数,则有多少个子类就需要写多少个函数。这也体现了JAVA的抽象编程思想。\x0d\x0a\x0d\x0a二、向下转型。\x0d\x0a\x0d\x0a与向上转型相反,即是把父类对象转为子类对象。\x0d\x0a\x0d\x0a看下面代码:\x0d\x0a\x0d\x0a[java] view plaincopyprint?\x0d\x0apackage com.wensefu.other1 \x0d\x0apublic class Girl { \x0d\x0apublic void smile(){ \x0d\x0aSystem.out.println("girl smile()...") \x0d\x0a} \x0d\x0a} \x0d\x0aclass MMGirl extends Girl{ \x0d\x0a \x0d\x0a@Override \x0d\x0apublic void smile() { \x0d\x0a \x0d\x0aSystem.out.println("MMirl smile sounds sweet...") \x0d\x0a} \x0d\x0apublic void c(){ \x0d\x0aSystem.out.println("MMirl c()...") \x0d\x0a} \x0d\x0a} \x0d\x0aclass Main{ \x0d\x0a \x0d\x0apublic static void main(String[] args) { \x0d\x0a \x0d\x0aGirl g1=new MMGirl()//向上转型 \x0d\x0ag1.smile() \x0d\x0a \x0d\x0aMMGirl mmg=(MMGirl)g1//向下转型,编译和运行皆不会出错 \x0d\x0ammg.smile() \x0d\x0ammg.c() \x0d\x0a\x0d\x0aGirl g2=new Girl() \x0d\x0a// MMGirl mmg1=(MMGirl)g2//不安全的向下转型,编译无错但会运行会出错 \x0d\x0a// mmg1.smile() \x0d\x0a// mmg1.c() \x0d\x0a/*output: \x0d\x0a* CGirl smile sounds sweet... \x0d\x0a* CGirl smile sounds sweet... \x0d\x0a* CGirl c()... \x0d\x0a* Exception in thread "main" java.lang.ClassCastException: com.wensefu.other1.Girl \x0d\x0a* at com.wensefu.other1.Main.main(Girl.java:36) \x0d\x0a*/ \x0d\x0aif(g2 instanceof MMGirl){ \x0d\x0aMMGirl mmg1=(MMGirl)g2 \x0d\x0ammg1.smile() \x0d\x0ammg1.c() \x0d\x0a} \x0d\x0a \x0d\x0a} \x0d\x0a} \x0d\x0a\x0d\x0aGirl g1=new MMGirl()//向上转型\x0d\x0ag1.smile()\x0d\x0a MMGirl mmg=(MMGirl)g1//向下转型,编译和运行皆不会出错\x0d\x0a\x0d\x0a这里的向下转型是安全的。因为g1指向的是子类对象。\x0d\x0a\x0d\x0a而\x0d\x0aGirl g2=new Girl()\x0d\x0aMMGirl mmg1=(MMGirl)g2//不安全的向下转型,编译无错但会运行会出错\x0d\x0a\x0d\x0a运行出错:\x0d\x0a\x0d\x0aException in thread "main" java.lang.ClassCastException: com.wensefu.other1.Girl\x0d\x0aat com.wensefu.other1.Main.main(Girl.java:36)\x0d\x0a如代码所示,可以通过instanceof来防止出现异常。

java 转型问题其实并不复杂,只要记住一句话:父类引用指向子类对象。

什么叫父类引用指向子类对象,且听我慢慢道来.

从2个名词开始说起:向上转型(upcasting) 、向下转型(downcasting).

举个例子:有2个类,Father是父类,Son类继承自Father。

Father f1 = new Son() // 这就叫 upcasting (向上转型)

// 现在f1引用指向一个Son对象

Son s1 = (Son)f1 // 这就叫 downcasting (向下转型)

// 现在f1还是指向Son对象

第2个例子:

Father f2 = new Father()

Son s2 = (Son)f2 // 出错,子类引用不能指向父类对象

你或许会问,第1个例子中:Son s1 = (Son)f1问什么 是正确的呢。

很简单因为f1指向一个子类对象,Father f1 = new Son()子类s1引用当然可以指向子类对象了。

而f2 被传给了一个Father对象,Father f2 = new Father();子类s1引用不能指向父类对象。

总结:

1。父类引用指向子类对象,而子类引用不能指向父类对象。

2。把子类对象直接赋给父类引用叫upcasting向上转型,向上转型不用强制转换。

如:Father f1 = new Son()

3。把指向子类对象的父类引用赋给子类引用叫向下转型(downcasting),要强制转换。

如:f1 就是一个指向子类对象的父类引用。把f1赋给子类引用s1即 Son s1 = (Son)f1;

其中f1前面的(Son)必须加上,进行强制转换。

一、向上转型。

通俗地讲即是将子类对象转为父类对象。此处父类对象可以是接口。

1,向上转型中的方法调用。

看下面代码:

[java] view plaincopyprint?

package com.wensefu.others

public class Animal {

public void eat(){

System.out.println("animal eatting...")

}

}

class Bird extends Animal{

public void eat(){

System.out.println("bird eatting...")

}

public void fly(){

System.out.println("bird flying...")

}

}

class Main{

public static void main(String[] args) {

Animal b=new Bird()//向上转型

b.eat()

//! error: b.fly()b虽指向子类对象,但此时丢失fly()方法

dosleep(new Male())

dosleep(new Female())

}

public static void dosleep(Human h) {

h.sleep()

}

}

[java] view plaincopyprint?

package com.wensefu.others

public class Human {

public void sleep() {

System.out.println("Human sleep..")

}

}

class Male extends Human {

@Override

public void sleep() {

System.out.println("Male sleep..")

}

}

class Female extends Human {

@Override

public void sleep() {

System.out.println("Female sleep..")

}

}

注意这里的向上转型:

Animal b=new Bird()//向上转型

b.eat()

此处将调用子类的eat()方法。原因:b实际指向的是Bird子类,故调用时会调用子类本身的方法。

需要注意的是向上转型时b会遗失除与父类对象共有的其他方法。如本例中的fly方法不再为b所有。

2,向上转型的好处。

看上面的代码,

public static void dosleep(Human h) {

h.sleep()

}

这里以父类为参数,调有时用子类作为参数,就是利用了向上转型。这样使代码变得简洁。不然的话,

如果dosleep以子类对象为参数,则有多少个子类就需要写多少个函数。这也体现了JAVA的抽象编程思想。

二、向下转型。

与向上转型相反,即是把父类对象转为子类对象。

看下面代码:

[java] view plaincopyprint?

package com.wensefu.other1

public class Girl {

public void smile(){

System.out.println("girl smile()...")

}

}

class MMGirl extends Girl{

@Override

public void smile() {

System.out.println("MMirl smile sounds sweet...")

}

public void c(){

System.out.println("MMirl c()...")

}

}

class Main{

public static void main(String[] args) {

Girl g1=new MMGirl()//向上转型

g1.smile()

MMGirl mmg=(MMGirl)g1//向下转型,编译和运行皆不会出错

mmg.smile()

mmg.c()

Girl g2=new Girl()

// MMGirl mmg1=(MMGirl)g2//不安全的向下转型,编译无错但会运行会出错

// mmg1.smile()

// mmg1.c()

/*output:

* CGirl smile sounds sweet...

* CGirl smile sounds sweet...

* CGirl c()...

* Exception in thread "main" java.lang.ClassCastException: com.wensefu.other1.Girl

* at com.wensefu.other1.Main.main(Girl.java:36)

*/

if(g2 instanceof MMGirl){

MMGirl mmg1=(MMGirl)g2

mmg1.smile()

mmg1.c()

}

}

}

Girl g1=new MMGirl()//向上转型

g1.smile()

MMGirl mmg=(MMGirl)g1//向下转型,编译和运行皆不会出错

这里的向下转型是安全的。因为g1指向的是子类对象。

Girl g2=new Girl()

MMGirl mmg1=(MMGirl)g2//不安全的向下转型,编译无错但会运行会出错

运行出错:

Exception in thread "main" java.lang.ClassCastException: com.wensefu.other1.Girl

at com.wensefu.other1.Main.main(Girl.java:36)

如代码所示,可以通过instanceof来防止出现异常。