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

  • 相关阅读:
    Windows 上运行 Zookeeper
    【Kubernetes】K8S的默认调度策略--如何保证POD调度按照提交顺序进行?
    rabbitmq crashdump分析
    java.sql.SQLRecoverableException: IO Error: SO Exception was generated
    常见的数据分析模型
    事实表设计
    PHP系列 | PHP curl报错:417
    工具系列 | Ubuntu18.04安装Openssl-1.1.1
    PHP系列 | PHP中使用gRPC extension 扩展安装
    云原生之容器安全实践
  • 原文地址:https://www.cnblogs.com/alhh/p/5378579.html
Copyright © 2011-2022 走看看