如何编写java的方法声明

如何编写java的方法声明,java,methods,get,declaration,Java,Methods,Get,Declaration,试图弄清楚我将如何为这个方法构建方法声明 double cost = School.getCost(782.42, new Student(int credits)); 这就是我到目前为止所想到的 public void getCost(double in_cost, //not sure what to do here?) 应该是 public static void getCost(double in_cost, Student student) 它是静态的,因为您正在以这种方式调用。

试图弄清楚我将如何为这个方法构建方法声明

double cost = School.getCost(782.42, new Student(int credits));
这就是我到目前为止所想到的

public void getCost(double in_cost, //not sure what to do here?)
应该是

public static void getCost(double in_cost, Student student)
它是静态的,因为您正在以这种方式调用。

请尝试以下操作:

public static void getCost(double in_cost, Student student)

将void指定为double,最终的方法如下

public static double getCost(double in_cost, Student student)
{
 double result;
  int student_credits=student.credits;

  // calculations

  return result;

}

在代码中,将方法的返回值指定给一个
double
,这意味着方法的返回值必须是
double

public static double getCost
public static double getCost (double d, Student s) {

}
现在让我们看看参数列表,该方法需要一个浮点数和一个
Student
对象。我们可以推断第二个参数必须是
Student
。那么第一个呢?它是一个双浮点数吗?由于它没有任何后缀,例如
F
F
,因此它是一个
双后缀

public static double getCost
public static double getCost (double d, Student s) {

}
上面代码中的参数是任意的,如果需要,可以给它们指定更有意义的名称