简体   繁体   English

调用Java中不带参数的方法

[英]Call a method without arguments in Java

I have a code like this: 我有这样的代码:

import com.eteks.sweethome3d.SweetHome3D;
import com.eteks.sweethome3d.model.Home;

public class Test extends SweetHome3D {
  public static void main(String [] args) {
    new Test().init(args);
  }

  @Override
  public Home createHome() {
    Home home = super.createHome();
    // Modify home as you wish here
    return home;
  }
}

and I want to run my method through main. 我想通过main运行我的方法。 Do you know how I can do that? 你知道我该怎么做吗? I have tried many ways like: 我尝试了很多类似的方法:

createHome();

super.createHome();

But it doesn't recognize this type. 但是它不能识别这种类型。 I also tried 我也试过

Test test = new Test();
  test.init(args);
  test.createHome();

which doesn't create any error, but still, what I want is not done. 这不会产生任何错误,但是我想要的仍然没有完成。 And they advised me "You shouldn't call test.createHome(); directly" 他们建议我“您不应直接调用test.createHome();”

Thanks in advance:) 提前致谢:)


The thing is that now it runs my application but when I put this 问题是现在它可以运行我的应用程序,但是当我把它放进去时

System.out.println("method called");

into my method, it is displayed twice...??? 进入我的方法,它显示两次... ??? Why? 为什么?

There is no problem in invoking test.createHome() , and it is the way it should work. 调用test.createHome()没问题,这是它应该起作用的方式。

In order to check whether the method is called you can put System.out.println("method called) in the createHome() method. 为了检查该方法是否被调用,您可以将System.out.println("method called)放在createHome()方法中。

Your problem probably lies somewhere else, perhaps in the superclass. 您的问题可能在其他地方,也许在超类中。

The problem is the difference between static methods and instance methods. 问题是静态方法和实例方法之间的区别。 createHome is an instance method which means you have to have an instance of that class to call that method. createHome是一个实例方法,这意味着您必须具有该类的实例才能调用该方法。 That's why you can call createHome() from your test variable because test is an instance of Test. 这就是为什么您可以从测试变量调用createHome()的原因,因为test是Test的一个实例。 The main method is a static method, and it's associated with Class Test and it doesn't have an instance of Test to invoke that method on. main方法是一个静态方法,它与Class Test相关联,并且没有Test实例来调用该方法。 A Class is a different piece of memory from the instances created from that Class. 类是与该类创建的实例不同的内存。 However, a Class can have methods and variables associated with it through the use of the static keyword. 但是,通过使用static关键字,Class可以具有与之关联的方法和变量。

Now why it's not working has probably more to do with your code and the assumptions it makes about when that method can be called. 现在,为什么它不起作用可能与您的代码以及它在何时可以调用该方法所做的假设有关。 I suppose the init() method is doing some rather large and it's not ready to handle invocations to createHome() because the system hasn't fully started. 我想init()方法正在做一些相当大的事情,并且由于系统尚未完全启动,因此尚不准备处理对createHome()的调用。 Looks like a UI program so there might be some issues with timing and processing events. 看起来像UI程序,所以在计时和处理事件上可能会出现一些问题。

It's hard to know why it's not working for you without specific errors. 很难知道为什么没有特定的错误对您不起作用。

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

相关问题 Java中方法调用和参数之间的求值顺序 - Evaluation order between a method call and arguments in Java 在Clojure中不带任何参数的方式调用Java实例方法 - Call Java instance method with no arguments in Clojure Java mono 用 arguments 调用 void 方法 - Java mono call void method with arguments Mockito java - 使用不同参数调用测试方法 - Mockito java - test method call with different arguments java中没有对象的调用方法? - call method without object in java? 不指定类型参数,不能将Java 8方法与lambda参数一起使用 - Cannot use Java 8 method with lambda arguments without specifying type arguments Java如何通过原始类型作为参数的反射来调用方法 - Java how to call method by reflection with primitive types as arguments 在这种传入方法调用和参数的情况下,是否保证Java评估顺序 - Is Java evaluation order guaranteed in this case of method call and arguments passed in 如何在Clojure中调用没有参数的Java静态方法? - How do I call a Java static method with no arguments in Clojure? 使用Luaile和LuaJava调用带有可变数字参数的Java方法 - Call a Java-method with a variable number arguments from a Luafile with LuaJava
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM