课堂练习4:
编写代码模拟手机与SIM卡的组合关系。
要求:
SIM卡类负责创建SIM卡;
Phone类负责创建手机;
手机可以组合一个SIM卡;
手机可以更换其中的SIM卡。
package PersonalComputer;
public class PhoneDemo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SIM sim1=new SIM();
SIM sim2=new SIM();
Phone phone =new Phone();
sim1.setSIMinfo("中国移动", "1388888888");
sim2.setSIMinfo("中国联通", "1315555555");
phone.setPhoneSIM(sim1);
System.out.println("此手机的SIM卡1信息为:"+phone.getPhoneSIM().gettSIMinfo());
System.out.println(phone.call("15066666666"));
phone.setPhoneSIM(sim2);
System.out.println("此手机的SIM卡2信息为:"+phone.getPhoneSIM().gettSIMinfo());
System.out.println(phone.call("17822222222"));
}
}
class SIM{
private String opertor;
private String number;
public void setSIMinfo(String opertor,String number){
this.opertor=opertor;
this.number=number;
}
public String gettSIMinfo(){
return "SIM卡的运营商是:"+opertor+",卡号是:"+number+"。";
}
}
class Phone{
private SIM phoneSIM;
public void setPhoneSIM(SIM phoneSIM){
this.phoneSIM=phoneSIM;
}
public SIM getPhoneSIM(){
return phoneSIM;
}
public String call(String callnumber){
return "正在打电话给:"+callnumber;
}
}
课堂练习4
最新推荐文章于 2024-05-10 12:19:25 发布