通过重写equals方法实现逻辑判断,代码如下: package com . tiger . practice ; /** * 写一个 "立方体" 类, 长, 宽, 高, 重写 equals 方法,用于判断两个 "立方体" 体积是否相等。 * @author tiger * */ pub
通过重写equals方法实现逻辑判断,代码如下:
/**
* 写一个 "立方体" 类, 长, 宽, 高, 重写 equals 方法,用于判断两个 "立方体" 体积是否相等。
* @author tiger
*
*/
public class Test03 {
public static void main(String[] args) {
//立方体 1
Cube cube = new Cube("立方体1",30, 302, 100);
System.out.println(cube);
System.out.println();
//立方体 2
Cube cube2 = new Cube("立方体2",302, 30, 100);
System.out.println(cube2);
System.out.println();
System.out.println(cube.equals(cube2));
}
}
/**
*
* 立方体类
* @author tiger
*
*/
class Cube{
private String name;
private int wide;
private int height;
private int lon;
public boolean equals(Cube cube){
if (this.wide * this.height * this.lon == cube.wide * cube.height * cube.lon) {
System.out.print("两立方体体积相等 : ");
return true;
}
System.out.print("两立方体不相等 : ");
return false;
}
@Override
public String toString() {
return name+" \t\t\nwide = " + wide + " ,height = " + height + " ,lon = " + lon ;
}
public Cube(String name,int wide, int height, int lon) {
super();
this.name = name;
this.wide = wide;
this.height = height;
this.lon = lon;
}
public int getWide() {
return wide;
}
public void setWide(int wide) {
this.wide = wide;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getLon() {
return lon;
}
public void setLon(int lon) {
this.lon = lon;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}