Java Basic Learning Path (10) this keyword

Source: Internet
Author: User


ClassBook {String name; intPrice ; intnum;
Mutual calls between construction methods solve the code duplication problem, but be sure to leave the export PublicBook () { This("Please enter the title",0,0); } PublicBook (String name) { This(Name,9999,9999);//this Call Method} PublicBook (String name,intnum) { This(Name,num,9999); } PublicBook (String name,intNumintPrice ) { This. Name =name;//this Call Property This. Price =Price ; This. num =num; } PublicString GetInfo () {return "Title:"+ This. name+"\ n"+"Number:"+ This. num+"\ n"+"Unit Price:"+ This. Price; }} Public classTest1 { Public Static voidMain (String args[]) {book Book_0=NewBook (); Book Book_1=NewBook ("my world."); Book Book_2=NewBook ("the old man and the Sea",5); Book Book_3=NewBook ("Lu Bingxun",3,2); System. out. println (Book_0.getinfo ()); System. out. println (Book_1.getinfo ()); System. out. println (Book_2.getinfo ()); System. out. println (Book_3.getinfo ()); } }

Java Basic Learning Path (10) this keyword

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.