Assuming that I have a purpose of class car, such as from 30 to 30 variables from top speed I want to create a MyCar object (which extends Class Mike Cars) which is basically the same thing, besides it stores some more information.
It is not possible for me to create a MyCar object yet (only a few of the car objects will be made up of a few MICAR objects), unless I leave the spare field empty, but also Does not look professional or build a constructor which takes 30 columns, or method sets 30 arguments through call.
So, is there a way to easily get all the variables from a superclass object?
PS: My program is not about cars, but I thought it would be an easy example.
Edit
Thanks for the answer, they are all useful for my program, but not for this particular problem. Builders are not beneficial because my cars do not have default values for their variables. Every time a car is built, all the variables are filled (which is necessary to create a "fact sheet").
The envelopes are an interesting design, but still I wanted the variables in all the subclasses manufacturers that I would have a way to overcome this. Templates will also require me to copy all the variables one by one.
The subclass in my program acts as a "wrapper class" in a search engine. Subclasses are similar to normal cars, but they have "ranking scores". My program is designed to display regular cars And by expanding them, I can easily display sub-classes and order them with a score at the same time.
I have to make new objects in any way, because many searches can be done on the same list of cars, so editing the variables in the original cars was not an option. Perhaps this problem is a better solution, but now I think that I have to send the superclass object to the constructor and all will have to copy
PS: I am just thinking, Maybe I could throw all the variables in a hashmap. In this way I can access them using .get, and I only have to pass a Hashmap variable in the subclass. I have to pour a lot towards the bottom, because the wires are a mixture of strings, moves, doubles and so on. What do you think, is this acceptable coding style?
Effective Java version 2, item 2: When facing a manufacturer's standards, a manufacturer Think
And neither produces a constructor that takes 30 columns, or the method sets 30 arguments through the call.
If you are facing Constructor with the too many parameters then you have a look at: this idea is only optional Without having to worry, or want to use the default, just set the field you want / know Builder value , then the actual object Call build () for the construction. See java examples of that article.
Once you set up that pattern, you can construct a car like this (clean structure notice):
car car = new car. Builder (expected_update 1, expected_most_ 2). Color (red) // optional parameter .topSpeed (300) .zeroToHundred (2) .build ();
Comments
Post a Comment