java - 为什么这个使用多个构造函数的 Java 代码在 VS 代码中运行但不能用 javac 编译?
问题描述
我一直在尝试在 Java 中使用多个构造函数。下面是我的代码:
public class MultipleConstructors {
int x = 20;
int y = 50;
String color = "Green";
String color2 = "Yellow";
public MultipleConstructors() {
}
public MultipleConstructors(int numb, int numb2, String colOne, String colTwo) {
x = numb;
y = numb2;
color = colOne;
color2 = colTwo;
}
public MultipleConstructors(int numb3, int numb4, String colThree, String colFour) {
x = numb3;
y = numb4;
color = colThree;
color2 = colFour;
}
public MultipleConstructors(int numb5, int numb6, String colFive, String colSix) {
x = numb5;
y = numb6;
color = colFive;
color2 = colSix;
}
public static void main(String[] args) {
MultipleConstructors myObjOne = new MultipleConstructors(100, 200, "Pink", "Blue");
MultipleConstructors myObjTwo = new MultipleConstructors(300, 500, "Burgandy", "Silver");
MultipleConstructors myObjThree = new MultipleConstructors(800, 1000, "Black", "White");
MultipleConstructors myObjFour = new MultipleConstructors();
System.out.println(myObjOne.x + myObjOne.y + " " + myObjOne.color + " " + myObjTwo.color2 + " " + " : SUCCESS");
System.out.println(myObjTwo.x + myObjTwo.y + " " + myObjTwo.color + " " + myObjTwo.color2 + " " + " : VICTORY");
System.out.println(myObjThree.x + myObjThree.y + " " + myObjThree.color + " " + myObjThree.color2 + " " + " : YES");
System.out.println(myObjFour.x + myObjFour.y + " " + myObjFour.color + " " + myObjFour.color2 + " " + " : YES");
}
}
它在 VS Code 中运行,但不会使用javac
命令编译。我认为它可能是public MultipleConstructors() {}
父属性声明之后的未定义构造函数。
解决方案
如果您收到类似以下错误消息:
MultipleConstructors.java:11: error: constructor MultipleConstructors(int,int,java.lang.String,java.lang.String) is already defined in class MultipleConstructors
那么你应该使用这个答案。如果您遇到 ClassNotFoundException,您应该看到 Ntshembo Hlongwane 的回答。
您的问题是您有多个相同的构造函数。我认为您要做的是构造函数重载,这意味着您可以拥有多个具有不同标头的构造函数,但是您的三个构造函数具有相同的标头。在下面的构造函数中查看我的评论:
public MultipleConstructors() {
// header has no parameters
// fine, leaves values alone
}
public MultipleConstructors(int numb, int numb2, String colOne, String colTwo) {
// header has parameters int, int String String
//fine, offers you the opportunity to change those values
x = numb;
y = numb2;
color = colOne;
color2 = colTwo;
}
public MultipleConstructors(int numb3, int numb4, String colThree, String colFour) {
// header has parameters int, int, String, String
// Wait, that's the same as the one above!
x = numb3;
y = numb4;
color = colThree;
color2 = colFour;
}
public MultipleConstructors(int numb5, int numb6, String colFive, String colSix) {
// header has parameters int, int, String, String
// Wait, that's also the same as the one above!
x = numb5;
y = numb6;
color = colFive;
color2 = colSix;
}
当你实例化这个对象时,你给它两个整数和一个字符串,你希望调用哪个构造函数?这是一个逻辑错误,而不是语法错误或编译器错误,因为您的代码不明确。希望这对您有所帮助,并祝您在学习之旅中好运。:)
推荐阅读
- laravel - 为什么在某些环境中找不到这个 Laravel 外观?
- gcc - 如何告诉 GCC 不要将 main 的堆栈与 16 字节边界对齐?
- javascript - 如何使用 FeathersJS 检查服务器中的用户身份验证?
- c++ - 有没有办法将位从双精度复制到 uint64 而不进行类型转换或内存溢出?
- c - 内联汇编中的结构偏移
- asp.net-core - 何时使缓存无效 - .net core api
- c# - 在 android 中使用 volley 调用 C# Web 服务的方法
- python - Python如何检测线程连接是否超时
- powershell - 应该使用什么运算符来检测空的 psobject?
- python - 如何使用 Boto3 和 Python 将 lambda 函数订阅到 DynamoDB 流?