This is called when a reference to the current object is internally obtained:
(1) Return returns the current object;
(2) constructor call also has a constructor, with the number of parameters;
(3) The name of the parameter and the name of the data member are the same;
Attention:
This constructor can only be called once in a method;
A non-constructor cannot call this with a parameter.
//:flower.java/** * Constructor * * Created by C.l.wang on 15/7/12. * * Public class Flower { intPetalcount =0; String s ="Initial value"; Flower (intPetals) {petalcount = petals; System.out.println ("int arg only, Petalcount ="+ Petalcount); } Flower (String ss) {s = ss; System.out.println ("String arg only, Petalcount ="+ s); } Flower (String s,intPetals) { This(petals); This. s = s; System.out.println ("String & int args"); } Flower () { This("HI", -); System.out.println ("Default construction (No args)"); }voidPrintpetalcount () {System.out.println ("Petalcount ="+ Petalcount +"s ="+ s); } Public Static void Main(string[] args) {Flower x =NewFlower (); X.printpetalcount (); }}/** * output:int arg only, Petalcount = String & int args default construction (no args) Petalcount = s = h I/*//:~
How to use Java-this