import java.util.Scanner;
class IronMan {
private double totalTime = 3.7;
public IronMan() {
System.out.println("First Constructor running");
}
public IronMan() {
System.out.println("Second Constructor running");
}
}
public class Marathon {
public static void main(String[] args) throws InterruptedException {
IronMan person1 = new IronMan();
Scanner scan = new Scanner(System.in);
System.out.println(
"A triathlon is a challenging task. This program will allow you to know which is the perfect course for you.");
Thread.sleep(3000);
System.out.println("What is your age?");
int age = scan.nextInt();
Thread.sleep(1000);
System.out.println("What is your time for one mile, in minutes?(ex: 5.3 or 6.2");
double time = scan.nextDouble();
Thread.sleep(1000);
System.out.println("How much is your budget?");
double money = scan.nextDouble();
System.out.println(money);
if (money <= 100) {
System.out.println("You can't afford entrance!");
} else if (money > 100) {
if (age < 10) {
System.out.println("You don't qualify!");
} else {
if (time > 10) {
System.out.println("You do not qualify");
} else {
System.out.println("Good! you do qualify");
IronMan person2 = new IronMan();
}
}
}
}
}
我对构造函数的概念有点陌生。我试图创建构造函数 IronMan;但是,Eclipse 在 IronMan 一词下给出了一条错误消息。它说“在 IronMan 类型中重复方法 IronMan()”。我不明白为什么它说该方法是重复的,因为它应该是一个构造函数。
最佳答案
您有两个具有相同签名的构造函数(都没有参数)。这是不允许的,因为两者之间没有任何区别。
当您编写 IronMan person1 = new IronMan();
时,您无法指定应调用这两个构造函数中的哪一个,因此不允许同时调用两者。
关于java - 构造函数说方法不能重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32913215/