How to use Java-this

Source: Internet
Author: User

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

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.