shapes

接口 shape

package shape;

public abstract interface shape {

public abstract void Draw();
public abstract void getString();
public static void main(String[] args) {

}

}

 实体类矩形

package shape;

public class Rect implements shape{
private int length = 0;
private int wideth = 0;
public Rect(int length,int wideth){
this.length = length;
this.wideth = wideth;
}
public void Draw(){
System.out.println("Draw Rect");
}

public void getString() {
System.out.println("Rect [length=" + length + ", wideth=" + wideth + "]");
}
public static void main(String[] args) {
// TODO Auto-generated method stub

}

}

实体类 圆

package shape;

public class Circle implements shape{
private int Radius = 0;
public Circle(int Radius){
this.Radius = Radius;
}
public void Draw(){
System.out.println("Draw Circle");
}

public void getString() {
System.out.println("Circle [Radius=" + Radius + "]");
}
public static void main(String[] args) {

}

}

多态   操作

package shape;

public class Board {

public shape[] S = { new Circle(1), new Circle(2)
, new Circle(3), new Rect(1,2)
, new Rect(2,3), new Rect(3,4) };

public void Refresh(shape s) {
s.Draw();
s.getString();
}

public static void main(String[] args) {
Board B = new Board();
for (int i = 0; i < 6; i++) {
B.Refresh(B.S[i]);
}
}

}

原文地址:https://www.cnblogs.com/the-wang/p/6538368.html