简体   繁体   English

如何在处理/ Java Eclipse项目中没有构造函数的情况下访问其他类中的主类对象?

[英]how to access main class objects in other classes without a constructor in a processing/java eclipse project?

I cant seem to find an answer on the internet anywhere. 我似乎无法在任何地方的互联网上找到答案。

So I have a processing project in Eclipse 所以我在Eclipse中有一个处理项目

I want to be able to access objects in my "main" class that extends PApplet in another class without having to pass them through the constructor. 我希望能够访问在另一个类中扩展PApplet的“主”类中的对象,而不必通过构造函数传递它们。 I am not the most experienced programmer so my terminology might be off but hopefully this example clarifies what I am trying to do. 我不是最有经验的程序员,因此我的术语可能不正确,但是希望该示例可以阐明我的工作意图。

so for 因此对于


public class Main Extends PApplet{

    //Example Class to access objects from main
    Interaction interaction;   

    //Example Objects I want
    boolean someObject;

    void setup(){        
        someObject = true;
        Interaction = new Interaction();  
    }

    void draw(){
    }   
}

public class Interaction{

    PApplet p;

    public Interaction(PApplet parent){
        p = parent;
    }

    public void mousePressed(){
        if(someObject){
            //do something
        }   
    }
}  

so I know I could pass that object into the constructor of Interaction like 所以我知道我可以将该对象传递给Interaction的构造函数,例如

PApplet p;
boolean o;

public Interaction (PApplet parent, boolean SomeObject){
    p = parent;
    o = someObject;
}

but this gets a little crazy in an example like this where I just want to hold all of my mouse and keyboard interaction in its own class because its getting huge, but I have run into the need to this time and time again and cant seem to figure it out. 但这在这样的示例中有点疯狂,我只是想将所有的鼠标和键盘交互保持在自己的类中,因为它的规模越来越大,但是我一次又一次地遇到了这种需求,而且似乎无法想办法。

What you are describing is called a getter method. 您所描述的称为getter方法。

You can add a getter method to your Main , but first read this -> http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html 您可以在Main添加一个getter方法,但首先请阅读-> http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html

Here is the example. 这是例子。

public class Main Extends PApplet {

    ...

    public boolean getSomeObject ( )
    {
        return someObject;
    }
}

public class Interaction {

   PApplet p;

   public Interaction(PApplet parent){
      p = parent;
   }

   public void mousePressed() {

       if( p.getSomeObject( ) ) {
           //do something
       }

   }
}

BTW, boolean is not an Object in Java, Boolean is. 顺便说一句, boolean不是Java中的ObjectBoolean是。

EDIT After your comment I DO understand what you need. 编辑在您发表评论后,我确实了解您的需求。

You need to create an interface and pass this interface to Interaction constructor. 您需要创建一个interface并将此接口传递给Interaction构造函数。

This interface should provide all the method that your Interaction needs. 该界面应提供Interaction所需的所有方法。 If it needs an access to PApplet , then that's what your interface should provide. 如果需要访问PApplet ,则这是您的界面应提供的。

With this in mind, here is a new hierarchy: 考虑到这一点,这是一个新的层次结构:

public interface IInteractionContext
{
  boolean getSomeObject( );
}

public class Main
  extends PApplet
  implements IInteractionContext
{
  @Override
  public boolean getSomeObject ( )
  {
    return someObject;
  }

  ...
}

public class Main Extends PApplet {

    ...

    public boolean getSomeObject ( )
    {
        return someObject;
    }
}

public class Interaction {

   final IInteractionContext ctx;

   public Interaction(IInteractionContext ctx)
   {
      this.ctx = ctx;
   }

   public void mousePressed() {

       if( ctx.getSomeObject( ) )
       {
           //do something
       }

   }
}

With these changes, Interaction could care less if ctx is Main , PApplet or Unicorn . 通过这些更改,如果ctxMainPAppletUnicorn ,则Interaction可能会不太PApplet Its constructor requests the expected behavior and your implementation of Main provides this behavior at runtime. 它的构造函数请求预期的行为,而Main的实现在运行时提供了此行为。

In any case read the javaworld article and related articles. 无论如何,请阅读javaworld文章和相关文章。 Also, read articles about Dependency Injection 另外,请阅读有关Dependency Injection文章

An applet is basically a top level container for an application that provides a context for user interaction GUI/Display and input (mouse clicks, keyboard). 小程序基本上是应用程序的顶级容器,它为用户交互GUI /显示和输入(鼠标单击,键盘)提供了上下文。

In a well designed application, your business logic should be encapsulated in one or more classes, and you would call methods on this based on external input and then perhaps query it for information for output to the GUI. 在设计良好的应用程序中,您的业务逻辑应封装在一个或多个类中,并且您将基于外部输入在此方法上进行调用,然后可能会查询它的信息以输出到GUI。 This is very much a generalization BTW. 这非常笼统。

The top level container (in this case, the Applet) should only be concerned with instantiating your business objects, capturing user input, calling methods on the business objects and then updating the GUI based on the state of the business objects. 顶级容器(在本例中为Applet)只应与实例化业务对象,捕获用户输入,在业务对象上调用方法,然后根据业务对象的状态更新GUI有关。

I'm guessing that you feel compelled to wrap all of your interaction code into a class to keep it clear for the business logic. 我猜想您会被迫将所有交互代码包装到一个类中,以使业务逻辑清晰明了。 If this is the case, it is the business logic that should be encapsulated, not spread through the Applet class. 在这种情况下,应该封装业务逻辑,而不是通过Applet类进行传播。 This would allow you to easily transfer your business object to a desktop application or even run it without a GUI at all. 这将使您可以轻松地将业务对象转移到桌面应用程序,甚至完全不使用GUI即可运行它。

暂无
暂无

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

相关问题 在eclipse中如何创建没有Java项目的类? - In eclipse how to create a class without a java project? Java主类和子类-Eclipse - Java main class & sub classes - Eclipse 如何将主类中的对象添加到其他类Java中的ArrayList - how to add the objects from main class to ArrayList in other class Java 如何在eclipse中显示java类从其他类调用方法 - How to display java classes in eclipse calling a method from other class 如何在main()中创建每个其他类都可以访问的对象? (Java) - How to create an object in main() that every other class can access? (Java) eclipse java 内容辅助不适用于特定的 class,适用于项目中的其他类 - eclipse java content assisst doesn't work for particular class, work for other classes in project Java:使 package 级别的构造函数可用于外包装。 仅允许项目内的某些类访问构造函数 - Java: make package level constructor in a class accessible for outer packages. Allow only certain classes inside project access to constructor 如何将对象从我的 Main 类传递到其他类 - How do I pass objects along from my Main class to other classes 如何为具有多个参数的类创建对象数组,并在其他类中访问对象(及其值)? - How to create Array of Objects for Class with Multiple Parameters and Access objects (and their values) in other classes? 如何将主Java类转换为构造函数? - How to turn a main Java class into a constructor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM