Home > Blockchain >  Acessing class methods from another class?
Acessing class methods from another class?

Time:12-05

Lets say I have this:

Main:

public class Main {
    public static void main(String[] args) {
        Orchestra orchestra = new Orchestra();

        Drum drum = new Drum();
        Xylophone xylophone = new Xylophone();
        //----------------------------------
        drum.sendToOrchestra();
        xylophone.sendToOrchestra();
    }
}

Drum:

public class Drum{
    public void play(String note){
        System.out.println("Playing... drums (note "   note   ")");
    }

    public void sendToOrchestra(){
        Orchestra orchestra = new Orchestra(this);
    }
}

Xylophone:

public class Xylophone{
    public void play(String note){
        System.out.println("Playing... xylophone (note "   note   ")");
    }

    public void sendToOrchestra(){
        Orchestra orchestra = new Orchestra(this);
    }
}

Orchestra:

public class Orchestra {
    static Object[] instrumentsArray = new Object[2];

    public Orchestra(){

    }
    public Orchestra(Xylophone xylophone){
        // this works: xylophone.play()
        instrumentArray[0] = xylophone;
        // but this does not: instrumentsArray[0].play()
    }
    public Orchestra(Drum drum){
        // this works: drum.play()
        instrumentArray[1] = drum;
        // but this does not: instrumentsArray[1].play()
    }

    public void playInstruments(){
        // here is where I will iterate through the instrumentsArray, and make all elements inside it: .play()
    }
}

My question would be, how can access the instances methods, after they are inserted into the array? Because I can access them before they go into the array.

CodePudding user response:

I would argue that you have mixed up your class' concerns and have caused too much coupling between your classes.

Main, should be to create all the objects. It would create the Orchestra, Drum, and Xylophone. The Drum and Xylophone instances would then be added to the Orchestra instance directly in the main method.

The Drum and Xylophone shouldn't know about the Orchestra class at all.

Then Main will have access to the playInstruments() method of Orchestra.

Side Notes

I would recommend that both Drum and Xylophone implement an interface called Instrument that has one method called play(String note). So then the Orchestra doesn't have to be coupled to specifically Drums and Xylophones, it could just have Instruments and you could add all kinds of Instruments later without every having to edit your base Orchestra class.

Examples

Possible new Orchestra class:

import java.util.ArrayList;
import java.util.List;

public class Orchestra {

    private List<Instrument> instruments;

    public Orchestra() {
        this.instruments = new ArrayList<>();
    }

    public Orchestra(List<Instrument> instruments) {
        this.instruments = instruments;
    }

    public void add(Instrument instrument) {
        this.instruments.add(instrument);
    }

    public void play() {
        this.instruments.forEach(i -> i.play("b flat"));
    }
}

Instrument interface:

public interface Instrument {

    void play(String note);
}

Drum:

public class Drum implements Instrument {

    @Override
    public void play(String note) {
        System.out.println("Drums: "   note);
    }
}

Xylophone:

public class Xylophone implements Instrument {

    @Override
    public void play(String note) {
        System.out.println("Xylophone: "   note);
    }
}

And finally, Main:

public class Main {

    public static void main(String[] args) {

        Orchestra orchestra = new Orchestra();
        orchestra.add(new Drum());
        orchestra.add(new Xylophone());
        orchestra.play();
    }
}
  • Related