简体   繁体   English

Java找不到类的方法

[英]Java can not find method of class

I am trying to call a method of my selfmade "Filter" class, but the Compiler can not find the method. 我正在尝试调用自制的“ Filter”类的方法,但是编译器找不到该方法。

It is a little bit tricky, because I write my code in Eclipse where everything seems to be fine. 这有点棘手,因为我在一切似乎都很好的Eclipse中编写了代码。 But I have to copy my code into another Software and compile it there as well. 但是我必须将我的代码复制到另一个软件中并在那里进行编译。 This Compiler on the other hand can not find my method. 另一方面,此编译器找不到我的方法。

I assume that the Eclipse Compiler maybe "allows" more errors of which I am not even aware of. 我认为Eclipse编译器可能“允许”更多我什至不知道的错误。 The other Compiler however struggles with them. 然而,另一个编译器却与之抗争。

Here is what the "other" Compiler prints as error code: 这是“其他”编译器作为错误代码输出的内容:

C:\Program Files\Enomic\enomic-server\data\rules\testcompileboehmch\CodeTest.java:77: error: cannot find symbol filt.setHasRemoved(true);
symbol:   method setHasRemoved(boolean)
location: variable filt of type tms.Filter

I have no idea why my class is incorrect. 我不知道为什么我的课不正确。 As said above in eclipse everything works fine. 如上文所述,在日食中一切正常。

The CodeTest Class(cut down to the important part): CodeTest类(缩减至重要部分):

package tms;

import tms.Filter;
import java.util.*;

    Filter filt = new Filter();
    filt.setHasRemoved(true);//cannot be found

My Filter class: 我的过滤器类:

package tms;

import java.util.ArrayList;
import java.util.List;

public class Filter {

private List<Object> remainingList;
private List<Object> removedList;
private Object typ;
private boolean hasRemoved;

public Filter()
{
    this.remainingList = new ArrayList<Object>();
    this.removedList = new ArrayList<Object>();
    this.typ = new Object();
    this.hasRemoved = false;
}   
public Filter(List<Object> remaining, List<Object> removed, Object typ, boolean hasRemoved)
{
    this.remainingList = new ArrayList<>();
    if(remaining != null)
    {
        this.remainingList.addAll(remaining);
    }
    this.removedList = new ArrayList<>();
    if(removed != null)
    {
        this.removedList.addAll(removed);
    }
    this.typ = typ;
    this.hasRemoved = hasRemoved;
}

//Set-Methoden      
public void setRemainingList(List<Object> list)
{
    this.remainingList.clear();
    this.remainingList.addAll(list);
}
public void setRemovedList(List<Object> list)
{
    this.removedList.clear();
    this.removedList.addAll(list);
}
public void setTyp(Object val)
{
    this.typ = val;
}
public void setHasRemoved(boolean val)
{
    this.hasRemoved = val;
}

//Get-Methoden
public List<Object> getRemainingList()
{
    return this.remainingList;
}
public List<Object> getRemovedList()
{
    return this.removedList;
}
public Object getTyp()
{
    return this.typ;
}
public boolean getHasRemoved()
{
    return this.hasRemoved;
}
} 

I really do not know why this doesn't work. 我真的不知道为什么这行不通。 Is there any mistake I can't see? 我看不到任何错误吗?

Here: 这里:

C:\\Program Files\\Enomic\\enomic-server\\data\\rules\\testcompileboehmch\\CodeTest.java:77: error: cannot find C:\\ Program Files \\ Enomic \\ enomic-server \\ data \\ rules \\ testcompileboehmch \\ CodeTest.java:77:错误:找不到

And

package tms;

The point is: the java compiler expects that the folder structure resembles the package structure. 关键是:java编译器期望文件夹结构类似于结构。

So your problem is that your classes do not exist in a directory named tms . 因此,您的问题是您的类在名为tms的目录中存在。

It is rather strange that eclipse works here either. 日食在这里也很奇怪。 In that sense: you want to read here or there for example. 从某种意义上说:例如,您想在这里那里阅读。

And no, there are only very few subtle things where the eclipse java compiler works differently than other products. 不,eclipse java编译器与其他产品的工作原理几乎没有什么微妙的区别。 Rest assured: chances that you as newbie run into such things are very close to zero. 放心:您作为新手遇到这种情况的机会几乎为零。 Your problems are caused by the fact that you do not understand the basics of how java files are compiled. 您的问题是由于您不了解如何编译Java文件的事实引起的。 ( that is the downside when you start learning programming using an IDE such as eclipse - the IDE hides many of these things from you. and then, when you need it - you have no idea what is going on). (这是您开始使用诸如eclipse之类的IDE学习编程的缺点-IDE向您隐藏了许多这些东西。然后,当您需要它时-您不知道发生了什么。)

By renaming my Filter Class I was able to solve the problem. 通过重命名我的过滤器类,我能够解决问题。 It looks like the Compiler never knew exactly to which class i was referring. 看起来编译器从未完全知道我要指的是哪个类。 Nevertheless thanks for all the help, you directed me into the right direction. 不过,感谢您的所有帮助,您使我朝着正确的方向前进。

Note to myself (and maybe others too): Come up with your own class names! 提醒自己(也许还有其他人): 提出自己的班级名称!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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