package test1; 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 void CircleRound() { System.out.println("圆"); } @Override public double findArea() { return 3.14 * 3.14 * radius; } public boolean equals(Object o) { if(this == o) { return true; } if(o instanceof Circle) { Circle c = (Circle)o; return this.radius == c.radius; } return false; } }
package test1; public abstract class GeometricObject { protected String color; protected double weight; protected 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 abstract double findArea(); }
package test1; public class GeoTest { public static void main(String[] args) { GeoTest T = new GeoTest(); Circle c0 = new Circle("red",1.0,2.3); Circle c1 = new Circle("red",1.0,3.2); T.displayGeometricObject(c0); T.displayGeometricObject(c1); System.out.println(T.equalArea(c0,c1)); } public boolean equalArea(GeometricObject g1,GeometricObject g2) { return g1.findArea() == g2.findArea(); } public void displayGeometricObject(GeometricObject g) { System.out.println(g.findArea()); if(g instanceof Circle) { Circle c = (Circle)g; c.CircleRound(); } if(g instanceof MyRectangle) { MyRectangle r = (MyRectangle)g; r.Rectangle(); } } }
package test1; public class MyRectangle extends GeometricObject { private double width; private double height; public MyRectangle(String color,double height,double weight, double width) { super(color, weight); this.width = width; this.height = height; } public double getWidth() { return width; } public void setWidth(double width) { this.width = width; } public void Rectangle() { System.out.println("三角形"); } @Override public double findArea() { return width * height; } }
文章来源地址https://www.toymoban.com/news/detail-426416.html
文章来源:https://www.toymoban.com/news/detail-426416.html
到了这里,关于JAVA使用抽象类改进面向对象编程测试------JAVA入门基础教程的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!