如何从不同的文件中调用观察者方法?

huangapple go评论66阅读模式
英文:

How to call an Observer method from a different file?

问题

Sure, here's the translated code portion:

我是Java的新手,一直在尝试编写一个从另一个文件调用的程序。假设我有文件A.java,并且它有一个观察者方法

...

//在Item.java中

public class Item{
    private String name;
     
    //带参数的构造函数
    public Item(String name){
        this.name = name;    
    }

    public String getName(){
        return this.name;
    }
}

假设在文件B.java中,我想要打印出所有Item的名称,我认为代码应该是这样的

//在Bag.java中
public class Bag {
    private int size;

    public void print(){
        // 这样是不起作用的 
        System.out.println(Item.getName());
    }
}

但这并没有起作用。我无法更改print方法的名称。有没有其他方法可以实现这个目标?

目标:抱歉如果我没有表达清楚,目标是打印出物品的名称。假设我们已经将一个物品传递给了Item.java类,你将如何打印出该物品?

(Note: The translation might not be perfect due to differences in programming terminology and context between English and Chinese.)

英文:

I'm new to Java and have been trying to program a file that calls from another file. Lets say I have file A.java and it has an observer method

...

//in Item.java

public class Item{
    private String name;
     
    //parametrized cons
    public Item(String name){
        this.name = name;    
    }

    public String getName(){
        return this.name;
    }
}

And lets say in file B.java I want to print all Item.names, I thought it would be something like this

//in Bag.java
public class Bag {
    private int size;

    public void print(){
        // this does not work 
        System.out.println(Item.getName());
    }
}

But this didn't work. I can't change the method (name) of print at all. Is there another way this can be done?

Goal: Sorry If I am not being clear, the goal is to print the name of an item. Assuming we passed an item to the Item.java class already, how would you print that item?

答案1

得分: 1

在您提供的代码中,并不清楚 B 如何知道 A,但您可以在创建 new B() 时通过构造函数将其传递进去。

public class B {
  private A a;

  public B(A a) {
     this.a = a;
  }  

  public void print() {
    System.out.println(this.a.getName());
  }
}

或者您可以在 B 中定义一个静态方法,接受一个 A 实例作为参数。

public static void print(A a) {
   System.out.println(a.getName());
}

然后调用 B.print(a_instance)

英文:

Not clear how B would know about A in your given code, but you can pass it in when you create a new B() via the constructor

public class B {
  private A a;

  public B(A a) {
     this.a = a;
  }  

  public void print() {
    System.out.println(this.a.getName());
  }
}

Or you can define a static method in B that accepts an A instance

public static void print(A a) {
   System.out.println(a.getName());
}

And call B.print(a_instance)

答案2

得分: 0

//在B.java中
public void print(){
    System.out.println(new A().getName());
}
英文:
//in B.java
public void print(){
    System.out.println(new A().getName());
}

huangapple
  • 本文由 发表于 2020年9月17日 04:18:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63927403.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定