简体   繁体   中英

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. 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 .

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.

Just use composition: that is make Bloblo an attribute of 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.

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

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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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