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

  • 相关阅读:
    2.截取部分字符串中的内容(可做文件上传时的文件重命名)
    1.git fetch的使用
    2.java.lang.IllegalStateException: Optional long parameter 'id' is present but cannot be translated into a null value due to being declared as a primitive type. Consider declaring it ......Springmvc报错
    彩色动态球
    小球落下的动画
    form表单
    repeating-radial-gradient示例
    background示例一
    css中关于以background开的的介绍
    块元素居中的范例
  • 原文地址:https://www.cnblogs.com/alhh/p/5378579.html
Copyright © 2011-2022 走看看