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