看完的理解是跟名稱一樣,可以先預先建立一個抽象樣版
這樣建立物件後,就可以從樣版執行預設的程式碼
public abstract class Member {
private String name;
private BigDecimal money;
abstract String getCompany();
abstract boolean isGold();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public BigDecimal getMoney() {
return money;
}
public void setMoney(BigDecimal money) {
this.money = money;
}
}
public abstract class Member {
private String name;
private BigDecimal money;
abstract String getCompany();
abstract boolean isGold();
public String registerStart() {
StringBuilder text;
text = new StringBuilder();
text.append("商店名稱:");
text.append(getCompany());
text.append(" / ");
text.append("會員姓名:");
text.append(getName());
text.append("\n");
text.append("消費金額:");
text.append(getMoney());
text.append(" / ");
text.append("是金會員:");
text.append(isGold());
text.append("\n");
return text.toString();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public BigDecimal getMoney() {
return money;
}
public void setMoney(BigDecimal money) {
this.money = money;
}
}
public class CarrefourMember extends Member {
@Override
boolean isGold() {
boolean isGold;
isGold = getMoney().compareTo(BigDecimal.valueOf(1000)) > 0;
return isGold;
}
@Override
String getCompany() {
return "家樂福";
}
}
public class FamilyMarkMember extends Member {
@Override
boolean isGold() {
boolean isGold;
isGold = getMoney().compareTo(BigDecimal.ONE) > 0;
return isGold;
}
@Override
String getCompany() {
return "全家";
}
}
public class JavaTest {
@Test
public void show() {
StringBuilder text;
text = new StringBuilder();
text.append("\n");
Member carrefourMember1 = new CarrefourMember();
carrefourMember1.setName("Andy");
carrefourMember1.setMoney(BigDecimal.ONE);
text.append(carrefourMember1.registerStart());
text.append("\n");
Member carrefourMember2 = new CarrefourMember();
carrefourMember2.setName("Jack");
carrefourMember2.setMoney(BigDecimal.valueOf(2000));
text.append(carrefourMember2.registerStart());
text.append("\n");
Member familyMarkMember1 = new FamilyMarkMember();
familyMarkMember1.setName("Mark");
familyMarkMember1.setMoney(BigDecimal.ZERO);
text.append(familyMarkMember1.registerStart());
text.append("\n");
Member familyMarkMember2 = new FamilyMarkMember();
familyMarkMember2.setName("Rich");
familyMarkMember2.setMoney(BigDecimal.TEN);
text.append(familyMarkMember2.registerStart());
text.append("\n");
assertEquals("測試", text.toString());
}
}
商店名稱:家樂福 / 會員姓名:Andy
消費金額:1 / 是金會員:false
商店名稱:家樂福 / 會員姓名:Jack
消費金額:2000 / 是金會員:true
商店名稱:全家 / 會員姓名:Mark
消費金額:0 / 是金會員:false
商店名稱:全家 / 會員姓名:Rich
消費金額:10 / 是金會員:true