1. Defining the interface
Package Com.lianxi; Public Interface ICPU { public string Pinpai (string a) ; brand public void yunxing (); Run public int luosigeshu (int d); number of screws }
2. Defining computers
PackageCom.lianxi; Public classJSJ {PrivateICPU CPU;//naming Publicicpu getcpu () {returnCPU; } //Getter and Setter Public voidSetCpu (Icpu CPU) { This. CPU =CPU; } PrivateString Neicun;//Defining Properties PrivateString Yingpan;//Defining Properties PublicString Getneicun () {returnNeicun; } Public voidSetneicun (String neicun) { This. Neicun =Neicun; } PublicString Getyingpan () {returnYingpan;//Getter and Setter } Public voidSetyingpan (String yingpan) { This. Yingpan =Yingpan; } voidKaiji ()//Defining Methods{intelcpu inte=Newintelcpu (); Inte.yunxing (); System.out.println ("Powering on"); } voidGuanji ()//Defining Methods{System.out.println ("Shutting down"); } intJisuan (intAintb//Defining Methods { return(A +b); } }
3. Define CPU
PackageCom.lianxi; Public classIntelcpuImplementsicpu {@Override PublicString Pinpai (String a)//Brand Realization { returnA; } @Override Public voidYunxing ()//implementation Run{System.out.println ("CPU starts running"); } @Override Public intLuosigeshu (intD//number of screws implemented { //TODO Auto-generated method stubs returnD; }}
4. Instantiate and run
PackageCom.lianxi; Public classTESTJSJ { Public Static voidMain (string[] args) {JSJ JSJ=NewJSJ (); Jsj.setyingpan ("500G"); Jsj.setneicun ("Acura Memory"); INTELCPU CPU=Newintelcpu (); intA =cpu.luosigeshu (5); String b= Cpu.pinpai ("Intel card"); Jsj.kaiji (); intc = Jsj.jisuan (5, 6); System.out.println ("Computer hard drive =" +Jsj.getyingpan ()); System.out.println ("Computer memory is" +Jsj.getneicun ()); System.out.println ("Cup on a total of" + A + "screws"); System.out.println ("The brand of the CPU is" +b); System.out.println ("The result of calculation 5 + 6 is" +c); Jsj.guanji (); /*The result of the operation is as follows: The CPU starts to run the computer hard disk = 500G computer memory is the best memory bar on a total of 5 screws CPU brand is the Intel card The result of calculation 5 + 6 is that 11 is shutting down*/ }}
January 21-Practice Interface PC CPU