这是我的 3 个类:主方法、dog 类和 getFee 类。
主要方法
package test;
import java.util.Scanner;
import pets.Dog;
import Utilities.FeeCalculator;
public class TestKennel
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
Dog dog = new Dog();
System.out.println("Hello " + dog.name + ", who's a good dog?");
dog.talk();
FeeCalculator getFeeStatment = new FeeCalculator();
getFeeStatement.getFee();
input.close();
}
}
狗类
package pets;
public class Dog
{
public String name = "Fido";
public byte age = 10;
public void talk()
{
System.out.println("Woof Woof Woof!!!");
System.out.println("I am " + age * 7 + " the equivalent of human years old.");
}
}
最后是 getFee 类
package Utilities;
import pets.Dog;
import java.util.Scanner;
public class FeeCalculator
{
double dailyRate;
public String getFee(Dog dog, Scanner input)
{
System.out.println();
System.out.println();
System.out.println("What is the name of your pet?");
dog.name = input.next();
System.out.println("Input how many days your pet will be staying.");
int numberOfDays = input.nextInt();
String feeStatement = "The cost for boarding "
+ dog.name
+ " for "
+ numberOfDays
+ " days is $" + numberOfDays * dailyRate;
return feeStatement;
}
}
在main方法部分,为什么eclipse给我一个错误?这不是我调用方法的方式吗?:
FeeCalculator getFeeStatment = new FeeCalculator();
getFeeStatement.getFee();
我们将非常感谢您的帮助!
最佳答案
FeeCalculator
类具有带有两个参数的方法 getFee()
。
public String getFee(Dog dog, Scanner input){}
您需要传递参数才能调用它。
改变
getFeeStatement.getFee();
到
getFeeStatement.getFee(dog,input );
变量名称也不同,FeeCalculator getFeeStatment = new FeeCalculator();
和 getFeeStatement.getFee(dog, input);
。 getFeeStatment
关于java - 如何在 Java 中调用另一个类的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30051061/