下列 A 类中【代码 1】~【代码 4】哪个是错误的?
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 A {
public static void main(String args[]) {
Tom tom = new Tom();
tom.x = 22; //【代码 1】
tom.y = 33; //【代码 2】
tom.z = 55; //【代码 3】
tom.g(); //【代码 4】
}
}
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 A {
public static void main(String args[]) {
Tom tom = new Tom();
tom.x = 22; //【代码 1】
tom.y = 33; //【代码 2】
tom.z = 55; //【代码 3】
tom.g(); //【代码 4】
}
}
举一反三
- 给出下列【代码】注释标注的代码的输出结果。class Tom {int weight = 10;void Tom(){weight = 18;}}public class E {public static void main(String args[]) {Tom cat = new Tom();System.out.println(cat.weight); //【代码】}}
- 对于下列Tom类,哪个叙述是正确的 public class Test { public static void main(String args[]){ Tom cat1 = new Tom(); Tom cat2 = new Tom(100); } } class Tom { void Tom{ System.out.print("hello"); } Tom(int n){ System.out.print(n); } }
- 请说出E类中[代码1]和[代码2]的输出结果。 class A{ double f(int x, double y){ return x+y; } int f(int x,int y) { return x*y; } } public class E{ public static void main(String args[ ] ){ A a=new A(); System.out.println(a.f(10,10)) ; // [代码1] System.out.println(a.f (10,10.0)) ; // [代码2] } }
- 请说出E类中【代码1】,【代码2】的输出结果。 interface A { double f(double x,double y); } class B implements A { public double f(double x,double y) { return x*y; } int g(int a,int b) { return a+b; } } public class E { public static void main(String args[]) { A a = new B(); System.out.println(a.f(3,5)); //【代码1】 B b = (B)a; System.out.println(b.g(3,5)); //【代码2】 } }
- 以下代码的输出结果 ? public class Test1{ static int x = 3; static { ++x; } public static void main(String args[]){ System.out.println(x); } static { x++; } }