抽象类实验:SIM卡抽象

抽象SIM:

package sim_package;

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

SIM子类:

package sim_package;

public class SIMOFChinaMobile extends SIM{
	String phonenumber;
	public SIMOFChinaMobile(){
		phonenumber="";
	}
	public SIMOFChinaMobile(String phonenumber){
		this.phonenumber=phonenumber;
	}
	 public String giveNumber(){
		 return phonenumber;
	 }
	 public String giveCorpName(){
		 return "中国移动";
	 }
	 public void setNumber(String phonenumber){
		 this.phonenumber=phonenumber;
	 }
}

SIM子类:

package sim_package;

public class SIMOFChinaUnicom extends SIM {
	String phonenumber;
	public SIMOFChinaUnicom(){
		phonenumber="";
	}
	public SIMOFChinaUnicom(String phonenumber){
		this.phonenumber=phonenumber;
	}
	 public String giveNumber(){
		 return phonenumber;
	 }
	 public String giveCorpName(){
		 return "中国联通";
	 }
	 public void setNumber(String phonenumber){
		 this.phonenumber=phonenumber;
	 }
}

手机类:

package Mobile_telephone;
import sim_package.*;
public class Mobiletelephone {
	SIM card;
	public Mobiletelephone(){
		
	}
	public Mobiletelephone(SIM card){
		this.card=card;
	}
	public void useSIM(SIM card){
		System.out.println("运营商:"+card.giveCorpName());
		System.out.println("手机号:"+card.giveNumber());
	}
}

主程序:

import Mobile_telephone.*;
import sim_package.*;
public class Application {

	public static void main(String[] args) {
		SIM sim=new SIMOFChinaUnicom();
		sim.setNumber("13887656432");
		Mobiletelephone phone=new Mobiletelephone();
		phone.useSIM(sim);
		sim=new SIMOFChinaMobile();
		sim.setNumber("13097656437");
		phone.useSIM(sim);
	}

}


posted on 2017-11-18 09:29  MACHINE_001  阅读(225)  评论(0编辑  收藏  举报

导航