简体   繁体   English

通过将对象作为参数传递在方法中使用对象

[英]Using an object in a method by passing it as a parameter

I'm facing a situation that involves class and methods.我面临着涉及类和方法的情况。

To make it simple, imagine you have 2 classes: A and B为了简单起见,假设您有 2 个类:A 和 B

In the class A, I have a method called, lets say, "jumpOnOtherClass".在 A 类中,我有一个名为“jumpOnOtherClass”的方法。 In the class B, I have another method called "doThings".在 B 类中,我有另一个名为“doThings”的方法。

I'd like the class A method to take as argument any object of any class, so that it first checks if the object is null (so it can creates a new instance of the object passed), and then execute a method of the object's class.我希望类 A 方法将任何类的任何对象作为参数,以便它首先检查对象是否为空(因此它可以创建传递的对象的新实例),然后执行对象的方法班级。

Here is an example code:这是一个示例代码:

class MyMainClass
{
  private A objectA = new A();
  private B objectB;

  public void main()
  {
    this.objectA.jumpOnOtherClass(this.objectB,"doThings");
  }
}

class A
{
  public void jumpOnOtherClass(Object objectFromAnyClass, String methodToInvoke)
  {
    if(objectFromAnyClass == null) objectFromAnyClass = new DefaultConstructorOfB();

    objectFromAnyClass.getMethod(methodToInvoke);
  }
}

class B
{
  public void doThings()
  {
    //Do some stuff here
  }
}

I know there are exception to take in consideration but for the example we'll assume that the class, and the methods exist and are found :)我知道有例外需要考虑,但对于这个例子,我们假设类和方法存在并被找到:)

I tried things with "java.lang.reflect" but it seems not to be the good way to achieve what I try to do...我尝试使用“java.lang.reflect”进行操作,但这似乎不是实现我尝试做的事情的好方法......

Any help would be really nice!任何帮助都会非常好!

Thanks and have a nice day!感谢,并有一个愉快的一天!

I'm not entirely sure I understand the question, but it looks like you may be looking for a way to differentiate between classes initially passed as Objects?我不完全确定我理解这个问题,但看起来您可能正在寻找一种方法来区分最初作为对象传递的类? You could use the instanceof keyword, and then risk some casting exceptions您可以使用 instanceof 关键字,然后冒一些铸造异常的风险

class MyMainClass
{
private A objectA = new A();
private B objectB;

public void main()
{
   this.objectA.jumpOnOtherClass(this.objectB,"doThings");
}
}

class A
{
  public void jumpOnOtherClass(Object objectFromAnyClass, String methodToInvoke)
  {
    if(objectFromAnyClass == null) objectFromAnyClass = new DefaultConstructorOfB();
    else if (objectFromAnyClass instanceof B) {
         if (methodToInvoke.equals("doThings")) 
            ((B) objectFromAnyClass).doThings();
    }
  }
}

class B
{
  public void doThings()
  {
    //Do some stuff here
  }
}

The only problem is that if the reference is null, you cannot really know the class of the "object" at runtime, so you will need to fix this by providing additional information.唯一的问题是,如果引用为空,则在运行时您无法真正知道“对象”的类,因此您需要通过提供附加信息来解决此问题。

I suggest this approach (it does involve reflection):我建议这种方法(它确实涉及反思):

public <T> void jumpOnOtherClass(T objectFromAnyClass, Class<T> classType, String methodToInvoke)
            throws
            NoSuchMethodException,
            IllegalAccessException,
            InvocationTargetException,
            InstantiationException
    {
        if(objectFromAnyClass == null) 
            objectFromAnyClass = classType.getDeclaredConstructor().newInstance();

        classType.getMethod(methodToInvoke).invoke(objectFromAnyClass);
    }

and use it like this:并像这样使用它:

jumpOnOtherClass(this.objectB, B.class, "doThings");

Ok!好的! I get it works finally!我终于明白了! So here is the final code:所以这是最终的代码:

class A
{
  public <T> void jumpOnOtherClass(T objectFromAnyClass, Class<T> classType, String methodToInvoke) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException
  {
    if(objectFromAnyClass == null)
    {
      println("instantiating");
      objectFromAnyClass = classType.newInstance();
    }

    println("getting the method");
    classType.getMethod(methodToInvoke).invoke(objectFromAnyClass);

    println("It works :D");
  }
}

static class B
{  
  public void doThings()
  {
    println("message from class B");
  }
}

The problem was actually coming from the class nesting made by processing (and in general, any class nesting).问题实际上来自于通过处理(通常是任何类嵌套)进行的类嵌套。 I removed the "getConstructor()" from the line where it was checking if the object b was null.我从检查对象 b 是否为空的行中删除了“getConstructor()”。

Maybe someone could explain under this post why does removing this method made everything working... I'm gonna try to understand and if I have something about that, it'll be posted down here!也许有人可以在这篇文章下解释为什么删除这种方法会使一切正常......我会试着理解,如果我有什么关于它的东西,它会被贴在这里!

Again, thanks a lot to you guys for helping :D再次,非常感谢你们的帮助:D

Have a nice day!祝你今天过得愉快!

Larry.K拉里·K

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

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