我们正在研究数据挖掘项目。问题是在指定的代码中无法访问对象模式。任何人都可以解释代码中的问题吗?
try{
String query = "select algm from accuracy where id=1";
PreparedStatement pst = connection.prepareStatement(query);
ResultSet rs = pst.executeQuery();
alg = rs.getString("algm");
pst.execute();
pst.close();
}catch(Exception e1){
JOptionPane.showMessageDialog(null,e1);
}
if(alg.equals("DecisionStump"))
{
DecisionStump Mode = new DecisionStump();
}
else if(alg.equals("NaiveBayes"))
{
NaiveBayes Mode = new NaiveBayes();
}
// filter for removing samples:
Remove rm = new Remove();
rm.setAttributeIndices("1"); // remove 1st attribute
// filtered classifier
FilteredClassifier fc = new FilteredClassifier();
fc.setFilter(rm);
fc.setClassifier(Mode); //This object Mode is not accesible
答案 0 :(得分:1)
您在if或else if语句中声明Mode
对象,并在其不再可用的区域外使用相同的对象(块范围)。
您可以执行以下操作:
Classifier Mode; // a parent class
if(alg.equals("DecisionStump")) {
Mode = new DecisionStump();
} else if(alg.equals("NaiveBayes")) {
Mode = new NaiveBayes();
}
答案 1 :(得分:0)
标识符Mode
正在一个立即退出范围的本地块中声明。
Classifier mode
if(alg.equals("DecisionStump"))
{
mode = new DecisionStump();
}
else if(alg.equals("NaiveBayes"))
{
mode = new NaiveBayes();
}