下列ABCD注释标注的哪行代码有错误? class Tom { private int x = 120; protected int y = 20; int z = 11; private void f() { x = 200; System. out. printIn (x); } void g() { x = 200; System. out. println (x); } } public class E { public static void main (String args[]) Tom tom = new Tom(); tom. x = 22; //A tom. y = 33; //B tom. z = 55;//C tom. g () ; //D } }
时间: 2024-02-14 07:11:03 浏览: 117
该段代码有一个语法错误,应该在 main 方法的第五行末尾加上 `{`,正确代码如下:
```java
class Tom {
private int x = 120;
protected int y = 20;
int z = 11;
private void f() {
x = 200;
System.out.println(x);
}
void g() {
x = 200;
System.out.println(x);
}
}
public class E {
public static void main(String args[]) {
Tom tom = new Tom();
tom.x = 22; //A
tom.y = 33; //B
tom.z = 55; //C
tom.g(); //D
}
}
```
这样就可以通过编译了。
相关问题
下列ABCD注释标注的哪行代码有错误? class Tom { private int x = 120; protected int y = 20; int z = 11; private void f() { x = 200; System. out. printIn (x); } void g() { x= 200; System. out. print]n (x); public class E { public static void main (String argsl Tom tom = new Tom); tom. x = 22; /A tom. y = 33; //B tom. z = 55; TIC tom. g () ; //D
代码中有两处错误,第三行应该是 public class Tom 而不是 class Tom,第九行应该是 new Tom() 而不是 Tom)。正确代码应该是:
```java
public class Tom {
private int x = 120;
protected int y = 20;
int z = 11;
private void f() {
x = 200;
System.out.println(x);
}
void g() {
x = 200;
System.out.println(x);
}
}
public class E {
public static void main(String[] args) {
Tom tom = new Tom();
tom.x = 22; //A
tom.y = 33; //B
tom.z = 55; //C
tom.g(); //D
}
}
```
挑错题(ABCD注释标注的哪行代码有错误?) class OutClass { int m = 1; static float x; //A class InnerClass { int m =12; //B static float n =20.89f;//C InnerClass(){ } void f() { m = 100; } } void cry() { InnerClass tom = new InnerClass(); //D } }
在给出的代码中,注释标注的行C有错误。
在内部类中,静态成员变量是不允许的。因为内部类是与外部类的实例相关联的,它不能拥有静态成员。所以在行C中,将静态关键字应用于内部类的成员变量n是错误的。
正确的代码应该是:
```java
class OutClass {
int m = 1;
static float x;
class InnerClass {
int m =12;
InnerClass(){
}
void f() {
m = 100;
}
}
void cry() {
InnerClass tom = new InnerClass();
}
}
```
阅读全文