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