PackageCom.imooc; Public classTelphone {Private floatScreen ; Private floatCPU; Private floatMem; Public floatGetscreen () {returnScreen ; } Public voidSetscreen (floatNewscreen) { screen=Newscreen; } PublicTelphone () {System.out.println ("The parameterless construction method was executed!" "); } PublicTelphone (floatNewscreen,floatNEWCPU,floatNewmem) { //construction method ensures that the value of the variable assigned to the member is a reasonable value if(newscreen<3.5f) {System.out.println ("There is a problem with the parameter you entered, automatically assigned a value of 3.5"); screen= 3.5f; }Else{ screen=Newscreen; } CPU=newcpu; Mem=Newmem; System.out.println ("The constructive method of the argument has been executed!" "); }}
PackageCom.imooc; Public classInitialtelphone { Public Static voidMain (string[] args) {//TODO auto-generated Method Stub//You can create an object by using an argument-free construction methodTelphone phone1 =NewTelphone (); //An object can be created by means of an argument construction method, and an initial value is assigned to an object's instance variable .Telphone Phone2 =NewTelphone (1.5f, 1.4f, 2.2f); //Modifying the value of a variable by setter methodPhone1.setscreen (5.0f); //get the value of a variable by getter methodSystem.out.println ("screen:" +Phone1.getscreen ()); }}
Java Wrapper sample code