Java-2实用教程(第5版)习题解答

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.f(); //【代码4】 tom.g(); //【代码5】 } }

7.下列E类的类体中哪些【代码】是错误的。4

class E {

int x; //【代码1】 long y = x; //【代码2】 public void f(int n) {

int m; //【代码3】 int t = n+m; //【代码4】 } }

1.B。2.D。3.D。4.D。5.CD。6.【代码1】【代码4】。7.【代码4】。 三、阅读程序

1.说出下列E类中【代码1】~【代码3】的输出结果。 class Fish {

int weight = 1; }

class Lake { Fish fish;

void setFish(Fish s){

fish = s; }

void foodFish(int m) {

fish.weight=fish.weight+m; } }

public class E {

public static void main(String args[]) { Fish redFish = new Fish();

System.out.println(redFish.weight); //【代码1】 Lake lake = new Lake(); lake.setFish(redFish); lake.foodFish(120);

System.out.println(redFish.weight); //【代码2】 System.out.println(lake.fish.weight); //【代码3】 } }

2.请说出A类中System.out.println的输出结果。 class B {

int x = 100,y = 200;

public void setX(int x) { x = x; }

public void setY(int y) { this.y = y; }

public int getXYSum() { return x+y; } }

public class A {

public static void main(String args[]) { B b = new B(); b.setX(-100); b.setY(-200);

System.out.println(\ } }

3.请说出A类中S

>>闂佽绻掗崑鐐裁洪弽顐n潟闁硅揪绠戠粈鍌炴煏婵犲繘妾柣搴嫹<<
12@gma联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4