package TEXT;
public class text6
{
public static void main(String[] args) {
//定义变量半径和边长
Object object1 = new Circle4(2);
Object object2 = new Rectangle1(2, 1);
displayObject(object1);
displayObject(object2);
}
public static void displayObject(Object object) { //instanceof判断是否相同并返回bool
if (object instanceof Circle4)
{
System.out.println("The circle area is " + ((Circle4)object).getArea());
System.out.println("The circle diameter is " + ((Circle4)object).getDiameter());
}
else if (object instanceof Rectangle1)
{
System.out.println("The rectangle area is " + ((Rectangle1)object).getArea());
System.out.println("The creat message is :"+((Rectangle1)object).toString());
}
}
public static class GeometricObject1 {
private String color = "white";
private boolean filled;
private java.util.Date DateCreated;
//无参构造
public GeometricObject1()
{
DateCreated = new java.util.Date();
}
//含参构造
public GeometricObject1(String Color, boolean filled)
{
DateCreated = new java.util.Date();
this.color = color;
this.filled = filled;
}
public void setColor(String color) {
this.color = color;
}
public String getColor() {
return color;
}
public boolean isFilled() {
return filled;
}
public void setFilled(boolean filled) {
this.filled = filled;
}
public java.util.Date getDateCreated() {
return DateCreated;
}
public String toString()
{
return "created on " + DateCreated + "ncolor: " + color + " and filled: " + filled;
}
}
//嵌套调用,与this类似
public static class Circle4 extends GeometricObject1 {
private double radius;
public Circle4() {
}
public Circle4(double radius) {
super();
this.radius = radius;
}
public Circle4(double radius, String color, boolean filled) {
super(color, filled);
this.radius = radius;
setColor(color);
setFilled(filled);
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double getArea() {
return radius * radius * Math.PI;
}
//返回直径
public double getDiameter() {
return 2 * radius;
}
//返回周长
public double getPerimeter() {
return 2 * radius * Math.PI;
}
//打印出圆的信息
public void printCircle() {
System.out.println(toString() + "The circle is created " + getDateCreated() +
" and the radius is " + radius);
}
public String toString() {
return "Circle WWWW " + getColor() + super.toString();
}
}
public static class Rectangle1 extends GeometricObject1 {
private double width;
private double height;
public Rectangle1() {
}
public Rectangle1(double width, double height) {
this.width = width;
this.height = height;
}
public Rectangle1(double width, double height, String color,boolean filled) {
this.width = width;
this.height = height;
setColor(color);
setFilled(filled);
}
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 double getArea() {
return width * height;
}
}
}



