首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

一点小小的疑问

2013-02-04 
一点小小的疑问,请教大家public class TestString {public static void main(String[] args) {Point p1

一点小小的疑问,请教大家


public class TestString {
public static void main(String[] args) {
Point p1 = new Point();
Point p2 = new ColorPoint();
ColorPoint p3 = new ColorPoint();
 p1.draw(p1);
 p1.draw(p2);
 p2.draw(p3);
p2.draw(p1);
p2.draw(p2);
p2.draw(p3);
}
}
class Point {
public void draw(Point p) {
System.out.println("Draw in Point");
}
}
class ColorPoint extends Point {
public void draw(ColorPoint p) {
System.out.println("Draw in ColorPoint");
}
}
}

请问为什么调用的全是父类的方法? java
[解决办法]
ColorPoint 类的draw(ColorPoint p) 方法,参数改为Point类型的。
重写的方法,参数类型要一致,这个没有多态。
[解决办法]
这个类里面还隐藏着一个方法
class ColorPoint extends Point {
public void draw(Point p) {
System.out.println("Draw in Point");
}
//这个方法不是重构因为参数不一样
public void draw(ColorPoint p) {
System.out.println("Draw in ColorPoint");
}

热点排行
Bad Request.