简体   繁体   English

在Java中3个类中的另一个类中调用方法

[英]Call a method in another class within 3 classes in java

I've 3 classes in one package. 我在一个包中有3个班级。 The first class ( ClassStart ) generates each an instance of the 2 other classes ( ClassA and ClassB ). 第一个类( ClassStart )分别生成其他两个类( ClassAClassB )的实例。 I want to call in ClassB a method of ClassA by means of its instance "a". 我想通过实例“ a”在ClassB调用ClassA的方法。
Though the scope of Instance "a" is the package (because of the attribute "ClassA a;" in ClassStart the line "a.showText()" doesn't work. It gets the error message "a cannot resolved" . So I tried "sashowText()" but it doesn't work because the instance "s" was generated in a static method and I don't know how to access to "s". 尽管实例“ a”的作用域是包(由于ClassStart中的属性"ClassA a;" ,行"a.showText()"不起作用。它得到了错误消息"a cannot resolved" 。所以我尝试过"sashowText()"但是它不起作用,因为实例“ s”是在静态方法中生成的,并且我不知道如何访问“ s”。

The first class (contains the main-method): 第一类(包含主方法):

public class ClassStart {
    ClassA a;

    public static void main(String[] args) {
    ClassStart s = new ClassStart();
    }

    public ClassStart() {
    a = new ClassA();
    ClassB b = new ClassB();
    }
}

The second class: 第二类:

public class ClassA {

    public void showText() {
    System.out.println("This text comes from ClassA.");
    }
}

The third class: 第三类:

public class ClassB {

    public ClassB() {
    a.showText();
    }
}

How can I call in ClassB the method "showText()" of the ClassA? 如何在ClassB中调用ClassA的方法“ showText()”?
(I had looked for answers in this forum but I didn't find a answers for a three class problem like this.) Thank you for answer. (我在这个论坛中一直在寻找答案,但是对于这样的三类问题,我没有找到答案。)谢谢您的回答。

If the ClassA object needs to be the same throughout, then pass it into B: 如果ClassA对象始终需要相同,则将其传递给B:

public class ClassB {
   private ClassA a;

   // pass the ClassA reference into the ClassB constructor
   public ClassB(ClassA a) {
     this.a = a;   // assign it to the a field
     // a.showText();  // or you can do this if you need it called in the constructor
   }

   // or do this if you want the a method called in a ClassB method.
   public void callAShowText() {
     a.showText();
   }
}

then: 然后:

public class ClassStart {
    ClassA a;

    public static void main(String[] args) {
       ClassStart s = new ClassStart();
    }

    public ClassStart() {
       a = new ClassA();  // create your ClassA instance
       ClassB b = new ClassB(a);  // pass it into your ClassB instance
       b.callAShowText();
    }
}

The key bit of understanding here is to understand reference types and reference variables. 理解的关键是了解引用类型和引用变量。 You want a reference variable in ClassB to refer to the ClassA object created in ClassStart. 您希望ClassB中的引用变量引用在ClassStart中创建的ClassA对象。 One way to do that is to pass the object into ClassB either in its constructor or in a setter method. 一种方法是将对象通过其构造函数或setter方法传递给ClassB。 Once you've done that, then ClassB has the reference it needs and it can call any ClassA method on the instance. 完成此操作后,ClassB将拥有所需的引用,并且可以在实例上调用任何ClassA方法。

Note that you can also "solve" this by creating and using a public static ClassA variable or a public static showText() method, but in general you will try to avoid doing this since while it would work fine in a simple example like this, it doesn't "scale" well, meaning if used generally in larger more complex programs, it will risk increasing the potential connections in your complex program, greatly increasing the risk of bugs. 请注意,您还可以通过创建和使用公共静态ClassA变量或公共静态showText()方法来“解决”此问题,但是通常,您会避免这样做,因为尽管在这样的简单示例中它可以正常工作,它不能很好地“缩放”,这意味着,如果通常将其用于较大的更复杂的程序中,将有增加复杂程序中潜在连接的风险,从而大大增加了发生错误的风险。 It was for this reason, to decrease complexity and decrease connectedness (decrease coupling) that object oriented programming was created. 出于这个原因,为了降低复杂性并减少连接性(减少耦合),创建了面向对象的编程。

Make the method static: 将方法设为静态:

public static void showText()

Then call it: 然后调用它:

ClassA.showText();

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

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