简体   繁体   English

如何从另一个类方法调用已创建对象的方法?

[英]How to call a method of an already created object from another class method?

I am trying a small program where I have two classes with overloaded constructors. 我正在尝试一个小程序,其中有两个带有重载构造函数的类。 At first, I create an object of the first class passing to it's constructor an integer. 首先,我创建第一个类的对象,该对象将其整数传递给其构造函数。 Then I create an object of the second class passing to it's constructor a string. 然后,我创建第二个类的对象,并将其传递给构造函数一个字符串。

The class Blabla contains a run() method that should call the method afficher of the Bloblo object t already created in the main method. 类BLABLA包含应该调用的主要方法已创建Bloblo对象T的方法afficher一个run()方法。 However, I am having trouble doing this since the constructor is overloaded and I should pass some argument. 但是,由于构造函数已重载,所以我很难这样做,我应该传递一些参数。 And the Blabla object ts doesn't know the value initially passed to the Bloblo constructor: 2345 . 而且Blabla对象ts不知道最初传递给Bloblo构造函数的值: 2345

Here is an example so I make myself clear: 这是一个示例,因此我很清楚:

public class Trial {

  public static void main(String[] args) {

            Bloblo t = new Bloblo(2345);
            Blabla ts = new Blabla("Imad");
            ts.run();
          }
}

public class Bloblo {
        private int port;
        public Bloblo(int leport)
        {
            port = leport;
            System.out.println("au debut le port est: " + port);
        }
        public void afficher(String nom)
        {
            System.out.println("on va afficher dans BLOBLO: " + nom + "\net le port est: "+ port);

        }
}

public class Blabla implements Runnable{

        String Name = "";

        public Blabla(String nom)
        {
            Name = nom;
        }

        public void run()
        {
            System.out.println("voici le nom: " + Name);
            Bloblo obj = new Bloblo();
            obj.afficher(Name);
        }
}

The idea here is that i cannot create the instance obj because I have to give port as parameter and I don't know what port it is. 这里的想法是我无法创建实例obj,因为我必须将port作为参数,并且我不知道它是哪个port。

Just use composition: that is make Bloblo an attribute of Blabla. 只需使用组合即可:这使Bloblo成为Blabla的属性。

public class Blabla implements Runnable{

    String name = "";
    Bloblo obj;

    public Blabla(Bloblo obj, String name)
    {
        this.name = name;
        this.obj = obj
    }

    public void run()
    {
        System.out.println("Here is the name: " + name);
        obj.display(name);
    }
}

public class Trial {

    public static void main(String[] args) {

        Bloblo t = new Bloblo(2345);
        Blabla ts = new Blabla(t, "Imad");
        ts.run();
    }
}

You should pass the reference to your BloBlo into the BlaBla constructor and store it in a field. 您应该将对BloBlo的引用传递到BlaBla构造函数中,并将其存储在字段中。

public class Trial {

  public static void main(String[] args) {

            Bloblo t = new Bloblo(2345);
            Blabla ts = new Blabla("Imad", t);
            ts.run();
          }
}

public class Bloblo {
        private int port;
        public Bloblo(int leport)
        {
            port = leport;
            System.out.println("au debut le port est: " + port);
        }
        public void afficher(String nom)
        {
            System.out.println("on va afficher dans BLOBLO: " + nom + "\net le port est: "+ port);

        }
}

public class Blabla implements Runnable{

        String Name = "";
        Bloblo bloblo;
        public Blabla(String nom, Bloblo b)
        {
            Name = nom;
            bloblo = b;
        }

        public void run()
        {
            System.out.println("voici le nom: " + Name);

            bloblo.afficher(Name);
        }
 }

You have at least two options, Either you can pass a port to BlaBla or you can provide an instantiated BloBlo 您至少有两个选择,要么可以将端口传递给BlaBla,要么可以提供实例化的BloBlo

public class Blabla implements Runnable{

    String Name = "";
    BloBlo myBloBLo;

    public Blabla(String nom, int leport)
    {
        Name = nom;
        myBloBlo = new Bloblo(leport);
    }

    public Blabla(String nom, Bloblo bloblo)
    {
        Name = nom;
        myBloBlo = bloblo;
    }


    public void run()
    {
        System.out.println("voici le nom: " + Name);
        myBloBLo.afficher(Name);
    }
}

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

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