为什么这个 Java 代码在 VS 代码中使用多个构造函数 运行 但不会用 javac 编译?
Why does this Java code using multiple constructors run in VS code but won't compile with 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 的回答。
你的问题是你有多个相同的构造函数。我认为你想要的是构造函数重载,这意味着你可以有多个具有不同 header 的构造函数,但是你的三个构造函数具有相同的 header。请在下面查看我与您的构造函数内联的评论:
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;
}
当您实例化这个 object 并给它两个整数和一个字符串时,您希望调用哪个构造函数?这是逻辑错误,不是语法错误或编译器错误,因为你的代码有歧义。希望这对您有所帮助,并祝您在学习过程中好运。 :)
基于运行ning代码时发现的异常
Correct, runs in VSCode but won't compile in order to run in terminal. It throws the error "could not find or load main class " caused by "ClassNotFoundException"
这是由于:
- 假设您将文件命名为
Test.java
- 然后在您的代码中将 class 写为
MyTest
- 当您尝试 运行 代码
java Test
时,您会收到以下错误:
Error: Could not find or load main class Test
Caused by: java.lang.ClassNotFoundException: Test
- 这是因为 Java 现在创建了一个
MyTest.class
,您必须 运行 而不是
我一直在尝试在 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 的回答。
你的问题是你有多个相同的构造函数。我认为你想要的是构造函数重载,这意味着你可以有多个具有不同 header 的构造函数,但是你的三个构造函数具有相同的 header。请在下面查看我与您的构造函数内联的评论:
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;
}
当您实例化这个 object 并给它两个整数和一个字符串时,您希望调用哪个构造函数?这是逻辑错误,不是语法错误或编译器错误,因为你的代码有歧义。希望这对您有所帮助,并祝您在学习过程中好运。 :)
基于运行ning代码时发现的异常
Correct, runs in VSCode but won't compile in order to run in terminal. It throws the error "could not find or load main class " caused by "ClassNotFoundException"
这是由于:
- 假设您将文件命名为
Test.java
- 然后在您的代码中将 class 写为
MyTest
- 当您尝试 运行 代码
java Test
时,您会收到以下错误:
Error: Could not find or load main class Test
Caused by: java.lang.ClassNotFoundException: Test
- 这是因为 Java 现在创建了一个
MyTest.class
,您必须 运行 而不是