简体   繁体   English

来自已加载类的Java Call类函数

[英]Java Call class function from loaded class

How would I, if possible, call a function in my Main class in the class that I load? 如果可能的话,如何在我加载的类的Main类中调用函数? (I know that is impossible to understand, so I'll explain) (我知道这是不可能理解的,所以我会解释)

IE: IE浏览器:

public class SomeClass
{
     public SomeClass
     {
          //load a class here
     }

     public void FuncToCall() {}
}

//In a separate file, dynamically loaded.
public class LoadedClass
{
     public LoadedClass
     {
          //Call a function in the class that loads this
          SomeClass.FuncToCall();
     }
}

So I would end up having 2 files: SomeClass.class and LoadedClass.class. 因此,我最终将拥有2个文件:SomeClass.class和LoadedClass.class。 I guess my main question is, how do I reference SomeClass in LoadedClass? 我想我的主要问题是,如何在LoadedClass中引用SomeClass?

**EDIT: **编辑:

So maybe I should better explain the use case. 因此,也许我应该更好地解释用例。 A java program dynamically loads the "SomeClass" script from a folder. Java程序从文件夹动态加载“ SomeClass”脚本。 The script then downloads a .jar file from the internet and opens and runs the "LoadedClass" script within that. 然后,该脚本从Internet下载一个.jar文件,并在其中打开并运行“ LoadedClass”脚本。 How do I use functions in SomeClass in LoadedClass if SomeClass isn't in the same .jar or in a .jar at all? 如果SomeClass不在同一.jar或.jar中,该如何在LoadedClass的SomeClass中使用函数?

You just do it.. 你就去做

If they are in different packages you have to either import the class or use the fully qualified name. 如果它们在不同的程序包中,则必须导入类或使用完全限定的名称。

Let me see if I get it. 让我看看我是否明白。

Check this running sample with modifications to your source code: 通过修改源代码来检查此运行示例:

C:\Users\oreyes\java>type SomeClass.java LoadedClass.java Main.java
//SomeClass.java
public class SomeClass{
     public SomeClass () /* added () */ {
          //load a class here
          LoadedClass lc = new LoadedClass( this );
     }
     public  void funcToCall() {
         System.out.println("SomeClass.functToCall: Being invoked :)");
     }
}
//LoadedClass.java
//In a separate file, dynamically loaded.
public class LoadedClass {
     public LoadedClass( SomeClass sc )/*added () */ {
          //Call a function in the class that loads this
          //SomeClass.funcToCall();
          sc.funcToCall();
     }
}
//Main.java
//
public class Main {
    public static void main( String ... args ) {
        new SomeClass();
    }
}
C:\Users\oreyes\java>javac SomeClass.java LoadedClass.java Main.java
C:\Users\oreyes\java>java Main
SomeClass.functToCall: Being invoked :)
C:\Users\oreyes\java>

What a did was to pass a reference of the loader class into the loaded, and from there...just invoke the method. 所做的是将加载程序类的引用传递到已加载的文件中,然后从那里...仅调用该方法。

I hope this helps. 我希望这有帮助。

EDIT 编辑

As per your comment it looks like what you need is to use Reflection 根据您的评论,看来您需要使用反射

You do it right now. 您现在就做。

We call it --> Recursivity (if you call the same function from the function you call) Or simply.. a redirection.. You learn little by little what is the POTENTIAL of the programming :D 我们称它为->递归(如果您从调用的函数中调用相同的函数),或者简单地..重定向..您会逐渐了解编程的潜力:D

Welcome to StackOverflow :D Good Luck! 欢迎来到StackOverflow:D祝您好运!

because if you call a class from another class, you need to create an object before. 因为如果您从另一个类中调用一个类,则需要先创建一个对象。 (OOP) Example: (OOP)示例:

Class X{ public functionX(){}}
Class Y { 
   X object = new X(); //Create an object to access to the function X
    public functionY(){ 
             object.functionX(); //you call the function from Class X
    }
}

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

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