In general, we invoke the method of the class in Java by instantiating a class, but if there are multiple classes calling this method of the class, is it necessary to create an object of the called class within each of the keynote classes?
If this is done, will it affect the execution of the program because of multiple instantiation?
The answer is: there is no need to create the object of the called class more than once, and multiple instantiation can also affect the performance of the program.
The solution is to write a constructor in each of the keynote classes, and the parameter is the object of the called Class.
Public Shopcartitem (Shopcart shopcart) { Super(); this. shopcart=shopcart; }
when instantiating the called class, we get an object that, when instantiating the keynote class, invokes the constructor with a called class object, so that the object of the called class that is instantiated inside the test class is passed to the various keynote functions, thus avoiding the instantiation of the called Class. as follows:
Shopcartitem shopcartitem=New Shopcartitem (shopcart); // shopcartitem shopcartitem=new shopcartitem ();
Take a look at the following code:
(The purpose of the program is to use HashMap to add books to the shopping cart and to get the number of books according to the name of the book)
Shopping Cart Class (set up, implement Add book method) ************************
1 PackagePractise05_1;2 3 ImportJava.util.*;4 5 Public classShopcart {6Map map=new HashMap (); 7 Public voidAdd (book book) {//Book is the shape of parameters, as long as the guaranteed value simultaneous is the object, the object name is arbitrary8Map.put (Book.getname (), book);//form parameter, with the same name as book9 }Ten}
Book Class **********************
1 PackagePractise05_1;2 3 Public classBook {4 Private intNumber ;5 Private DoublePrice ;6 PrivateString name;7 PublicBook (intNumberDoublePrice , String name) {8 Super();9 This. Number =Number ;Ten This. Price =Price ; One This. Name =name; A } - PublicBook (intNumberDoublePrice ) { - Super(); the This. Number =Number ; - This. Price =Price ; - } - PublicBook () { + Super(); - } + PublicString GetName () { A returnname; at } - Public voidsetName (String name) { - This. Name =name; - } - Public intGetNumber () { - returnNumber ; in } - Public voidSetnumber (intNumber ) { to This. Number =Number ; + } - Public DoubleGetPrice () { the returnPrice ; * } $ Public voidSetprice (DoublePrice ) {Panax Notoginseng This. Price =Price ; - } the}
Auxiliary class (implements the return quantity method) ************************
1 PackagePractise05_1;2 3 ImportJava.util.*;4 ImportJava.util.Scanner;5 6 Public classShopcartitem {7Scanner sc =NewScanner (system.in);8 Shopcart Shopcart = new Shopcart ();9 //Get QuantityTen Public voidGetbooknumber () { OneSYSTEM.OUT.PRINTLN ("Please select the name of the input book from OOP, JAVA, C + +, sql:")); AString str=Sc.next (); -Book book=(book) shopcart.map.get (str); - if(Shopcart.map.containsKey (str)) { theSystem.out.println ("The current quantity is:" +Book.getnumber ()); - } - Else{ -System.out.println ("You don't have this book in your shopping cart!") "); + } - } +}
Test class ***********************
1 PackagePractise05_1;2 3 Public classHashmaptest {4 5 /**6 * @paramargs7 */8 Public Static voidMain (string[] args) {9Shopcart shopcart=NewShopcart ();TenShopcart.add (NewBook (132,98, "OOP")); OneShopcart.add (NewBook (12,12.5, "JAVA")); AShopcart.add (NewBook (22,45, "JSP")); -Shopcart.add (NewBook (132,98, "OOP")); -Shopcart.add (NewBook (42,18, "C + +")); the //Shopcartitem shopcartitem=new Shopcartitem (shopcart);Shopcartitem shopcartitem=new Shopcartitem (); - Shopcartitem.getbooknumber (); - } +}
The results of the operation are as follows:
Will find that there is an OOP book in the collection, the results of the query is control. Why? The reason is that the auxiliary test class inside the 9th row has been instantiated a shopping cart class, and the helper class inside again instantiated a shopping cart class, which in general case is possible. However, the special point is that we set up the collection object in the shopping cart class, although the object of the shopping cart is instantiated, there is no explicit point to execute the sentence, but in fact, when the instantiation, the set of the code is also executed, the equivalent of this code
1 Map map=New HashMap ();
is written in the shopping Cart class construction method, each time the class is instantiated to re-establish a collection, so the new set of values are empty.
So. How to modify it?
As follows:
In the auxiliary class, the 9th line of red code is changed to the following code:
1 NULL ; 2 Public Shopcartitem (Shopcart shopcart) {3 Super (); 4 this. shopcart=Shopcart; 5 }
Test class inside the 16th line of red code with the 15th line Purple Code, OK problem solved! Run again, the results are as follows:
Questions about how to do not create objects in one class by creating another class, but calling methods of another class