testgeometricobject.java
来自「用java实现的两个测试程序!在机子上编绎并且通过!!很实用!」· Java 代码 · 共 86 行
JAVA
86 行
interface Colorable{
public abstract String howToColor();
}
abstract class GeometricObject
{
private String color="white";
private boolean fill;
protected GeometricObject()
{
}
protected GeometricObject(String color,boolean fill)
{this.color=color;
this.fill=fill;
}
public void setColor(String c)
{ this.color=c;
}
public String getColor()
{return color;
}
public boolean isFill()
{return fill;
}
public void setFill(boolean f)
{this.fill=f;
}
}
class Square extends GeometricObject implements Colorable{
private double width;
private double height;
public Square(){
this(1.0,1.0,"white",false);
}
public Square(double width,double height,String color,boolean filled){
super(color, filled);
this.width=width;
this.height=height;
}
public double getWidth(){
return width;
}
public void setWidth(double width){
this.width=width;
}
public double getHeight(){
return height;
}
public void setHeight(double height){
this.height=height;
}
public String howToColor(){
return "color it red";
}
}
class Circle extends GeometricObject{
private double radius;
public Circle(){
this(1.0,"white",false);
}
public Circle(double radius,String color,boolean filled){
super(color,filled);
this.radius=radius;
}
public double getRadius(){
return radius;
}
public void setRadius(double radius){
this.radius=radius;
}
public String howToColor(){
return "color it green";
}
}
public class TestGeometricObject{
public static void main(String[] args){
Object[] objects={new Square(),new Circle()};
for(int i=0;i<objects.length;i++)
showObject(objects[i]);
}
public static void showObject(Object object){
if(object instanceof Colorable)
System.out.println(((Colorable)object).howToColor());
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?