简体   繁体   English

静态,非静态以及在Java Tapestry中单独的类之间进行调用

[英]Static, non-static, and calling between separate classes in Java Tapestry

In my Java Tapestry application, I have a class called NachrichtenBubble.java which tests to see if there are any messages to announce, and therefor if a particular element should be shown. 在我的Java Tapestry应用程序中,我有一个名为NachrichtenBubble.java的类,该类进行测试以查看是否有任何消息要宣布,因此是否应该显示特定元素。 It looks like this: 看起来像这样:

public boolean ShowBubble() {
    int n = getHowManyNachrichten();
    if (n == 0) {
        return false;
    }
    return true;
}

Now, in a different place in my application, I want to determine whether to show or hide an element based on how many messages there are. 现在,在我的应用程序的其他位置,我想根据有多少条消息来确定是显示还是隐藏元素。 Since this is already done in NachrichtenBubble.ShowBubble(), I'd like to be able to call ShowBubble() from my separate class (let's call it OtherClass.java) and act on the result. 因为这已经在NachrichtenBubble.ShowBubble()中完成,所以我希望能够从我自己的类中调用ShowBubble()(我们将其称为OtherClass.java)并根据结果进行操作。

If I put this into my OtherClass.java, I get an "Cannot make a static reference to the non-static method ShowBubble() from the type NachrichtenBubble" error: 如果将其放入OtherClass.java中,则会收到“无法从NachrichtenBubble类型对静态方法ShowBubble()进行静态引用”错误:

public boolean ShowNachrichten() {
    boolean m = NachrichtenBubble.ShowBubble();
    return m;
}

I have read through the answers to other static/non-static method questions on here, which has been educational, but I have been unable to apply them successfully to this problem. 在这里,我已经阅读了其他有关静态/非静态方法问题的答案,这是有教育意义的,但是我无法成功地将其应用于此问题。 I have tried to create a new Context method in NachrichtenBubble.java and called that from OtherClass.java, but it's not working. 我试图在NachrichtenBubble.java中创建一个新的Context方法,并从OtherClass.java中调用它,但是它不起作用。

Should I persist with attempting a Context-based solution, or is there something else that I could try to get a useable result from NachrichtenBubble.ShowBubble() into OtherClass.ShowNachrichten()? 我应该坚持尝试基于上下文的解决方案,还是我可以尝试将NachrichtenBubble.ShowBubble()转换为OtherClass.ShowNachrichten()的可用结果?

Your call to NachrichtenBubble is static, in other words you don't have an instance of the class. 您对NachrichtenBubble的调用是静态的,也就是说,您没有该类的实例。 The method ShowBubble is an instance method though. ShowBubble方法虽然是一个实例方法。 You could change the ShowBubble method to be static but only if the method it calls (getHowManyNachrichten()) also can be made static and doesn't use any instance fields, which seems unlikely considering the name. 您可以将ShowBubble方法更改为静态方法,但前提是该方法所调用的方法(getHowManyNachrichten())也可以设置为静态方法,并且不使用任何实例字段,考虑到名称,这似乎不太可能。

To put it differently, without a specific NachrichtenBubble you (probably) don't know how many Nachrichten there are, so you can't ask for that information. 换句话说,没有特定的NachrichtenBubble,您(可能)不知道有多少Nachrichten气泡,因此您无法要求该信息。

Alternatively, you could let your OtherClass know of this instance of NachrichtenBubble. 或者,您可以让OtherClass知道此NachrichtenBubble实例。

On another note, I would probably not re-use a method like that. 另一方面,我可能不会重用这种方法。 Especially since you can replace the entire method by getHowManyNachrichten() > 0 特别是因为您可以通过getHowManyNachrichten() > 0替换整个方法

This is completely unrelated to Tapestry, being instead a pure Java and Object-Oriented Programming question instead. 这与Tapestry完全无关,而是纯Java和面向对象的编程问题。 You can either change ShowBubble() and getHowManyNachrichten() to be static methods or, better yet, in ShowNarichten, create a NarichtenBubble field named narichtenBubble and call narichtenBubble.showBubble() instead. 您可以将ShowBubble()和getHowManyNachrichten()更改为静态方法,或者更好的是,在ShowNarichten中,创建一个名为narichtenBubble的NarichtenBubble字段,然后调用narichtenBubble.showBubble()。 By the way, your method names are so out of the Java conventions that it makes it hard to read and undestand it. 顺便说一句,您的方法名称太不符合Java约定,以致于很难阅读和理解它。

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

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