栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

2021-10-12

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

2021-10-12

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;
        }


    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/322924.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号