java - 构造函数说方法不能重复

标签 java

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/

相关文章:

java - 如何使用正则表达式在 Intellij IDEA 中用小写替换大写?

java - JNI 函数 Java_java_io_WinNTFileSystem_rename0 内存泄漏?

java - 将ini文件放入字符串中

java - 尝试使用 File 类加载文件时出现空指针异常

windows-xp - Windows XP 上 Wiimote(Wii Remote)的 Java API

java - 如果我从 RPM 安装 Java 或仅运行二进制文件,会有什么区别吗?

java - 我可以只为 JButton 类的一个子集设置 ButtonUI 吗?

java - 为什么我应该将实现的接口(interface)方法声明为 "public"?

java - 如何编译 Apache Thrift 定义文件?

java - 从 String 的 getchars() 方法创建的 char 数组的最后一个字符更改为不同的字符