对于入门级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
任何帮助将不胜感激,我试图展示我的问题解决能力和访问可用资源,包括堆栈交换等在线资源。这将是一个改变位置,以便我能做到尽我所能!
谢谢
答案 0 :(得分:2)
看一下你的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 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
答案 1 :(得分:0)
首先,评论很好,但我认为你可能只有一些太多,使代码略微不可读。
秒,final
的混淆意味着您无法将变量更改为指向新变量,因此您已经拥有
private final int fooAge = 23;
这意味着fooAge
无法更改。
如果您不想使用此功能,请删除final
修饰符。
答案 2 :(得分:0)
一些问题
首先,您将变量定义为final,但您稍后尝试为其指定新值。
在Java编程语言中, final关键字用于多个 不同的上下文来定义以后无法更改的实体。
第二次,如果我们想象高度不是最终的,你指定了fooHeight
高度值,你为它指定了为什么?
public void setHeight(final double height) { <-- you cannot pass around final variable
fooHeight = height;
height = null; <--- why did you assing null to height
}
height
的类型为double
,您尝试将null
分配给它
第三次,您厌倦了使用静态方法
访问静态变量System.out.println(Foo.FOONAME); <-- totally wrong
FOONAME
是您班级的一个字段,因此需要让对象可以访问它
我认为你的意思是
Foo foo = new Foo(); //sets a value for the Foo class
System.out.println(foo.FOONAME); //prints foobar in the cmd window
而不是
Foo foo = new Foo(); //sets a value for the Foo class
System.out.println(Foo.FOONAME); //prints foobar in the cmd window
第四,你的a
变量是int类型而你年轻没有意义
switch(a) {
case young:
第五次问题
您没有正确导入List
界面
import java.util.List;
答案 3 :(得分:0)
1
public Foo(int age, int weight)
不能工作,因为fooAge和fooWeight是最终的。
2
System.out.println(Foo.FOONAME)
不能工作。因为FOONAME不是静态的
3
List<String> names = new ArrayList<>(); //creates a new resizable arrayList class
不能工作。因为只导入了ArrayList
4
height = null; // gives height a value of null, which may or may not be allowed for the double data type
不能工作。因为&#34;身高&#34;是双倍,而不是双倍。不能为null,最终变量的值不可更改。