【问题标题】:Pass values from a constructor to a method将值从构造函数传递给方法
【发布时间】:2016-04-04 01:29:00
【问题描述】:

我必须传递 Guitar 构造函数中的值,然后将它们传递给 generateSong 方法。有一个问题。生成歌曲方法不能带参数,因为值在同一个类中,它们应该能够从构造函数中获取值。

当我尝试在该方法中实现值时,无法访问这些值。我该如何解决这个问题?

public Guitar(int chord, int numOfStrings)  {
        //pass these values to the generateSong method
        System.out.println("Guitar () generated a guitar with: " + numOfStrings + "." + "Song length is " +chord);
        // declare the array for which the song is stored 
        // store the values of the last row i (highest index of the array)
        double[] max = null;
        Guitar.generateSong();
        // start the generate song method 
        public static void generateSong () {
            double [] [] song = new double [numOfStrings] [chord];
            double[] max;
            int findmax =0;
            int sum =0;
            for (int i =0; i<song.length; i++) {
                for (int j =0; j<song.length; j++ ) {
                    sum = (int) (sum +song[i][j]);
                }
                for (int e=0; e<song.length; e++) {
                    if (song[e]!=song[findmax] ) {
                        max[e] =e;
                    }
                    for(int k=0; k<song.length; k++) {
                        for (int l=0; k<song[k].length; k++)
                            // assign note values to the array
                            song [k] [l] = 27.5 + (Math.random()* 4186); 
                        // print out the proper value 
                        System.out.printf("8.2%f", song); 
                        for( int m=0; m<max.length; m++)
                            max[m]= 1 +( Math.random() *3);
                        System.out.printf("8.2%f", max);
                    }
                }
            }
        }

int chordint numOfStrings 的值来自 main 方法中的命令行参数,并通过以下对象传递给该方法:

Guitar guitar = new Guitar (numOfStrings, chord); 

【问题讨论】:

    标签: java methods constructor


    【解决方案1】:

    因此,有两个语法错误和一个对要求的误解。让我们从误解开始。

    generate song方法不能带参数,因为值在同一个类中,应该可以从构造函数中取值。

    这只能通过字段来完成。您需要设置您的构造函数,以便为这些值创建字段,然后可以在此类中的其他任何地方使用这些字段。

    在这种情况下...

    public class Guitar {
        private int chord;
        private int numberOfStrings;
    
        public Guitar(final int chord, final int numberOfStrings) {
            this.chord = chord;
            this.numberOfStrings = numberOfStrings;
        }
     }
    

    其次,您不能在其他方法中定义方法。将 generateSong 方法的定义移出构造函数。

    第三,不要将 generateSong 方法设为静态!您创建一个要使用的实例,并在 main 内部使用该实例执行你想要的行为。

    // in main()
    Guitar guitar = new Guitar(4, 10);
    guitar.generateSong();
    

    作为附录,您需要确保解析从命令行获得的整数,因为这些值总是以String 的形式出现。我把这个作为练习留给读者。

    【讨论】:

    • Second, you can't define methods inside of other methods. 不同意。你可以。匿名,lambdas 也是函数。
    • @ssc:lambda 是一个函数(或者更好的是,是匿名类的一个非常薄的饰面)。这不是一种方法。
    • 我的错。我经常交替使用这些术语,但我理解并理解了你的意思。谢谢。
    猜你喜欢
    • 2014-09-10
    • 2021-02-07
    • 2011-11-27
    • 1970-01-01
    • 1970-01-01
    • 2018-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多