Creating a Java class and instantiating an in-depth understanding

Source: Internet
Author: User

1  PackageCom.sanguosha.java;2 3 ImportJava.util.Scanner;//Importing Packages4 5  Public classTestperson {6      Public Static voidMain (string[] args) {7         //instantiation of the person class8Person p1=NewPerson ();9 p1.eat ();Ten p1.sleep (); OneP1.info ();//call the info () method to view the value of a person's property AP1.age=21; -P1.name= "Tutu"; -p1.sex=true; the p1.info (); -          -P1.setname ("Hu Handsome");//Change Name - p1.info (); +          -Person P2=P1;//equivalent to assigning P1 's first address to P2, two objects sharing one memory space +         //Proof: ASystem.out.println ("The first address of the output P1 is" +p1); atSystem.out.println ("The first address of the output P2 is" +p2); -P2=NewPerson ();//P2 re-new out of a memory space, and P1 separate -System.out.println ("Re-output P2 The first address is" +p2); -         //It is also now possible to understand the instantiation of the scanner class that obtains a numeric value from the keyboard -Scanner n=NewScanner (system.in);//the scanner class was not created by ourselves. -         //so to import the Scanner class of the package import Java.util.Scanner;//Importing Packages in         int_int=n.nextint ();//call method to get the numeric value of type int for keyboard input -     } to } + classperson{ -     //Setting Properties the String name; *     intAge ; $     BooleanSex//suppose true is male; false is femalePanax Notoginseng     //Setup Method -      Public voideat () { theSYSTEM.OUT.PRINTLN ("People eat"); +     } A      Public voidsleep () { theSystem.out.println ("People Sleep"); +     } -      PublicString GetName () {//get the person's name $         returnname; $     } -      Public voidSetName (String N) {//Change person's name -Name=N; the     } -      Public voidInfo () {//set a method specifically for exporting a person's name, gender, ageWuyiSYSTEM.OUT.PRINTLN ("Person's name is:" +name+ "Sex Is" +sex+ "age is" +Age ); the     } -}

Operation Result:

Creating a Java class and instantiating an in-depth understanding

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.