AbstractModel
public abstract class AbstractModel {
private String name;
private double waistline;
private double hipline;
private double bust;
public void walkThrough() {
System.out.println(String.format("模特%s走台操作", name));
}
@Override
public int hashCode() {
return Objects.hash(name, waistline, hipline, bust);
}
@Override
public String toString() {
return "[" +
"name='" + name + '\'' +
", bust=" + bust +
", waistline=" + waistline +
']';
}
public abstract void changeClothes();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getWaistline() {
return waistline;
}
public void setWaistline(double waistline) {
this.waistline = waistline;
}
public double getHipline() {
return hipline;
}
public void setHipline(double hipline) {
this.hipline = hipline;
}
public double getBust() {
return bust;
}
public void setBust(double bust) {
this.bust = bust;
}
}
TModel
public class TModel extends AbstractModel{
public TModel(String name,double waistline,double hipline,double bust) {
this.setName(name);
this.setWaistline(waistline);
this.setHipline(hipline);
this.setBust(bust);
}
@Override
public void changeClothes() {
System.out.println(String.format("T台模特%s换衣操作",this.getName()));
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
AbstractModel that = (AbstractModel) o;
if (o instanceof TModel) {
//判断两个T台模特对象,是否相等依据:胸围+臀围+腰围
return Double.compare(that.getWaistline(), this.getWaistline()) == 0 && Double.compare(that.getHipline(), this.getHipline()) == 0 && Double.compare(that.getBust(), this.getBust()) == 0;
} else if (o instanceof AdModel) {
//判断两个广告模特对象,是否相等依据:姓名+胸围
return Double.compare(that.getWaistline(), this.getWaistline()) == 0 && that.getName().equalsIgnoreCase(this.getName());
}
//判断两个平面模特对象,是否相等依据:胸围+腰围
return Double.compare(that.getWaistline(), this.getWaistline()) == 0 && Double.compare(that.getBust(), this.getBust()) == 0;
}
}
PModel
public class PModel extends AbstractModel {
public PModel(String name, double waistline, double hipline, double bust) {
this.setName(name);
this.setWaistline(waistline);
this.setHipline(hipline);
this.setBust(bust);
}
@Override
public void changeClothes() {
System.out.println(String.format("平面模特%s换衣操作", this.getName()));
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
AbstractModel that = (AbstractModel) o;
if (o instanceof TModel) {
//判断两个T台模特对象,是否相等依据:胸围+臀围+腰围
return Double.compare(that.getWaistline(), this.getWaistline()) == 0 && Double.compare(that.getHipline(), this.getHipline()) == 0 && Double.compare(that.getBust(), this.getBust()) == 0;
} else if (o instanceof AdModel) {
//判断两个广告模特对象,是否相等依据:姓名+胸围
return Double.compare(that.getWaistline(), this.getWaistline()) == 0 && that.getName().equalsIgnoreCase(this.getName());
}
//判断两个平面模特对象,是否相等依据:胸围+腰围
return Double.compare(that.getWaistline(), this.getWaistline()) == 0 && Double.compare(that.getBust(), this.getBust()) == 0;
}
}
AdModel
public class AdModel extends AbstractModel{
public AdModel(String name,double waistline,double hipline,double bust) {
this.setName(name);
this.setWaistline(waistline);
this.setHipline(hipline);
this.setBust(bust);
}
@Override
public void changeClothes() {
System.out.println(String.format("广告模特%s换衣操作",this.getName()));
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
AbstractModel that = (AbstractModel) o;
if (o instanceof TModel) {
//判断两个T台模特对象,是否相等依据:胸围+臀围+腰围
return Double.compare(that.getWaistline(), this.getWaistline()) == 0 && Double.compare(that.getHipline(), this.getHipline()) == 0 && Double.compare(that.getBust(), this.getBust()) == 0;
} else if (o instanceof AdModel) {
//判断两个广告模特对象,是否相等依据:姓名+胸围
return Double.compare(that.getWaistline(), this.getWaistline()) == 0 && that.getName().equalsIgnoreCase(this.getName());
}
//判断两个平面模特对象,是否相等依据:胸围+腰围
return Double.compare(that.getWaistline(), this.getWaistline()) == 0 && Double.compare(that.getBust(), this.getBust()) == 0;
}
}
测试类
public class test {
public static void main(String[] args) {
/*
1. 分别给每个子类创建2个对象,每个对象调用方法测试:1. 走台方法walkThrough 2.换衣服抽象方法changeClothes
*/
//实例化T台模特
AbstractModel TModel1 = new TModel("tmodel1", 1, 1, 1);
AbstractModel TModel2 = new TModel("tmodel2", 1, 3, 1);
//T台模特行为
TModel1.walkThrough();
TModel1.changeClothes();
TModel2.walkThrough();
TModel2.changeClothes();
//实例化广告模特
AbstractModel AdModel1 = new AdModel("admodel1", 2, 1, 1);
AbstractModel AdModel2 = new AdModel("admodel2", 2, 3, 1);
//广告模特行为
AdModel1.walkThrough();
AdModel1.changeClothes();
AdModel2.walkThrough();
AdModel2.changeClothes();
//实例化平面模特
AbstractModel PModel1 = new PModel("pmodel1", 3, 1, 1);
AbstractModel PModel2 = new PModel("pmodel2", 3, 3, 1);
//平面模特行为
PModel1.walkThrough();
PModel1.changeClothes();
PModel2.walkThrough();
PModel2.changeClothes();
/*
2. 希望打印对象的时候,实现效果如下:[姓名,胸围,腰围] toString方法
*/
System.out.println(PModel1);
/*
3. 判断两个T台模特对象,是否相等依据:胸围+臀围+腰围 equals方法
*/
System.out.println(PModel1.equals(PModel2));
}
}
运行结果
模特tmodel1走台操作
T台模特tmodel1换衣操作
模特tmodel2走台操作
T台模特tmodel2换衣操作
模特admodel1走台操作
广告模特admodel1换衣操作
模特admodel2走台操作
广告模特admodel2换衣操作
模特pmodel1走台操作
平面模特pmodel1换衣操作
模特pmodel2走台操作
平面模特pmodel2换衣操作
[name='pmodel1', bust=1.0, waistline=3.0]
true
Process finished with exit code 0
这是大概的代码,如有错误请谅解