简体   繁体   English

A类实例化B类实例。 ClassB的实例如何调用A类的方法?

[英]Class A instantiates a Class B instance. How can the Instance of ClassB call a method of class A?

I have 2 classes, and I have made one class (Class A) instantiate a Class B object. 我有2个类,并且已经使一个类(A类)实例化了B类对象。

I have a method in Class B that I want to call a method in Class A. 我在B类中有一个方法要在A类中调用一个方法。

I'm working on a larger project for practicing Java, so I am simplifying things here. 我正在从事一个更大的项目来练习Java,因此我在这里简化了事情。

// Class A: // A类:

public class ClassA {
  private int number;
  private ClassB instanceOfB = new ClassB();

  public ClassA {
    number = 0;
  }

  public void incrementNumber {
    number++;
  }

  public void incrementNumberLongWay {
    instanceOfB.incrementNumberInA()
  }
} 

// Class B: // B类:

public class ClassB {
  public void incrementNumberInA() {
    // My desire: Call Class A's incrementNumber method
    // What should I put here?
   }

} }

How do I make sure incrementNumberLongWay works? 如何确定crementNumberLongWay有效? Class A has been instantiated, and it's method incrementNumberLongWay is called, so this should call ClassB's method incrementNumberInA 类A已被实例化,并调用了methodIncrementNumberLongWay方法,因此应调用ClassB的方法IncrementNumberInA

I know this seems extremely convoluted, but the reason I'm doing this, is because in my program I'm not incrementing numbers, but instead doing some logic in Class B, and only wanting to affect Class A in certain cases. 我知道这似乎非常令人费解,但是我这样做的原因是因为在我的程序中我没有增加数字,而是在B类中进行了一些逻辑运算,并且只想在某些情况下影响A类。

You can't do this with the code provided. 您无法使用提供的代码执行此操作。 Relationships are by default one way. 关系默认是一种方式。 B doesn't know about A so cannot access it. B不了解A,因此无法访问它。

What you can do is pass a reference of A to B in it's construction process and then access A via that reference. 您可以做的是在构建过程中将A的引用传递给B,然后通过该引用访问A。

ClassB doesn't know anything about ClassA. ClassB对ClassA一无所知。 So, you couldn't do it. 所以,你做不到。 The ugly decision is 丑陋的决定是

public void incrementNumberLongWay() {
        instanceOfB.incrementNumberInA(this);
    }

and in 和在

public class ClassB {
    public void incrementNumberInA(ClassA cl) {
        cl.incrementNumber();
    }
}

You can't call methods from class A from class B as class B has no reference to an object of class a. 您不能从类B调用类A的方法,因为类B没有引用类a的对象。 You could, however, pass class A's current number state to class B as parameter, and return a value from class B which class A can then get and use. 但是,您可以将A类的当前数字状态作为参数传递给B类,然后从B类返回一个值,然后A类可以获取和使用它。

For example: 例如:

public class A {
    private int number;
    public A(int number) {
        this.number = number;
    }
    public void incrementNumber(boolean largeIncrement) {
        if(largeIncrement) {
            B bInstance = new this.B();
            number = bInstance.incrementNumberLongWay(number);
        }
        else {
            number++;
        }
    }
    private class B {
        private B() {
            // if some initialization is needed...
        }
        public int incrementNumberLongWay(int num) {
            num += 1000;
            return num;
        }
    }
}

Hope this is what you wanted. 希望这就是您想要的。

One solution would be to pass a method of A as a callback. 一种解决方案是传递A方法作为回调。

For example: 例如:

public class ClassA {
  private int number;
  private ClassB instanceOfB = new ClassB();

  public ClassA {
    number = 0;
  }

  public void incrementNumber {
    number++;
  }

  public void incrementNumberLongWay {
    instanceOfB.incrementNumberInA(this::increment);
    // alternatively
    // instanceOfB.incrementNumberInA(() -> incrementNumber());
  }
} 

public class ClassB {
  public void incrementNumberInA(Runnable callbackMethod) {
    callbackMethod.run();
  }
}

This removes B's dependency on A, and instead allows a general callback mechanism. 这消除了B对A的依赖,而是允许使用通用的回调机制。

However, for such a simple scenario this approach isn't advised. 但是,对于这种简单的情况,不建议使用此方法。

It's probably a bad idea in general to have a circular dependency in this way. 以这种方式具有循环依赖关系通常可能是一个坏主意。 One approach to break the cycle would be to have a third class (classC?) that implements the increment logic (or whatever your real-world equivalent is), and have classA and classB instances each reference classC. 打破周期的一种方法是让第三类(classC?)实现增量逻辑(或与您的实际等效的任何东西),并让classA和classB实例各自引用classC。 That way there's no case where two classes know about each other. 这样,就不会有两个类彼此了解的情况。

暂无
暂无

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

相关问题 Java-当类A调用类B时,在类B中如何使用类A的方法和属性? - Java- When class A call the classB, in class B how can I use the method and attribute of classA? 如果类B的实例是类A的成员,那么当按下类B中的按钮时,类B如何调用类A的方法? - If an instance of class B is a member of class A, how can class B call a method of class A when a button in class B is pressed? 如何在实例类中的实例上调用方法? - How can I call a method on an instance within the instance class? 一个类的调用方法以使用另一个类的实例 - Call method of a class to use instance of another class 如何调用在外部类的方法内部定义的类的实例 - How to call an instance of a class defined inside a method of an outer class 如何通过另一个 class 的方法调用实例变量的 class - How to call a class of instance variables through a method of another class 如何从Android中的另一个类调用预定义类中的实例方法 - How to call instance method in predefined class from another class in android 如何实现OOP来从一个类调用一个实例方法以存储在另一个类中另一个实例方法的数组中? - How can OOP be implemented to call an instance method from one class to be stored in an array in another instance method in a different class? 如何将包含可包裹类“ B”的实例的可包裹类“ A”的实例写入类“ B”的包裹 - How to write an instance of Parcelable class 'A' which contains an instance of parcelable class 'B', to Parcel from class 'B' 无法调用派生内部类的实例方法 - Can't call instance method of derived inner class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM