使用字段中的参数调用双数组作为方法

时间:2017-12-09 07:59:39

标签: java

免责声明:如果我错误地使用了这些条款,请注意。如果你能提供正确的术语(如果我错误地使用它们,那就太棒了!)

这是我的字段和构造函数的类:

double[] studentMathScores = {81.5,89.0,45.5,99.0,55.0,34.5,56.0,78.0,76.0,80.0};   
public StudentDashboard(double[] studentMathScores) 
    {
        this.studentMathScores = studentMathScores;
    }

如何使用studentMathScores在我的主类中声明一个对象?

StudentDashboard test = new StudentDashboard(studentMathScores);

`

2 个答案:

答案 0 :(得分:0)

我将开始使用OOP概念。

这就是宣言的完成方式:

The first step is to redirect your user to the Freelancer.com Identity 
authorise url. This redirect prompts the user to give your application 
permission to access their protected resources. When a user grants 
permission, they will be redirected to an endpoint on your server with an 
authorization code to be used in the following steps

现在我通过为对象分配类的构造函数来初始化对象。还为构造函数提供了数组所需的参数:

StudentDashboard test;

现在每当我需要从这个对象使用这个数组时,这就是它的完成方式。

StudentDashboard test = new StudentDashboard(studentMathScores);

答案 1 :(得分:0)

  1. 如果要保留当前的构造函数/类语法,则应在main方法中声明。
  2. double[] studentMathScores = {81.5,89.0,45.5,99.0,55.0,34.5,56.0,78.0,76.0,80.0};

    在这种情况下,studentMathScoresStudentDashboard的声明应该像double[] studentMathScores;

    然后就这样:

    public class Main { public static void main(String[] args) { double[] studentMathScores = {81.5,89.0,45.5,99.0,55.0,34.5,56.0,78.0,76.0,80.0}; StudentDashboard test = new StudentDashboard(studentMathScores); } }

    1. 如果可以更改类语法并且studentMathScores可以是静态的,则可以这样使用:

      public class StudentDashboard {
      
      static double[] studentMathScores ={81.5,89.0,45.5,99.0,55.0,34.5,56.0,78.0,76.0,80.0};
      
      public StudentDashboard()
      {
      }
      
      public static double[] getStudentMathScores() {
          return studentMathScores;
      }
      
      public class Main {
      
      public static void main(String[] args) {
          StudentDashboard test = new StudentDashboard();
          double[] studentMathScores = StudentDashboard.getStudentMathScores();
      }
      

      }