gpt4 book ai didi

java - 良好的面向对象设计

转载 作者:太空宇宙 更新时间:2023-11-04 08:25:39 25 4
gpt4 key购买 nike

问题和答案有层次结构:

class Answer
{
int doubtLevel;
}
class YesNoAnswer extends Answer
{
boolean isYes;
}
class ColorAnswer extends Answer
{
int red;
int green;
int blue;
}

class Question
{
Context ctx;
abstract List<? extends Answer> answers;
}
class DoesItWorkQuestion extends Question
{
Thing it;
List<YesNoAnswer> answers;
}
class IsItTastyQuestion extends Question
{
Dish dish;
List<YesNoAnswer> answers;
}
class FavoriteColorQuestion extends Question
{
List<ColorAnswer> answers;
}

首先,我不喜欢在每个具体类中包含答案列表只是为了修复答案的类型。有没有办法定义一个结构来将答案列表保留在问题之外?但问题的答案类型必须是固定的。

然后我需要创建为问题中的每个答案保存一个值的结构,例如假设我想要一个计算器来计算每个答案的概率:

class AnswerProbability<Q extends Question>
{
Q question;
//??
double getProbability(???Answer answer){...}
}
class ProbabilityCalculator
{
// different params may produce different answer lists
//(e.g. if probability of an answer is less than 1%, don't include
//the answer in the list).
AnswerProbability<IsItTastyQuestion> tastyQuestionProbability(String param1, String param2);
AnswerProbability<FavoriteColorQuestion> favoriteColorQuestion(String param);
...
}
...

以及如何使用它:

AnswerProbability<IsItTastyQuestion> prob = calculator.tastyQuestionProbability(...);
// I want the "get" method take only YesNoAnswer type.
println(prob.get(new YesNoAnswer(true, 10) + prob.get(new YesNoAnswer(false, 5));
// or:
for(YesNoAnswer ans : prob.question.answers)
{
if(ans.isYes)
println(prob.get(ans));
}
// Also I need work with superclasses
List<AnswerProbability> aps;
aps.add(calculator.calculateTastyQuestionProbability("soup", "mushroom"));
aps.add(calculator.calculateFavoriteColorQuestion("socks"));
..
// use `doubtLevel` from the `Answer` superclass.
for(AnswerProbability ap : aps)
{
for(Answer a : ap.question.answers)
{
if(a.doubtLevel < 5)
println(ap.get(a));
}
}

所以,我想避免类型转换和运行时检查。

最佳答案

你不能有抽象字段。您可以在此处使用泛型

class Question<A extends Answers> {
Context ctx;
final List<A> answers = new ArrayList<A>();
}

class DoesItWorkQuestion extends Question<YesNoAnswer> {
Thing it;
}

class FavoriteColorQuestion extends Question<ColorAnswer> {
}

但是我怀疑记录每个答案的计数会更有用。

class Question<A extends Answers> {
Context ctx;
final Map<A, Integer> answerCounts = new HashMap<>();
}

class DoesItWorkQuestion extends Question<YesNoAnswer> {
Thing it;
}

class FavoriteColorQuestion extends Question<ColorAnswer> {
}

// later
for(Entry<YesNoAnswer, Integer> ansCount : prob.question.answers.entrySet()) {
switch(ansCount.getKey()) {
case YES:
int count = ansCount.getValue();
// something
break;
}
}

这更有效,因为大小与可能答案的数量成正比,而不是答案的数量,并且计算概率非常容易。

关于java - 良好的面向对象设计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8593797/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com