今天主要对多态性的内容进行了复习以及练习,并对object类中常用的equals()、toString()方法的重写进行了学习。
具体的总结通过一个练习代码来展现:
1、GeometricObject类(父类)
public class GeometricObject {
protected String color;
protected double weight;
public GeometricObject() {
super();
color = "white";
weight = 1.0;
}
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() {
double area = 0;
return area;
}
}
2、Circle 类(extends GeometricObject)
package com.athanchang.com;
public class Circle extends GeometricObject{
private double radius;
public Circle() {
super();
//继承的父类的空参构造器会将color、weight自动初始化
radius = 1.0;
}
public Circle(double radius) {
super();
this.radius = 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;
}
@Override
public double findArea() {
return Math.PI*radius*radius;
}
//判断圆的半径是否相等
@Override
public boolean equals(Object obj) {
//先判断:当前对象和形参对象的引用地址是否一样
if(this == obj) {
return false;
}
//判断obj是否是当前Circle的一个实例
if(obj instanceof Circle) {
//强转一下
Circle cir = (Circle)obj;
return this.radius == cir.radius;
}
//不是的话,返回false
return false;
}
@Override
public String toString() {
return "Circle [radius=" + radius + "]";
}
}
3、MyRectangle类( extends GeometricObject)
public class MyRectangle extends GeometricObject{
private double width;
private 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;
}
@Override
public double findArea() {
return width*height;
}
}
4、GeometricTest类(测试类)
public class GeometricTest {
public static void main(String[] args) {
//用的是多态性
GeometricObject geom1 = new Circle("blue", 12, 5);
GeometricObject geom2 = new MyRectangle("purpose", 16, 4, 5);
// 想要调用本类中的方法,要先调用当前类的对象
GeometricTest geomtest = new GeometricTest();
boolean isEquals = geomtest.equalsArea(geom1, geom2);
if (isEquals) {
System.out.println("矩形和园的面积相等");
} else {
System.out.println("矩形和园的面积不相等");
}
System.out.print("圆的");
geomtest.displayGeometricObject(geom1);
System.out.print("矩形的");
geomtest.displayGeometricObject(geom2);
System.out.println("**************************");
//开始练习equals()、toString()重写的部分
Circle c1 = new Circle(9.8);
Circle c2 = new Circle(11.9);
//判断两个圆的颜色是否相等
if(c1.getColor().equals(c2.getColor())) {
/*
* 这里注意一下,本来自己不是特别明白该怎比较两个圆的颜色是否相同
* 即,比较两个对象中的属性(字符串)是否相等
* 引用数据类型,比较内容是否相等
* ----用equals()
*/
System.out.println("两个圆的颜色相同");
}else {
System.out.println("两个圆的颜色不相同");
}
//判断其半径是否相等
//因为对象里的重写的equals()比较的是半径,所以直接比较两个对象即可
boolean isEquals1 = c1.equals(c2);
if(isEquals1) {
System.out.println("两个圆的半径相等");
} else {
System.out.println("两个圆的半径不相等");
}
//输出半径
System.out.println(c1.toString());
}
// 测试两个对象的面积是否相等
//用的是多态性
public boolean equalsArea(GeometricObject geom1, GeometricObject geom2) {
return (geom1.findArea() == geom2.findArea());
}
//用的是多态性
public void displayGeometricObject(GeometricObject geom) {
System.out.println("面积为:" + geom.findArea());
}
}
行百里者,半九十。
加油,坚持住!