51,400
社区成员




import java.util.Date;
class Geometric {
private String color = "white";//几何图形边框颜色
private boolean filled;//内部是否填充颜色
private Date dateCreated;//该几何图形的创建时间
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public boolean isFilled() {
return filled;
}
public void setFilled(boolean filled) {
this.filled = filled;
}
public Date getDateCreated() {
return dateCreated;
}
public void setDateCreated(Date dateCreated) {
this.dateCreated = dateCreated;
}
//无参构造方法
public Geometric(){
this.dateCreated = new Date();
}
//带参数的构造方法
public Geometric(String color,boolean filled){
this.color = color;
this.filled = filled;
this.dateCreated = new Date();
}
//重写toString
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("color = ").append(color);
sb.append(", filled=").append(filled);
sb.append(", dateCreated=").append(dateCreated); //这里可以根据需要格式化日期
return sb.toString();
}
}
class Circle extends Geometric {
private double radius;//半径
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
//无参构造方法
public Circle(){
this.radius = 1;
}
//带参数的构造方法
public Circle(double radius,String color,boolean filled){
super(color,filled);//调用父类的构造方法
this.radius = radius;
}
//求周长
public double getPerimeter(){
return 2*Math.PI*radius;//圆的周长
}
//求面积
public double getArea(){
return Math.PI*radius*radius;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder(super.toString());//父类的toString
sb.append(", radius=").append(radius);
return sb.toString();
}
//下面进行测试
public static void main(String[] args) {
Circle circle = new Circle(2.0,"red",true);
System.out.println(circle);
}
}