练习10——多态性的练习

1、几何类


package test3;

/**
 * 〈一句话功能简述〉<br> 
 * 〈父类几何形状〉
 *
 * @author abu
 * @create 2019/7/15
 * @since 1.0.0
 */
public class GeometricObject {
    protected String color;
    protected double weight;

    public GeometricObject() {
    }

    public GeometricObject(String color, double weight) {
        this.color = color;
        this.weight = weight;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public double getWeight() {
        return weight;
    }

    public void setWeight(double weight) {
        this.weight = weight;
    }

    public double findArea(){
        return 0.0;
    }



}

2、圆类


package test3;

/**
 * 〈一句话功能简述〉<br> 
 * 〈圆〉
 *
 * @author abu
 * @create 2019/7/13
 * @since 1.0.0
 */
public class Circle extends GeometricObject {
    private double radius;

    public Circle(String color, double weight, double radius) {
        super(color, weight);
        this.radius = radius;
    }

//    public Circle(){
//        radius = 1;
//    }

    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }

    public double findArea(){
        return Math.PI * radius * radius;
    }
}

3、矩形类


package test3;

/**
 * 〈一句话功能简述〉<br> 
 * 〈子类矩形〉
 *
 * @author abu
 * @create 2019/7/15
 * @since 1.0.0
 */
public class MyRectangle extends GeometricObject {
    protected double width;
    protected double height;

    public MyRectangle(String color, double weight, double width,
                       double height) {
        super(color, weight);
        this.width = width;
        this.height = height;
    }

    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 findArea(){
        return width * height;
    }


}

4、测试类


package test3;

/**
 * 〈一句话功能简述〉<br> 
 * 〈测试不同子类的面积〉
 *
 * @author abu
 * @create 2019/7/15
 * @since 1.0.0
 */
public class TestEqualArea {
    public static void main(String[] args){
        TestEqualArea t = new TestEqualArea();
        Circle c1 = new Circle("red", 2, 3);
        Circle c2 = new Circle("green", 3,4);

        MyRectangle m1 = new MyRectangle("white",2,4,4);
        t.displayGeometricObject(c1);
        t.displayGeometricObject(m1);

        boolean b = t.equalsArea(c1,m1);
        System.out.println(b);



    }
    public boolean equalsArea(GeometricObject o1, GeometricObject o2){
//        if(o1.findArea() == o2.findArea())
//            return true;
//        else
//            return false;
        return o1.findArea() == o2.findArea();
    }
    public void displayGeometricObject(GeometricObject o){
        System.out.println(o.findArea());
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值