繁体   English   中英

从另一个 class 访问 class 方法?

[英]Acessing class methods from another class?

可以说我有这个:

主要的:

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

鼓:

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

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

木琴:

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

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

乐队:

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

我的问题是,如何在实例方法插入数组后访问它们? 因为我可以在将它们 go 放入数组之前访问它们。

我认为您混淆了班级的关注点并导致班级之间的耦合过多。

main,应该是创建的所有对象。 它将创建管弦乐队、鼓和木琴。 然后,Drum 和 Xylophone 实例将直接在 main 方法中添加到 Orchestra 实例。

鼓和木琴应该根本不知道管弦乐队class。

然后 Main 将可以访问 Orchestra 的playInstruments()方法。

旁注

我建议 Drum 和 Xylophone 都实现一个名为 Instrument 的接口,该接口具有一个名为play(String note)的方法。 因此,管弦乐队不必与特定的鼓和木琴耦合,它可以只有乐器,您可以稍后添加各种乐器,而无需每次都编辑您的基础管弦乐队 class。

例子

可能的新乐团 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"));
    }
}

仪器接口:

public interface Instrument {

    void play(String note);
}

鼓:

public class Drum implements Instrument {

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

木琴:

public class Xylophone implements Instrument {

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

最后,主要:

public class Main {

    public static void main(String[] args) {

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

暂无
暂无

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

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