首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >处理来自另一个类的类方法?

处理来自另一个类的类方法?
EN

Stack Overflow用户
提问于 2022-12-04 16:38:14
回答 1查看 38关注 0票数 0

我要说的是:

Main:

代码语言:javascript
复制
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();
    }
}

鼓:

代码语言:javascript
复制
public class Drum{
    public void play(String note){
        System.out.println("Playing... drums (note " + note + ")");
    }

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

木琴:

代码语言:javascript
复制
public class Xylophone{
    public void play(String note){
        System.out.println("Playing... xylophone (note " + note + ")");
    }

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

管弦乐队:

代码语言:javascript
复制
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()
    }
}

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-12-04 16:46:51

我认为您混淆了类的关注点,并在类之间造成了太多的耦合。

主,应该是创建所有的对象。它将创建管弦乐队、鼓和木琴。然后,鼓和木琴实例将直接添加到管弦乐团实例的主要方法。

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

然后,Main将访问playInstruments()方法的管弦乐团。

旁注

我建议鼓和木琴都实现一个名为that的接口,该接口有一个名为play(String note)的方法。所以管弦乐团不需要与特定的鼓声和木琴相连,它只需要有乐器,你以后就可以添加各种乐器,而无需编辑你的基础管弦乐队的类。

示例

可能的新管弦乐团班:

代码语言:javascript
复制
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"));
    }
}

仪器接口:

代码语言:javascript
复制
public interface Instrument {

    void play(String note);
}

鼓:

代码语言:javascript
复制
public class Drum implements Instrument {

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

木琴:

代码语言:javascript
复制
public class Xylophone implements Instrument {

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

最后,Main:

代码语言:javascript
复制
public class Main {

    public static void main(String[] args) {

        Orchestra orchestra = new Orchestra();
        orchestra.add(new Drum());
        orchestra.add(new Xylophone());
        orchestra.play();
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74678669

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档