Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中使用内部类_Java - Fatal编程技术网

在Java中使用内部类

在Java中使用内部类,java,Java,我现在正在研究一个推荐系统。我设计了一个类,如: class Recommend{ Path path; //recommended path, self-defined class. This is the problem! //some parameters and Models private Map<Integer, List<Integer>> Model1; //...other pram and models

我现在正在研究一个推荐系统。我设计了一个类,如:

class Recommend{
    Path path; //recommended path, self-defined class. This is the problem!
    //some parameters and Models        
    private Map<Integer, List<Integer>> Model1;
    //...other pram and models

    //methods
    public void getPath();
    public double score(int place);//
}
您的“getPath()”方法是问题的一部分;它“什么都没做”。您的“推荐”类似乎是为了给路径打分而设计的——而该路径似乎并没有实际暴露在推荐之外。您可以考虑在Advand中使用一种新的方法,即“Add(int Stand,DouthTime)”,并使该方法更新路径,然后调用一个私有的“ScRePASScript()”方法。然后path应该是一个内部类,因为它的唯一功能是保存数据结构


您可以通过在JavaDoc中写下每个类的用途来澄清您试图做什么。每门课应有一个明确的目的。例如,如果一旦你发现“路径”不需要出现在“推荐”之外,那么你可以将其设置为私人成员。

无论你是使用内部类还是组合(如你所做),你都可以实现你的目标。我认为事情更复杂,因为你没有遵循分离关注的原则


想想为什么需要getPath()score()类中的推荐方法。像add()方法一样,您的getPath()也应该是Path类的一部分。

为什么不将
评分
方法归为
Path
类呢它将适合@RC@我考虑过这种做法。上面的代码是一个演示,在实践中,有一大堆模型需要在推荐中初始化。推荐是一种稳定的,在运行时可能只有一个实例。相反,Path不是,因此如果我将
score
放在该类中(这在很大程度上取决于那些模型),每当我想实例化Path的新实例时,我都需要初始化模型,这是低效的。我在给RC的评论中解释了这一点。谢谢你的相关建议,我正在努力做到这一点。
class Path{
    List<Integer> path;
    List<Integer> times;
    double socre;

    public void add(int place, double time);
}
getPath(int place, double startTime);
//try greedy searching to find a time sensitive path that scored highest, 
//go through all possible places and scored them.

socre(int place, double time);
//In fact there are multiple score methods correspond to different models,
//**and notice here I modify the param to int place from Path path**
//This function evaluates the score of current place and time(used in greedy searching)

add(int place, double time);
//add a new place and time(which is proved by greedy searching to be the best)
//I want this update the score of place itself...okay maybe I should place this function in Recommend