Java — Initializing superclass variables in subclasses?

前端 未结 7 1525
逝去的感伤
逝去的感伤 2020-12-19 04:18

Okay, so, for example, let\'s say I have an abstract class called \"Vehicle\". The Vehicle class, has, among other things, a static variable called wheels, which is not init

相关标签:
7条回答
  • 2020-12-19 04:27

    I think there's a significantly more elegant way to do this

    What I am about to propose still suffers from the limitation that you need an instance. I don't see any way around that because you want wheels to be exposed as part of the superclass, but the value of wheels is dependent on the subclass and inside of Vehicle there is no notion of a subclass type without an instance.

    In my opinion, 'wheels'in this case is neither a static or non-static property. It is class metadata. And the Java way to specify class metadata is via annotations.

    What you need is a user-defined annotation like this:

    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    public @interface VehicleMetadata{
        int wheels();
    }
    

    You then annotate Motorcyle as follows:

    @VehicleMetadata(2)
    public class Motorcycle extends Vehicle {}
    

    In the superclass you provide an accessor that gets the value of the annotation property. I would recommend you use a "lazy evaluation" approach so you don't use reflection every time you need the value.

    Note the use of this to get the instance:

    private String wheelsValue;
    
    public String getWheels() {
        if (this.wheelsValue== null) {
    
            VehicleMetadatane = null;
            for (Annotation annotation : this.getClass().getAnnotations()) {
                if (annotation instanceof VehicleMetadata) {
                    ne = (VehicleMetadata) annotation;
                    break;
                }
            }
    
            wheelsValue = ne.wheels();
        }
        return wheelsValue ;
    }
    

    In my opinion, this is the most elegant solution.

    0 讨论(0)
  • 2020-12-19 04:30

    If you make a static variable in your objects it will be the same for every Vehicle class you will make, even when you would make another subclass for your abstract Vehicle class. This because of the "nature" of any static variable.

    I think you want to use a non-static variable so that for every instance of any subclass of the abstract Vehicle class you can determine the value of the wheels and that is done as following:

    public abstract class Vehicle {
        public int wheels; //number of wheels on the vehicle
    }
    

    and any subclass:

    public foo extends Vehicle{
    
         public void someMethode(){
             this.wheels = 2;
         }
    }
    

    You could also do this for the static variable but then you will change it for every instance of any subclass of Vehicle

    Hope i helped you

    0 讨论(0)
  • 2020-12-19 04:31

    The original class declaration:

    public abstract class Vehicle {
        static int wheels; //number of wheels on the vehicle
    }
    
    public class Motorcycle extends Vehicle{...}
    public class Truck extends Vehicle{...}
    

    does not work because the static variable goes with the class it was declared in. Static class variables create memory storage for only one instance of the variable per class and not per class object. When the compiler (jvm) sees the static variable in the class Vehicle it allocates memory to that variable and that memory location is static (does not change). Each subsequent use of the Vehicle class whether it is extended or instantiated as an object will point to the same location in memory for the static variable.

    In order to use the static variable in the child classes you have to use it inside a method. So, you could in essence re-write your Motorcycle class like this:

    class Motorcycle extends Vehicle{
        public Motorcycle(){
            wheels = 2;
        }
    }
    

    and it will compile; however, you will may not get the results you expect. For example if you do this in your code (assuming Truck class is declared like Motorcycle class and assigns 4 to wheels and there is a getter method to return the value of wheels).

    Motorcycle cycle = new Motorcycle();
    Truck pickup = new Truck();
    ...
    System.out.println("Motorcycle has " + cycle.getWheels() + " wheels.");
    

    will print:

    Motorcycle has 4 wheels.

    0 讨论(0)
  • 2020-12-19 04:44

    Static members are only defined once and are common to every extending class. Changing the value in one of them will affect all of the others. This is what I believe you really want to achieve:

    public abstract class Vehicle {
        private int _wheels; //number of wheels on the vehicle
        public int getWheels(){return _wheels;}
    
        protected Vehicle(int wheels){
            _wheels = wheels;
        }
    }
    
    public class Motorcycle extends Vehicle {
        public Motorcycle(){
            super(2);
        }
    }
    
    public class Car extends Vehicle {
        public Car(){
            super(4);
        }
    }
    
    0 讨论(0)
  • 2020-12-19 04:50

    What you're trying to do is fundamentally flawed. You could make Motorcycle initialize wheels once:

    // Static initializer
    static
    {
        wheels = 2;
    }
    

    ... or each time an instance was created:

    // Instance initializer
    {
        wheels = 2;
    }
    

    But there's just one variable - not one for Motorcycle, one for Truck etc. If you did the same thing for both Truck and Motorcycle, then whichever is initialized last would "win".

    It's not clear how you want to use this field anyway - but if you just have a single static field, then that's just going to have a single value - not one per subclass.

    0 讨论(0)
  • 2020-12-19 04:51

    Maybe you'd like to think about the constructors you are using.

    public Vehicle(int wheels) {
        this.wheels = wheels; 
    }
    
    public Motorcycle(int wheels) {
        super(wheels);
    }
    
    public Motorcycle cycle = new Motorcycle(2);
    

    The Motorcycle uses the super constructor that knows what to do with the parameter. It automatically sets wheels to 2.

    0 讨论(0)
提交回复
热议问题