自己老是给自己找麻烦~~很快写好代码后~却发了很长时间找自己的Bug~
好想找个人能和自己一起敲JAVA~一起交流~
GeOb类代码~(加了两个构造方法)
public abstract class GeOb{
private String color = "white";
private boolean filled;
protected java.util.Date da;
int a,b,r;
public void setBian(int a,int b){
this.a = a;
this.b = b;
}
public void setBan(int r){
this.r = r;
}
protected GeOb(){
da = new java.util.Date();
}
protected 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();
}
protected abstract double getArea();
protected abstract double getPerimeter();
}
JuXi子类代码~
public class JuXi extends GeOb{
protected double getArea() {
return this.a * this.b;
}
protected double getPerimeter() {
return 2 * a * b;
}
}
Yuan子类代码~
public class Yuan extends GeOb{
protected double getArea(){
return Math.PI * this.r * this.r;
}
protected double getPerimeter() {
return 2 * Math.PI * this.r * 1.0;
}
}
Text实现代码~~
public class Text2 {
public static void main(String[] args) {
GeOb[] a = new GeOb[4];
a[0] = new JuXi(); // 数组里的元素要逐个实例化
a[0].setBian(1, 2);
a[1] = new JuXi();
a[1].setBian(3, 5);
a[2] = new Yuan();
a[2].setBan(2);
a[3] = new Yuan();
a[3].setBan(7);
System.out.println(sumArea(a));
}
public static double sumArea(GeOb[] a){ // 题目要求的构造方法
double sum = 0.0;
for(int i = 0 ; i < a.length ; i++)
sum += a[i].getArea();
return sum;
}
}