日期:2014-05-20  浏览次数:20733 次

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

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");
}