开发者

Java, opinion on using Object class?

I'm wondering what the general opinion is on using Java's Object class for something like this:

private Car myCar;
private String myCarBrand;
public 开发者_JS百科void set(String variableToSet, Object valueToSet){
    if(myCarBrand.equals("AUDI")){
        if(varibleToSet.equals("COLOR"))
            (Audi myCar).setColor((Color) valueToSet);
        ...
    }
    ...
}

Somehow I have the feeling that I shouldn't be using the Object class. However my reason for doing so is that not all cars have the same setter functions, now I could split this into multiple setters with the same name, but with different types of valueToSet, that however requires a lot more code, especially if my valueToSet's have largely different types.

Thanks

ERRATA

  • Audi extends Car

  • setColor is supposed to represent a class that is not applicable to all cars, something like turnOnBackSeatDiscoLights might have been better suited.


I don't agree your design of your code writing O-O programming.

You should design your class as Car class, in the car object, you should have a method to set the color

public Car{
   //attributes of your car class
   private brandName;
   private color;
   ....
   ....
   ...constructor...

   public setColor(String color)
     this.color = color;
   public setbrandName(String brandName)
     this.brandName = brandName;    

}

From your question, I don't know what is your class actually refers, however, your function should not only have a name "set" which is not appropriate, it's hard to guess what it means, what to do. And remember when writing OO programming, you should remember to do abstraction.

To be specific to your answer, I can suggest

make use of the inheritance of your base Class (Car). You can make your Car class as abstract. and for a specific brandname of your car, which contains specific attributes or methods, you should subclass the Car class.

One important point to remember is that, for every method , attribute you declare, they must have some "meaning" for your Class. This is important concept of OO, otherwise, your code would not be maintained by other so easily, a bad example is your set function.


Short answer, don't write your own code to do this - use java beans.

How do you intend to call that set() method? Is it driven by some xml or properties file?

Consider the use of classes like java.beans.Introspector.


There are at least some instances where this is done in the Standard Library (e.g. Container.add(Component comp, Object constraints)), so it is not unprecedented.


This is not a good idea in my opinion. You are throwing away all the advantages of Java's compile-time type checking.

In the case of an attribute like colour that is relevant to all cars you should be able to set or get it without casting to a subclass like Audi. In the case where your objects have different behaviour and different types of attributes, you will need a lot more code to handle this anyway, even if you have one very general set method.


However my reason for doing so is that not all cars have the same setter functions, now I could split this into multiple setters with the same name, but with different types of valueToSet, that however requires a lot more code, especially if my valueToSet's have largely different types.

The way to deal with different kinds of things (in this case Cars) with different attributes / getters / setters is to define a hierarchy of classes.

This requires more code, but the code is simpler, and it is statically typed and therefore more reliable.


The problem with this code is that you're losing type-safety. Consider the following code:

Car car = new car();
car.setBrand("AUDI");
car.set("COLOR", "red"); //ka-boom

You could use inheritance to define a base car and then extend from it.

public class Car {
//....
}

public class Audi extends car {
//....
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜