如何从子类调用在父类中没有抽象声明的getter方法

huangapple go评论58阅读模式
英文:

How to call a getter from a subclass which hasn't an abstract declaration in the superclass

问题

以下是翻译好的内容:

我有一个超类,我将其实例化为ArrayList。超类的一个子类被保存在ArrayList中。现在我想从ArrayList中获取信息,但是只有在子类中才有属性的getter可用。我可以从其他具有抽象声明的类中获取信息(这些类在抽象超类中声明)。然而,我不想在所有扩展超类的类中实现“虚拟”方法。

以下是代码的一部分:

public class Question{

        QuestionPool() {
         questionPool = new ArrayList<Question>();
         ClozeQuestions q15 = new ClozeQuestions("Erster Text", "Zweiter Text");
         questionPool.add(q15);
        }
         public int startQuiz{
         System.out.printf("Q: %s", questionPool.get(i).?????
        }
}

public abstract class Question {

    String question;
    public String getQuestion() {
    return question;
    }
}

public class ClozeQuestions extends Question {

    ClozeQuestions(String questionPart1, String questionPart2){
        this.questionPart1 = questionPart1;
        this.questionPart2 = questionPart2;
    }

    public String getQuestionPart1() {
        return questionPart1;
    }

    public String getQuestionPart2() {
        return questionPart2;
    }  
}        

为了解决这个问题,我在Question类中实现了一个“辅助方法”:

public String getQuestionPart1(){
        ClozeQuestions question2ClozeQuestion = new ClozeQuestions();
        return question2ClozeQuestion.getQuestionPart1();
    }

然后我从QuestionPool类中调用它:

System.out.printf("Q: %s", questionPool.get(i).getQuestionPart1());
英文:

I have a superclass that I instantiate as ArrayList. A subclass of the superclass is saved in the ArrayList. Now I want to get the information out of the ArrayList, but the getter for the attribute is only available in the subclass. I can get the information (which are saved in the ArrayList too) from other classes that have an abstract declaration in the abstract superclass. However, I don’t like to implement “dummy” methods in all classes which extend the superclass.

Code truncated:

public class Question{

        QuestionPool() {
         questionPool = new ArrayList&lt;Question&gt;();
         ClozeQuestions q15 = new ClozeQuestions(&quot;Erster Text&quot;, &quot;Zweiter Text&quot;);
         questionPool.add(q15);
        }
         public int startQuiz{
         System.out.printf(&quot;Q: %s&quot;, questionPool.get(i).?????
        }
}

public abstract class Question {

    String question;
    public String getQuestion() {
    return question;
    }
}

public class ClozeQuestions extends Question {

    ClozeQuestions(String questionPart1, String questionPart2){
        this.questionPart1 = questionPart1;
        this.questionPart2 = questionPart2;
    }

    public String getQuestionPart1() {
        return questionPart1;
    }

    public String getQuestionPart2() {
        return questionPart2;
    }  
}        

To circumvent the problem I implemented a "helper method" in the Question class:

        ClozeQuestions question2ClozeQuestion = new ClozeQuestions();
        return question2ClozeQuestion.getQuestionPart1();
    }

Which I call from the QuestionPool class:

System.out.printf(&quot;Q: %s&quot;, questionPool.get(i).getQuestionPart1());

答案1

得分: 0

由于列表中的对象存储为超类类型,您需要在访问子类方法之前将其强制转换为子类。类型转换的示例如下:

(Subclass)objectOfSuperclassType

为了安全起见,在进行强制转换之前应进行类型检查,可以使用 instanceof 进行检查:

if (objectOfSuperclassType instanceof Subclass) {
    Subclass objectOfSubclassType = (Subclass)objectOfSuperClass;
}

最后,以下是处理列表时如何实现的示例:

List<Superclass> myList = new ArrayList<>();
myList.add(new Subclass());

if (myList.get(0) instanceof Subclass) {
    System.out.println( ((Subclass) myList.get(0)).getSomeString() );
}

尽管这可能回答了您的问题,您提供的代码实际上不要求列表在初始化时必须使用超类。只有当您有两个不同的子类并将它们存储在同一个列表中时,才有必要这样做。

英文:

Since the objects in the list are stored as the type of the superclass, you will have to cast it to the subclass before you can access the subclass methods. This is what type casting looks like:

(Subclass)objectOfSuperclassType

And to be safe, you should type check before casting which can be done with 'instanceof':

if (objectOfSuperclassType instanceof Subclass) {
    Subclass objectOfSubclassType = (Subclass)objectOfSuperClass
}

And finally, an example of how to implement this when dealing with a list:

List&lt;Superclass&gt; myList = new ArrayList&lt;&gt;();
myList.add(new Subclass());

if (myList.get(0) instanceof Subclass) {
        System.out.println( ((Subclass) myList.get(0)).getSomeString() );
}

Although this may answer your question, the code you provided does not require the List to be initialized with the superclass at all. If you were to have two different subclasses and stored both of them in the same list, only then would it make sense to do this.

huangapple
  • 本文由 发表于 2020年4月8日 05:02:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/61089364.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定