【JAVA 課後習題 13.7】+ 介面 + 抽象類
阿新 • • 發佈:2019-02-02
在GeOb類里加個 Colorable 介面字在子類裡實現就好~~定義陣列的時候要注意數組裡的元素要逐個 new 例項,父類是抽象類的話,要用子類實現~~JAVA越來越好用了嘛~~怪不得JAVA比較好找工作
GeOb類~~多加個了藉口
public abstract class GeOb{
private String color = "white";
private boolean filled;
protected java.util.Date da;
protected GeOb(){
da = new java.util.Date();
}
public GeOb(String color,boolean filled){
da = new java.util.Date();
this.color = color;
this.filled = filled;
}
public void setGe(String color,boolean filled){
da = new java.util.Date();
this.color = color;
this.filled = filled;
}
public String getcolor (){
return color;
}
public void setcolor(String color){
this.color = color;
}
public boolean getfillrd(){
return this.filled;
}
public void setfilled(boolean filled){
this.filled = filled;
}
public java.util.Date getda(){
return da;
}
public String toString(){
return "created on " + da + "\ncolor : " + color + " and filled : " + filled;
}
public void howToColor() {
System.out.println("Color all four sides ");
}
interface Colorable{
public void howToColor();
}
public abstract double getArea();
public abstract double getPerimeter();
}
Square 類~也即子類
class Square extends GeOb{
public double getArea() {
return 0;
}
public double getPerimeter() {
return 0;
}
public void howToColor() {
System.out.println("Color all four sides ");
}
}
Text1 實現程式碼 :
public class Text1 {
public static void main(String[] args) {
GeOb[] a = new GeOb[10]; // 定義陣列物件
for(int i = 0 ; i < 5 ; i++)
a[i] = new Square(); // 把數組裡的元素逐一 new 成例項
a[0].setGe("write",true);
a[1].setGe("write",true);
a[2].setGe("write",false);
a[3].setGe("write",false);
a[4].setGe("write",true);
for(int i = 0 ; i < 5 ; i++){
System.out.println(a[i].getfillrd());
if(a[i].getfillrd())
a[i].howToColor();
}
}
}