zoukankan      html  css  js  c++  java
  • 继承,多态,重写方法运用

    //将继承,多态,重写的性质运用一下

    package duotai;

    public class GeometricObject {
    protected String color;
    protected double weight;
    protected GeometricObject(String color, double weight) {
    super();
    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;
    }
    }

    package duotai;

    public class Circle extends GeometricObject {
    private double radius;

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

    public double getRadius() {
    return radius;
    }

    public void setRadius(double radius) {
    this.radius = radius;
    }
    public double findArea(){//重写
    return Math.PI*radius*radius;
    }
    }

    package duotai;

    public class MyRectangle extends GeometricObject{//继承
    private double width;
    private double height;
    public MyRectangle(double width,double height,String color,double weight){
    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;
    }
    }

    package duotai;

    public class TestGeometric {
    public static void main(String[] args) {
    TestGeometric t=new TestGeometric();
    Circle c1=new Circle("red", 3.2, 1.2);
    Circle c2=new Circle("black", 2.2, 6.2);
    MyRectangle m=new MyRectangle(2.3,1.2,"green",2.0);
    t.displayGeometricObject(m);
    boolean b=t.equalsArea(c1,c2);
    System.out.println(b);
    }
    public boolean equalsArea(GeometricObject o1,GeometricObject o2){//参数类型GeometricObject
    // if(o1.findArea()==o2.findArea())
    // return true;
    // else
    // return false;
    return o1.findArea()==o2.findArea();
    }
    public void displayGeometricObject(GeometricObject o){//GeometricObject o=("red", 3.2, 1.2)这就是多态
    System.out.println(o.findArea());
    }
    }

  • 相关阅读:
    IE6/IE7下:inline-block不兼容的问题
    ie6、ie7下overflow失效
    display:inline、block、inline-block三者之间的区别
    分割线用CSS样式做出来的效果
    jQuery实现鼠标点击div外的地方div隐藏消失的效果
    IE6下面的浮动问题
    [zabbix]zabbix分区表操作步骤
    [转载]系统管理:update-alternatives
    zabbix调优PPT
    mysql修改密码
  • 原文地址:https://www.cnblogs.com/alhh/p/5378579.html
Copyright © 2011-2022 走看看