1 Creating an interface
Package Lianxi; Public Interface ICPU { public boolean neicun (); Memory Interface }
2 Create a class to connect to this interface
Package Lianxi; Public class Implements icpu { @Override publicboolean Neicun () { System.out.println (" CPU memory is too small "); return true ; }}
3 Creating a parent class
Public classComputer {PrivateICPU CPU; Publicicpu Getdiannao () {returnCPU; } Public voidSetdiannao (Icpu CPU) { This. CPU =CPU; } DoubleRongliang; BooleanCP () {Booleanrtn=true; if(rongliang>50) {System.out.println ("Installed a" +rongliang+ "CPU"); RTN=false; }Else if(cpu==NULL) {System.out.println ("CPU not Installed"); RTN=false; }Else{RTN= This. Cpu.neicun (); }return true; } }
4 Creating subclass Calls
Package Lianxi; Public class Ceshi { publicstaticvoid main (string[] args) { // TODO Auto-generated method stubs Computer cop=New computer (); Cop.rongliang=34;
System.out.println ("When the capacity is" +cop.rongliang+ "); Cop.setdiannao (new Cpuceshi ()); Cop. CP ();} }
Package Lianxi; Public class Ceshi { publicstaticvoid main (string[] args) { // TODO Auto-generated method stubs Computer cop=New computer (); Cop.rongliang=70; System.out.println ("When the capacity is" +cop.rongliang+ "); Cop.setdiannao (new Cpuceshi ()); Cop. CP ();} }
CPU Interface Exercise (only this program proves that a classmate is really poisonous!) Toxic!!! )