【问题标题】:Acessing class methods from another class?从另一个类访问类方法?
【发布时间】:2022-12-05 00:48:49
【问题描述】:

可以说我有这个:

主要的:

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

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

【问题讨论】:

  • 你的乐器类应该只有 play 方法。您的主类应该负责实例化 Orchestra 类、乐器类,并将乐器实例添加到 orchestra 类。

标签: java methods


【解决方案1】:

您需要将 instrumentsArray 中的对象转换回它们的原始类型,以便能够调用它们的方法。例如,在 Orchestra 类中,您可以这样做:

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

    public Orchestra() {}

    public Orchestra(Xylophone xylophone){
        // Cast the object back to a Xylophone
        Xylophone x = (Xylophone) xylophone;
        // Now you can call the play method on the xylophone object
        x.play("C");

        // Store the xylophone in the array
        instrumentsArray[0] = xylophone;
    }
    public Orchestra(Drum drum){
        // Cast the object back to a Drum
        Drum d = (Drum) drum;
        // Now you can call the play method on the drum object
        d.play("D");

        // Store the drum in the array
        instrumentsArray[1] = drum;
    }

    public void playInstruments(){
        // Iterate through the instruments in the array
        for (int i = 0; i < instrumentsArray.length; i++) {
            Object instrument = instrumentsArray[i];

            // Check the type of the instrument and cast it to the appropriate type
            if (instrument instanceof Xylophone) {
                Xylophone x = (Xylophone) instrument;
                // Now you can call the play method on the xylophone object
                x.play("C");
            } else if (instrument instanceof Drum) {
                Drum d = (Drum) instrument;
                // Now you can call the play method on the drum object
                d.play("D");
            }
        }
    }
}

这样,插入数组后,就可以访问 instrumentsArray 中对象的方法。

【讨论】:

    【解决方案2】:

    我会争辩说你混淆了你的课程问题并且导致你的课程之间有太多的耦合。

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

    Drum and Xylophone 应该根本不知道 Orchestra 类。

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

    旁注

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-21
      • 2017-05-12
      • 2017-01-03
      • 2012-03-19
      • 2014-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多