Java Learning Note 14, this usage subsection in Java

Source: Internet
Author: User

When a variable is defined with a class name, it is defined as a reference, and the property and method inside the class can be accessed by this reference outside.

Are there enough and should there be a reference to access their properties and methods, Na?

Oh, Java provides a good thing, is the this object, it can be in the class to reference the properties and methods of the class. Let's start with a simple example:

1  Public classThisdemo {2String name= "Mick";3      Public voidprint (String name) {4System.out.println ("attribute Name= in class" + This. Name);5System.out.println ("property of local parameter =" +name);6     }   7      Public Static voidMain (string[] args) {8Thisdemo tt=NewThisdemo ();9Tt.print ("Orson");Ten     } One}

referring to the return class itself, this keyword is used to return itself to this object and then implement multiple operations within a single statement.

1  Public classThisdemo {2     intNumber ;3 Thisdemo Increment () {4number++;5          return  This;6     }  7   Private voidprint () {8System.out.println ("number=" +Number );9     }Ten      Public Static voidMain (string[] args) { OneThisdemo tt=NewThisdemo (); A tt.increment (). Increment (). Increment (). print (); -     } -}

Two constructors are defined in a class, and another constructor is called through this reference in a constructor, which should be implemented.

Such implementation mechanism in the actual application development when there will be what kind of use? Put down the code:

1  Public classThisdemo {2 String name;3     intAge ;4      PublicThisdemo () {5          This. age=21;6    }     7      PublicThisdemo (String name,intAge ) {8          This();9          This. Name= "Mick";Ten     }      One   Private voidprint () { ASystem.out.println ("Final name =" + This. Name); -System.out.println ("final age =" + This. age); -     } the      Public Static voidMain (string[] args) { -Thisdemo tt=NewThisdemo ("", 0);//the parameters that are passed in randomly - tt.print (); -     } +}

The call to this must be the first statement in the constructor

Here's an example:

1 Importjava.util.Arrays;2 classpeople3 {4     Private intAge ;5     PrivateString name;6 people (String name)7     {8          This. name=name;9     }TenPeople (String name,intAge ) One     { A         //This . People (name); -          This(name); -          This. age=Age ; the         //This (age); -     } -      Public voidspeak () -     { +System.out.println (name+ "," +Age ); -     } + } A /* at class Car - { - int num; - String color; - void Run () -     { in System.out.println (num+ "---" +color); -     } to } + */ - classNZF the { *      Public Static voidMain (string[] args) $     {Panax NotoginsengPeople p=NewPeople ("Sakura", 19); - p.speak (); the         /* + int []a1={1,2,3,4}; A int []a2={1,2,3,4}; the int []a3={2,3,4,5}; + System.out.println (Arrays.equals (A1,A2)); - System.out.println (Arrays.equals (A2,A3)); $ int []b=arrays.copyof (a1,6); $ for (int x:b) -         { - System.out.print (x+ ""); the         } - System.out.println ();Wuyi System.out.println (arrays.tostring (b)); the Arrays.fill (b,2,4,1); - System.out.println (arrays.tostring (b)); Wu Arrays.sort (b); - System.out.println (arrays.tostring (b)); About         */ $         /* - Car c=new car (); - c.num=5; - c.color= "green"; A C.run (); + Show (c); the         */ -         /* $ new Car (). num=5; the new Car (). color= "Red"; the new Car (). run (); the         */ the //Show (New Car ()); -     } in     /* the Public static void show (Car c) the     { About c.num=6; the c.color= "BLACK"; the System.out.println (c.num+ "---" +c.color); the     } +     */ -}

The following results are displayed:

If the Exchange line number 13-14 This sequence, change to this:

        this. age= age;          This (name);

The following error will occur:

To summarize:

1) The This keyword is a reference to itself within the class, and it is convenient for the method in the class to access its own properties;

2) You can return a reference to the object's own class, and you can also call another constructor in one of the constructor functions.

3) The call to this must be the first statement in the constructor or an error will be

Java Learning Note 14, this usage subsection in Java

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.