12 应用举例
作者:互联网
用类封装手机的基本属性和功能,要求手机既可以使用移动公司的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 {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
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();
}
}
标签:12,String,void,number,举例,card,应用,public,SIM 来源: https://www.cnblogs.com/xianbing2022/p/16441496.html