用类封装手机的基本属性和功能,要求手机既可以使用移动公司的SIM卡也可以使用联通公司SIM卡(可以使用任何公司提供的SIM卡)
public abstract class SIM {
public abstract void setNumber(String n);
public abstract String giveNumber();
public abstract String giveCorpName();
}
public class SIMOfChinaUnicom extends SIM {
String number;
public void setNumber(String n){
number=n;
}
public String giveNumber(){
return number;
}
public String giveCorpName(){
return "中国联通";
}
}
public class SIMOfChinaMobile extends SIM {
String number;
public void setNumber(String n){
number=n;
}
public String giveNumber(){
return number;
}
public String giveCorpName(){
return "中国移动";
}
}
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());
}
}
public class Application {
public static void main(String[] args) {
MobileTelephone telephone=new MobileTelephone();
SIM sim=new SIMOfChinaMobile();
sim.setNumber("13856432");
telephone.useSIM(sim);
telephone.showMess();
sim =new SIMOfChinaUnicom();
sim.setNumber("1309765437");
telephone.useSIM(sim);
telephone.showMess();
}
}
TRANSLATE with x
English
TRANSLATE with
COPY THE URL BELOW
Back
EMBED THE SNIPPET BELOW IN YOUR SITE
Enable collaborative features and customize widget: Bing Webmaster Portal
Back
原创文章,作者:3628473679,如若转载,请注明出处:https://blog.ytso.com/271437.html