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

发布时间 : 星期六 文章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类中System.out.println的输出结果。

class B { int n;

static int sum=0; void setN(int n) { this.n=n; }

int getSum() {

for(int i=1;i<=n;i++) sum=sum+i; return sum; } }

public class A {

public static void main(String args[]) { B b1=new B(),b2=new B(); b1.setN(3); b2.setN(5);

int s1=b1.getSum(); int s2=b2.getSum();

System.out.println(s1+s2); } }

4.请说出E类中【代码1】,【代码2】的输出结果n的输出结果。 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】 } }

5.上机实习下列程序,了解可变参数。

public class E {

public static void main(String args[]) { f(1,2);

f(-1,-2,-3,-4); //给参数传值时,实参的个数很灵活 f(9,7,6) ; }

public static void f(int ... x){ //x是可变参数的代表,代表若干个int型参数

for(int i=0;i

System.out.println(x[i]); //x[i]是x代表的第i个参数(类似数组) } } }

6.类的字节码进入内存时,类中的静态块会立刻被执行。实习下列程序,了解静态块。 class AAA {

static { //静态块

System.out.println(\我是AAA中的静态块!\ } }

public class E { static { //静态块

System.out.println(\我是最先被执行的静态块!\ }

public static void main(String args[]) { AAA a= new AAA(); //AAA的字节码进入内存

System.out.println(\我在了解静态(static)块\ } 1.【代码1】:1,【代码2】:121,【代码3】:121。 2.sum=-100。 3. 27。 4.【代码1】:100,【代码2】:20.0。 5. 上机实习题目,解答略。 6. 上机实习题目,解答略。 四、编程题

联系合同范文客服:xxxxx#qq.com(#替换为@)