简体   繁体   中英

Child classes need to set unique object field of parent class?

I have a parent class that has a declared unique ModelType object, which also has child classes. Each Child class has a unique modeltype so I want the reference variable "model" to be the same across all the child classes, so the variable can be referenced. However, after initializing the modeltype to whatever child object, my function throws an error, because the type isn't the ModelType, but rather Cuboid or Pyramind in this case (one of the children).

There's probably a better way to do this than creating a Parent and setting it to the child. How can I have a reference variable in the parent class that can be set to multiple types? In other words, the variable "model" needs to be different types.

class Parent {
  ModelType model;
}

class ChildOne extends Parent {
   model = new Cuboid();
   void float vertices(Pyramid c){
      // ... stuff ...
   }
}

class ChildTwo extends Parent {
   model = new Pyramid();

   void float vertices(Pyramid c){
      // ... stuff ...
   }
}

I get an error when using vertices when passing the "model" of another ChildTwo to that class where:

The function vertices(ModelType) doesn't exist


ChildTwo a = new ChildTwo();
ChildTwo b = new ChildTwo();
a.vertices(b.model);

Generic class can be used in this case with Upper Bouded WildCard , so that the vertices method can be restricted to accept parameter according to the child class type parameter.

abstract class Parent<T extends ModelType> {
    T model;

    abstract float vertices(T c);

    // To set the model, setter can be created in parent class.
    // Or through constructor of child class.
    void setModel(T modelToSet) {
        this.model = modelToSet;
    }
}

class ChildOne extends Parent<Cuboid> {
    ChildOne(Cuboid c) {
        this.model = c;
    }

    float vertices(Cuboid c) {
        // ... stuff ...
        return 0;
    }
}

class ChildTwo extends Parent<Pyramid> {
    ChildTwo(Pyramid p) {
        this.model = p;
    }

    float vertices(Pyramid p) {
        // ... stuff ...
        return 0;
    }
}

Hello!

You are getting this error because while specifying the method "vertices()" to receive a reference of an object of the type "Pyramid" as argument, you are passing a reference to an object of the type "ModelType" instead. To solve this, one option is to change the "vertices()" method parameter to the type "ModelType" . See bellow.

float vertices(ModelType c){
  // ... stuff ...
}

Your confusion is normal and is related to the understanding of inheritance. Just remember, while a variable of the type of a superclass can contain a reference to any instance (object) of it's subclasses, the opposite isn't true. A superclass "doesn't know of the existence of it's childs".

I hope my explanation was clear and will be of help to you. See ya!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM