【发布时间】:2017-01-26 06:22:42
【问题描述】:
我有这些课程;
public class Car extends JComponent {
}
public class Mazda extends Car {
}
public class Subaru extends Car {
}
在我的汽车类中,我重写了方法绘制组件
@Override
public void paintComponent(Graphics g) {
//why my planets aren't painted by this method
if (this instanceof Mazda) {
g.fillOval(0, 0, this.getWidth(), this.getHeight());
System.out.println(this.getClass());
}
if (this instanceof Subaru) {
g.setColor(Color.blue);
g.fillOval(0, 0, this.getWidth(), this.getHeight());
System.out.println(this.getClass());
}
}
它很好地绘制了马自达的实例,但从未调用过斯巴鲁实例的代码。看来斯巴鲁没有从 Car 继承 Jcomponent?或者为什么不调用 painComponent? Java 新手,所以我可能缺少一些基本的东西
【问题讨论】:
-
为什么不覆盖
Mazda和Subaru中的paintComponent? -
我试过了,但只有马自达代码有效,斯巴鲁没有,我也尝试在汽车中使其抽象,但没有运气。
-
如何以及在哪里添加实例?
-
考虑创建一个MCVE。删除额外的代码,在空框架上添加马自达和斯巴鲁,并告诉我们到底哪里出了问题。
-
该信息不如默认语言环境请求的 minimal reproducible example 有用。
标签: java inheritance multiple-inheritance jcomponent