thinking injava习题答.docx
《thinking injava习题答.docx》由会员分享,可在线阅读,更多相关《thinking injava习题答.docx(15页珍藏版)》请在三一办公上搜索。
1、thinking injava习题答这是第5章的答案: Chapter 5 Note that many of these exercises simply take you through the steps of discovering issues with packages. This is why so many of them are left to the reader. Exercise 1 /: c05:E01_ImplicitImports.java /+M java E01_ImplicitImports /* Exercise 1 * * Write a program
2、 that creates an ArrayList * object without explicitly importing * java.util.*. */ public class E01_ImplicitImports public static void main(String args) java.util.ArrayList al = new java.util.ArrayList; /: To do this, any references to the object must be made using its full package name. Exercise 2
3、/: c05:E02_LeftToReader.java /+M java E02_LeftToReader /* Exercise 2 * * In the section labeled package: the library * unit, turn the code fragments concerning * mypackage into a compiling and running set of * Java files. */ public class E02_LeftToReader public static void main(String args) System.o
4、ut.println(Exercise left to reader); /: Exercise 3 /: c05:E03_LeftToReader.java /+M java E03_LeftToReader /* Exercise 3 * * In the section labeled Collisions, take the * code fragments and turn them into a program, * and verify that collisions do in fact occur. */ public class E03_LeftToReader publi
5、c static void main(String args) System.out.println(Exercise left to reader); /: Exercise 4 /: c05:E04_LeftToReader.java /+M java E04_LeftToReader /* Exercise 4 * * Generalize the class P defined in this chapter * by adding all the overloaded versions of * rint and rintln necessary to handle all * th
6、e different basic Java types. */ public class E04_LeftToReader public static void main(String args) System.out.println(Exercise left to reader); /: Exercise 5 /: c05:E05_LeftToReader.java /+M java E05_LeftToReader /* Exercise 5 * * Change the import statement in TestAssert.java * to enable and disab
7、le the assertion mechanism. */ public class E05_LeftToReader public static void main(String args) System.out.println(Exercise left to reader); /: Exercise 6 /: c05:E06_AccessControl.java /+M java c05.E06_AccessControl /* Exercise 6 * * Create a class with public, private, * protected, and friendly d
8、ata members and * method members. Create an object of this class * and see what kind of compiler messages you get * when you try to access all the class members. * Be aware that classes in the same directory * are part of the default package. */ package c05; public class E06_AccessControl public int
9、 a; private int b; protected int c; int d; / Friendly public void f1 private void f2 protected void f3 void f4 / Friendly public static void main(String args) E06_AccessControl test = new E06_AccessControl; / No problem accessing everything inside / of main for this class, since main / is a member a
10、nd therefore has access: test.a = 1; test.b = 2; test.c = 3; test.d = 4; test.f1; test.f2; test.f3; test.f4; /: You can see that main( ) has access to everything because its a member. If you create a separate class within the same package, that class cannot access the private members: /: c05:E06_Oth
11、er.java / A separate class in the same package cannot / access private elements: package c05; public class E06_Other public E06_Other E06_AccessControl test = new E06_AccessControl; test.a = 1; /! test.b = 2; / Cant access: private test.c = 3; test.d = 4; test.f1; /! test.f2; / Cant access: private
12、test.f3; test.f4; /: If you create a class in a separate package (which you can do either by explicitly giving a package statement, or by simply putting it in a different directory) then it is unable to access anything but public members: /: c05:other2:E06_Other2.java / A separate class in the same
13、package cannot / access private elements: package c05.other2; public class E06_Other2 public E06_Other2 c05.E06_AccessControl test = new c05.E06_AccessControl; test.a = 1; /! test.b = 2; / Cant access: private /! test.c = 3; / Cant access: protected /! test.d = 4; / Cant access: friendly test.f1; /!
14、 test.f2; / Cant access: private /! test.f3; / Cant access: protected /! test.f4; / Cant access: friendly /: Exercise 7 /: c05:E07_ProtectedManipulation.java /+M java E07_ProtectedManipulation /* Exercise 7 * * Create a class with protected data. Create a * second class in the same file with a metho
15、d * that manipulates the protected data in the * first class. */ class WithProtected protected int i; public class E07_ProtectedManipulation public static void main(String args) WithProtected wp = new WithProtected; wp.i = 47; System.out.println(wp.i = + wp.i); /: The point of this exercise is to sh
16、ow that protected also means “package access” (aka “friendly”). That is, you can access protected fields within the same package. Just to be sure, try adding a protected method to WithProtected and accessing it from within E07_ProtectedManipulation. Exercise 8 /: c05:E08_LeftToReader.java /+M java E
17、08_LeftToReader /* Exercise 8 * * Change the class Cookie as specified in the * section labeled protected: sort of * friendly. Verify that bite is not public. */ public class E08_LeftToReader public static void main(String args) System.out.println(Exercise left to reader); /: Just follow the instruc
18、tions in the book. Exercise 9 /: c05:E09_Widget.java /+M java E09_Widget /* Exercise 9 * * In the section titled Class access youll * find code fragments describing mylib and * Widget. Create this library, then create a * Widget in a class that is not part of the * mylib package. */ import mylib.Wid
19、get; public class E09_Widget public static void main(String args) new Widget; /: /: c05:mylib:Widget.java package mylib; public class Widget public Widget System.out.println(Making a Widget); /: Exercise 10 /: c05:E10_LeftToReader.java /+M java E10_LeftToReader /* Exercise 10 * * Create a new direct
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- thinking injava习题答 injava 习题
链接地址:https://www.31ppt.com/p-3167062.html