[Javase Study Notes]-6.5 class type parameters and anonymous objects
This section describes the class type parameters and anonymous objects.
Let's continue using the previous mini car.
Class Car {int num; // This is the tire count attribute String color; // This is the color attribute String brand; // This is the brand void start () // the startup behavior {System. out. println ("car started");} void run () // This is the running behavior {System. out. println (num + "... "+ color + "... "+ brand );}}What should we do if we want to create multiple identical cars for our Car class?
For beginners, we will certainly use the following operations:
Class CarDemo {public static void main (String [] args) {// define the first Car myCar1 = new Car (); myCar1.num = 4; myCar1.color = "red "; myCar1.brand = "BWM"; // defines the second Car myCar2 = new Car (); myCar1.num = 4; myCar1.color = "red"; myCar1.brand = "BWM "; // define the third or more vehicles. You can only write code in this way }}
The above code is really bad. We have done a lot of the same operations. If we want to define a lot of the same cars, the reusability of the Code is indeed too bad.
So what should we do? Obviously, we must improve code reusability. That is to say, we can encapsulate code with the same function to reuse it. Then we can do this:
Class CarDemo {public static void main (String [] args) {// define the first Car myCar1 = new Car (); assemble (myCar1); System. out. println ("myCar1 is a" + myCar1.color + "color" + myCar1.brand + "brand car with" + myCar1.num + "Wheel "); // define the second Car myCar2 = new Car (); assemble (myCar2); System. out. println ("myCar2 is a" + myCar2.color + "color" + myCar2.brand + "brand car with" + myCar2.num + "Wheel "); // define the third Car that can be used to reuse the assemble (car Car) Assembly function.} // we can close the Assembly function of a car, then you can call this function to make sure that the public static void assemble (Car car) // class type parameter points to the object, or it is null {car. num = 4; car. color = "red"; car. brand = "BWM ";}}From the above code, we can easily assemble the same vehicle and improve code reusability. At the same time, we noticed that the encapsulated function parameter is a form parameter of the class type, it points to a Car type object.
Let's take a look at the results of the above Code:
We can see from the results that the same car we want to implement is indeed implemented.
Let's take a look at the special case: anonymous objects.
An anonymous object is an object without a name.
New Car (); // an anonymous object is a simplified format for defining objects.
How do I use anonymous objects?
Let's take a look at an example. For example, if we want to create a car and run the run method, we will do this:
Car c = new Car (); // This is a famous Object c. run ();
For the above situation, we can abbreviated it as the following code:
new Car().run;
Then we can see
First usage:
When an object calls a method only once, it can be simplified to an anonymous object.
Let's look at a piece of code:
class CarDemo{public static void main(String[] args){new Car().num = 5;new Car().color = "green";new Car().run();}}
Let's take a look at the results:
For each of these three statements, a new space is created in the heap memory instead of only one object. when the following statement is executed, the previous statement has been released, so the previous two statements have no significance. however, the last sentence is executed because the object only calls the method once.
Anonymous objectSecond usage:
Anonymous objects can be passed as actual parameters.
For example, in the previous method assemble (Car car), we can use:
assemble(new Car());