软件NetBeans IDE 7.0.1,需要单独写主类。
编写一个仿真购买手机与手机卡的例子。
封装手机卡接口SIMable,它表示是手机卡,可以应用为移动卡也可以是联通卡,声明个方法以适应通用手机: vod setNumer(string n); String givNumereo:tring gveCorpName)…
(2)封装手机类Molcelephne既可以是单卡手机,也可以是双卡双待手机,也可以没有手机卡,取决于SIM卡, 因此,需要声明3个SIMable类型的属性,以对应单卡、双卡、无卡,编写3个构造方法。后期可能还会换卡,因此需要编写2个方法以适应修改卡: void useSIM(SIMablecard),void useSIM(SIMable cardI, SIMable card2),编写查看手机卡信息方法void showMess().
(3)封装移动手机卡SIMOfChinaMobile,需要实现SIMable接口,同时要封装手机号Stringnumber.
(4)封装联通手机卡SIMOChinaUnicom,需要实现SIMable接口,同时要封装手机号String
numberf
(5)端写仿真程序执行入口Test. 创建手机对象和移动卡对象和联通卡对象,查看信息。
主类
import com.cn.jiekou.SIMable;
import com.cn.lei.MobileTelephone;
import com.cn.lei.SIMOFChinaMoblie;
import com.cn.lei.SIMOFChinaUnicom;
public static void main(String[] args) {
// TODO code application logic here
SIMable Moblie=new SIMOFChinaMoblie("158555555555555");
SIMable Unicom=new SIMOFChinaUnicom("134355353336363");
MobileTelephone phone=new MobileTelephone(Moblie,Unicom);
phone.showMess();
}
SIMable接口:
public interface SIMable {
void setNumber(String n);
String giveNumber();
String giveCorpName();
}
MobileTelephone类:
import com.cn.jiekou.SIMable;
public class MobileTelephone{
private SIMable card;
private SIMable card1;
private SIMable card2;
public MobileTelephone(SIMable card) {
this.card = card;
}
public MobileTelephone(SIMable card1, SIMable card2) {
this.card1 = card1;
this.card2 = card2;
}
public MobileTelephone() {
}
public void useSIM(SIMable card){
this.card1=card;
}
public void SIM(SIMable card1,SIMable card2){
this.card1=card1;
this.card2=card2;
}
public void showMess(){
if(card1==null){
System.out.println("此手机无卡");
}
if((card1!=null&&card2==null)||(card1==null&&card2!=null)){
System.out.println("手机为单卡手机");
System.out.println(card1.giveCorpName());
System.out.println("手机卡为:"+card1.giveNumber());
}
if(card1!=null&&card2!=null){
System.out.println("手机为双卡手机");
System.out.println("手机卡1为:"+card1.giveNumber());
System.out.println(card1.giveCorpName());
System.out.println("手机卡2为:"+card2.giveNumber());
System.out.println(card2.giveCorpName());
}
}
}
SIMOFChinaMoblie类:
import com.cn.jiekou.SIMable;
public class SIMOFChinaMoblie implements SIMable{
private String number;
public SIMOFChinaMoblie() {
}
public SIMOFChinaMoblie(String number) {
this.number = number;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String giveNumber(){
return number;
}
@Override
public String giveCorpName() {
return "ChinaMobile";
}
}
SIMOFChinaUnicom类:
import com.cn.jiekou.SIMable;
lass SIMOFChinaUnicom implements SIMable{
private String number;
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public SIMOFChinaUnicom(String number) {
this.number = number;
}
public String giveNumber(){
return number;
}
@Override
public String giveCorpName() {
return "ChinaUnion";
}
}