java 题目 麻烦高手进来解答一下

2024-12-31 21:41:31
推荐回答(1个)
回答1:

Colorable接口
public interface Colorable {
void howToColor();
}
GeometricObject类
public class GeometricObject{
}
Square类
public class Square extends GeometricObject implements Colorable{
public void howToColor() {
System.out.println("Colorall four sides");
}
}
场景类Main
public class Main {
public static void main(String args[]){
GeometricObject[] gs = {new GeometricObject(),new GeometricObject(),new Square(),new GeometricObject(),new GeometricObject(),};
for (GeometricObject geometricObject : gs) {
if(geometricObject instanceof Square){
Square s = (Square)geometricObject;
s.howToColor();
}
}
}
}
没有这个UML画图工具,我就不贴上图了,相信你根据这个代码之间的关系能画出来