| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 518 人关注过本帖
标题:Java中父类和子类的类型转换解惑
只看楼主 加入收藏
huatainong
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2012-7-30
结帖率:0
收藏
已结贴  问题点数:20 回复次数:1 
Java中父类和子类的类型转换解惑
我们平时经常在项目中经常会看到这样的场景,为实现java运行时绑定的多态性,以父类引用指向子类对象
如 【达内就业】
[java] view plaincopyprint?


Parent p = new Son()


这样做的好处是代码扩展性强,耦合性低。比如一个方法接受Parent的参数类型,那么我们可以再调用方法的时候传递任何一个继承了Parent类型的子类对象作为实参。这样就实现了运行时绑定。
但是,在这种情况下,我们只能通过p调用父类的方法,而不能调用子类的特有方法。所以如果我们想调用子类的特有方法的话就要通过强制类型转换来实现。

[java] view plaincopyprint?


((Son)p).s1() 或 Son s = (Son)p


这就是父类和子类的类型转换存在的意义。
还有另外一种情况,就是将父类引用指向父类对象,而后将父类引用强制转换为子类引用

[java] view plaincopyprint?



Parent p = new Parent();
on s = (Son)p;


java的开发者认为这样毫无意义,所以在开发过程中我们不能这样做,JVM会包类型转换错误的异常。
http://www.
搜索更多相关主题的帖子: Java 多态性 
2012-09-20 15:20
lz1091914999
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:四川
等 级:贵宾
威 望:37
帖 子:2011
专家分:5959
注 册:2010-11-1
收藏
得分:20 
知道为什么要向上转型吗?更多的特性反应在多态上面,比如我们可以看一个经典的例子:
                        图形(Shape)
                             ^
        |--------------------|------------------|
        |                    |                  |
三角形(Triangle)        矩形(Square)        圆形(Round)

程序代码:
abstract class Shape {
    public abstract String getName();
    public abstract void draw();
}

class Triangle extends Shape {
    public String getName() { return "Triangle"; }
    public void draw() {
        System.out.println("Draw a Triangle on the screen.");
    }
}

class Square extends Shape {
    public String getName() { return "Square"; }
    public void draw() {
        System.out.println("Draw a Square on the screen.");
    }
}

class Round extends Shape {
    public String getName() { return "Round"; }
    public void draw() {
        System.out.println("Draw a Round on the screen.");
    }
}

class ShapeGenerator {
    public static Shape getRandomShape() {
        switch ((int)(Math.random() * 3)) {
        default:
        case 0: return new Triangle();
        case 1: return new Square();
        case 2: return new Round();
        }
    }
}

public class ShapeTest {
    public static void main(String[] args) {
        for (int i = 0; i < 10; ++i) {
            Shape shape = ShapeGenerator.getRandomShape();
            System.out.println(shape.getName() + ":");
            shape.draw();
        }
    }
} /*
Square:
Draw a Square on the screen.
Triangle:
Draw a Triangle on the scree
Round:
Draw a Round on the screen.
Triangle:
Draw a Triangle on the scree
Square:
Draw a Square on the screen.
Triangle:
Draw a Triangle on the scree
Triangle:
Draw a Triangle on the scree
Square:
Draw a Square on the screen.
Round:
Draw a Round on the screen.
Round:
Draw a Round on the screen.
*/

在ShapeGenerator的getRandomShape()中,每个对象都是随机生成的,然后向上转型到Shape,在main里可以看到针对不同的Shape都调用了正确的方法,这便是多态性,也被称为方法的后期绑定或动态绑定,在运行时决定调用哪个方法。

[ 本帖最后由 lz1091914999 于 2012-9-21 21:46 编辑 ]

My life is brilliant
2012-09-21 21:44
快速回复:Java中父类和子类的类型转换解惑
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015627 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved