ImportJava.util.Scanner; Public classanything { PublicAnything () {//defining a parameterless construction method This("This call has a constructor method");//using the This keyword to invoke a parameterized construction methodSYSTEM.OUT.PRINTLN ("Non-parametric construction method"); } PublicAnything (String name) {//defining a method for constructing a parameterSystem.out.println ("Constructive method"); } Private intId//define a member variable of type int PrivateString name;//defines a member variable of type string PrivateString category;//defines a member variable of type string PublicString GetName () {//define a GetName () method intid = 0;//Local VariablesSetName ("Java");//calling other methods in the class returnID + This. Name;//Set method return value } Private voidSetName (String name) {//define a SetName () method This. name = name;//assigning a parameter value to a member variable in a class } Public /* Book*/anything GetBook () {System.out.println (ID); return This;//returns the book class reference } //define a book class PrivateString title;//define the title PrivateString author;//Define author Private DoublePrice//Define Price PublicAnything (String title,string author,DoublePrice) {//initializing a domain with a construction method This. title =title; This. Author =author; This. Price =Price ; } PublicString GetTitle () {//get the title returntitle; } PublicString Getauthor () {//Get author returnauthor; } Public DoubleGetPrice () {//Get the price returnPrice ; } //Temperature Unit conversion Tool Public DoubleGetfahrenheit (DoubleCelsius) {//Calculate Fahrenheit Temperature DoubleFAHRENHEIT = 1.8 * Celsius + 32;//return temperature of Fahrenheit returnFahrenheit; } Public Static voidMain (string[] args) {//defining the Main method for(intI =0;i<args.length;i++) {//loop operation according to parametersSystem.out.println (Args[i]);//Loop Print parameter Contents} anything book=NewAnything ("Java from beginner to proficient (second edition)", "Tomorrow Technology", 59.8);//Creating ObjectsSystem.out.println ("title:" +book.gettitle ());//Output TitleSystem.out.println ("" +book.getauthor ());//Output AuthorSystem.out.println ("Price:" +book.getprice () + "Yuan");//Output PriceSystem.out.println ("Please enter the temperature to be converted ()"); Scanner in=NewScanner (system.in);//Get console input DoubleCelsius = in.nextdouble ();//get the user input Celsius temperatureanything converter=NewAnything ();//to create an object of a class DoubleFahrenheit = Converter.getfahrenheit (Celsius);//conversion temperature is FahrenheitSystem.out.println ("The temperature of the conversion completed (unit: Fahrenheit)" +fahrenheit);//Output Conversion Results } }
Public classemperor{Private StaticEmperor Emperor =NULL;//declaring a reference to a Emperor classPrivateEmperor () {//The construction method is private} Public StaticEmperor getlnstance () {//instantiating a reference if(Emperor = =NULL) {Emperor=NewEmperor (); } returnEmperor;} Public voidGetName () {//use normal methods to output the emperor's nameSystem.out.println ("I am the Emperor; tomorrow's Technology"));} Public Static voidMain (string[] args) {System.out.println ("Create Emperor 1 object:"); Emperor Emperor1= Emperor.getlnstance ();//Create Emperor ObjectEmperor1.getname ();//output The name of the EmperorSystem.out.println ("Create Emperor 2 object:"); Emperor Emperor2=emperor.getlnstance (); Emperor2.getname (); System.out.println ("Create Emperor 3 object:"); Emperor Emperor3=emperor.getlnstance (); Emperor3.getname ();}}
0118 Practice Single-case mode