对于入门级Java开发人员面试,他们要求我对此代码发表评论,并说明程序将打印eb的内容(不重写,因此我认为那不是我应该做的?)该工作始于一堆付费Java训练。我不相信他们期望我成为天才。我三天前才开始学习java。我已经在代码中添加了注释,但是当我尝试使用jdk 1.8 javac Foo.java进行编译时会出现一堆错误。
import java.util.ArrayList; //allows use of the class ArrayList within program
public class Foo { //declares new class
public String FOONAME = "foobar"; //declares new string variable giving a value of foobar
private final int fooAge = 23; //declares a private int giving it a final value of 23
private static final int fooWeight = 150; //declares a private int giving it a final value of 150
private double fooHeight; //declares a private double variable,
private Foo() {} // declares new constructor setting default initial values
public Foo(int age, int weight) { //declares constructor and defines initial values
fooAge = age; // initial value of age will be 23
fooWeight = weight; // initial value of weight will be 150
}
public void main(String args) { //call to main method, every program needs one, no cmd args, and no return necessary
Foo foo = new Foo(); //sets a value for the Foo class
System.out.println(Foo.FOONAME); //prints foobar in the cmd window
int a = foo.getAge(); // declares int variable a giving it a value of foo.getAge() which = fooAge = 23
int w = foo.getWeight(); // declares int variable w giving it a value of foo.getWeight() which = fooWeight = 150
int young = 20; // delcares int variable called young, giving it a value of 20
switch(a) { // begins the switch statement, relies on a being a constant value
case young: // when a = young (never because a is a constant and already set to 23)
System.out.println("Young"); //print young on a new line in the cmd window is the case statement is True
break; // seperates the case statements of the switch statement
case 30: // when a = 30 (Again this won't happen because a is set at a constant that = fooAge = 23)
System.out.println("MiddleAged"); // print MiddleAged on a new line in the cmd window if the case statement is True
default: // defines the result if a is anything other than the two cases defined above.
System.out.println("Old"); // Old will be printed on a new line in the cmd window if the a meets the default requirements
} // closes switch statement
if (a > 70) //begins an if statement, if a is greater than 70 (which it won't be because a is again 23 the final fooAge
System.out.println("Really Old"); // Really Old would print if a were over 70
List<String> names = new ArrayList<>(); //creates a new resizable arrayList class
names.add("Bob"); //adds a string value to the arraylist
names.add("George");//adds a string value to the arraylist
names.add("Linda");//adds a string value to the arraylist
names.add(young); // adds the young variable to the array, but its not a string so that can't be right?
} //closes the main method
private int getAge() { //decalres the getage variable
return fooAge; //gives it the fooAge value that is final and previously set to 23
} // closes getAge Variable declaration
public static int getWeight() { // declares getWeight variable
return fooWeight; //gives it the fooWeight variable that is final and previosuly set to 150
} // closes the getWeight Declration
public void setHeight(final double height) { //decalres setHeight variable,
fooHeight = height; // gives height a value of fooheight that hasn't been defined yet
height = null; // gives height a value of null, which may or may not be allowed for the double data type
} // closes the setHeight variable
} // closes the the class Foo
/*
if compiled it will print foobar on one line and Old on another
*/
我的错误
C:\temp\MyWork> javac Foo.java
Foo.java:15: error: cannot assign a value to final variable fooAge
fooAge = age; // initial value of age will be 23
^
Foo.java:16: error: cannot assign a value to final variable fooWeight
fooWeight = weight; // initial value of weight will be 150 ^
Foo.java:23: error: non-static variable FOONAME cannot be referenced from a static context
System.out.println(Foo.FOONAME); //prints foobar in the cmd window ^
Foo.java:31: error: constant expression required
case young: // when a = young (never because a is a constant and already set to 23) ^
Foo.java:44: error: cannot find symbol
List<String> names = new ArrayList<>(); //creates a new resizable arrayList class ^
symbol: class List
location: class Foo
Foo.java:64: error: incompatible types: <null> cannot be converted to double
height = null; // gives height a value of null, which may or may not be allowed for the double data type ^ 6 errors
任何帮助将不胜感激,我正在尝试展示我解决问题和访问可用资源(包括诸如堆栈交换之类的在线资源)的能力。这将是改变职位的方式,所以我将尽我所能!
谢谢
最佳答案
看一下Foo的构造函数
public Foo(int age, int weight) {
fooAge = age;
fooWeight = weight;
}
但是这些
fooAge
和fooWeight
是final
private final int fooAge = 23;
private static final int fooWeight = 150;
您不能更改
final
变量。 final
它本身具有final
变量的含义。解决方案:您可以使用非
final
变量。下期
System.out.println(Foo.FOONAME); //FOONAME non static you can't call by class
//name non static variables
解决方案:将
FOONAME
设为静态。下期。
switch(a) {
case young: // cases must be constant.
在这里
young
不是常量。解决方案:将
young
设置为final。 final int young = 20;
下期。
List<String> names = new ArrayList<>(); // List not imported
names.add(young); // adding int to String array?
解决方案:使用适当的进口。
import java.util.List;
和
names.add(String.valueOf(young));
下期。
public void setHeight(final double height) {
fooHeight = height;
height = null; // you can't use null for primitive double
// and also height is final can't change it.
解决方案:您不能将新值分配给
height
关于java - 通过cmd行编译时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26210295/