java 面向抽象编程的思想

java 面向抽象编程的思想

java 面向抽象编程的思想

SIM.java

public abstract class SIM {
public abstract void setNumber(String n);
public abstract String giveNumber();
public abstract String giveCorpName();
}

MobileTelephone.java

public class MobileTelephone {
SIM card;
public void useSIM(SIM card) {
this.card=card;
}
public void showMess() {
System.out.println("使用的卡是:"+card.giveCorpName()+"提供的");
System.out.println("手机号码是:"+card.giveNumber());
}
}

SIMOfChinaMobile.java

public class SIMOfChinaMobile extends SIM {
String number;
public void setNumber(String n) {
number = n;
}
public String giveNumber() {
return number;
}
public String giveCorpName() {
return "中国移动";
}
}

SIMOfChinaUnicom.java

public class SIMOfChinaUnicom extends SIM {
String number;
public void setNumber(String n) {
number = n;
}
public String giveNumber() {
return number;
}
public String giveCorpName() {
return "中国联通";
}
}

Application.java

public class Application {
public static void main(String args[]) {
MobileTelephone telephone = new MobileTelephone ();
SIM sim=new SIMOfChinaMobile();
sim.setNumber("13887656432");
telephone.useSIM(sim);
telephone.showMess();
sim=new SIMOfChinaUnicom();
sim.setNumber("13097656437");
telephone.useSIM(sim);
telephone.showMess();
}
}
上一篇:iOS 中client和server的 Web Service 网络通信 (2)


下一篇:WebKit爬虫