Java Encapsulation Sample Code

Source: Internet
Author: User

 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.