Thinking in Java 第四版完整版 第六章练习题 访问权限控制

Thinking in Java 第四版完整版 第六章练习题,记录一下(jdk1.8.0)

1.

package com.test.c06;

/**
 * 练习1:在某个包中创建一个类,在这个类所处的包的外部创建该类的一个实例。
 * @author admin11
 * @date 2018年3月20日
 */
public class Exercise601 {

}
package com.test;

import com.test.c06.Exercise601;

public class Exercise601out {

    public static void main(String[] args) {
        new Exercise601();
    }
}

2.

package com.test.c06;

// import net.mindview.simple.*; 该包下也存在名字为Vector的类时,会产生冲突
import java.util.Vector;

/**
 * 练习2:将本节中的代码片段改写为完整的程序,并校验实际所发生的冲突。
 * @author admin11
 * @date 2018年3月20日
 */
public class Exercise602 {

    public static void main(String[] args) {
        // java.util.Vector v = new java.util.Vector();
        Vector v = new Vector();
        // 当该类的导入有多个时,会产生冲突,编译器不知道用的是哪个Vector类。
        // 建议:1.避免导入多个 2.指定全名的方式创建类
    }
}

3.

package com.test.c06;

// import com.test.c06.debugoff.Test;
// import com.test.c06.debug.Test;
/**
 * 练习3:创建两个包:debug和debugoff,它们都包含一个相同的类,
 * 该类有一个debug()方法。第一个版本显示发送给控制台的String参数,
 * 而第二个版本什么也不做。使用静态import语句将该类导入一个测试程序中,
 * 并示范条件编译效果。
 * @author admin11
 * @date 2018年3月20日
 */
public class Exercise603 {

    public static void main(String[] args) {
        com.test.c06.debugoff.Test.debug("debugoff"); // Test.debug("debugoff");
        com.test.c06.debug.Test.debug("debug"); // Test.debug("debug");
    }
}
package com.test.c06.debug;

public class Test {

    public static void debug(String msg) {
        System.out.println("Message: " + msg);
    }
}
package com.test.c06.debugoff;

public class Test {

    public static void debug(String msg) {}
}

这里写图片描述

4.

package com.test.c06.local;

/**
 * 练习4:展示protected方法具有包访问权限,但不是public。
 * @author admin11
 * @date 2018年3月20日
 */
public class Exercise604 {

    protected static void greeting() {
        System.out.println("Hello everyone!");
    }
}
package com.test.c06.local;

public class Exercise604in {

    public static void main(String[] args) {
        Exercise604.greeting();
    }
}
package com.test.c06;

public class Exercise604out {

    public static void main(String[] args) {
        //com.test.c06.local.Exercise604.greeting();
        // The method greeting() from the type Exercise604 is not visible
    }
}

这里写图片描述

5.

package com.test.c06;

/**
 * 练习5:创建一个带有public,private,protected和包访问
 * 权限域以及方法成员的类。创建该类的一个对象,看看在你试图调用所有类
 * 成员时,会得到什么类型的编译消息。请注意,处于同一目录中的所有类都是
 * 默认包的一部分。
 * @author admin11
 * @date 2018年3月20日
 */
public class Exercise605main {

    public int a;
    private int b;
    protected int c;
    int d;
    public void f1() {}
    private void f2() {}
    protected void f3() {}
    void f4() {}

    public static void main(String[] args) {
        Exercise605main e = new Exercise605main();
        e.a = 1;
        e.b = 2;
        e.c = 4;
        e.d = 5;
        e.f1();
        e.f2();
        e.f3();
        e.f4();
    }
}
package com.test.c06;

public class Exercise605test {

    public static void main(String[] args) {
        Exercise605main e = new Exercise605main();
        e.a = 1;
        //e.b = 2; 
        // The field Exercise605main.b is not visible
        e.c = 4;
        e.d = 5;
        e.f1();
        //e.f2(); 
        // The method f2() from the type Exercise605main is not visible
        e.f3();
        e.f4();
    }
}
package com.test;

import com.test.c06.Exercise605main;

public class Exercise605out {

    public static void main(String[] args) {
        Exercise605main e = new Exercise605main();
        e.a = 1;
        //e.b = 2;
        // The field Exercise605main.b is not visible
        //e.c = 4;
        // The field Exercise605main.c is not visible
        //e.d = 5;
        // The field Exercise605main.d is not visible
        e.f1();
        //e.f2();
        // The method f2() from the type Exercise605main is not visible
        //e.f3();
        // The method f3() from the type Exercise605main is not visible
        //e.f4();
        // The method f4() from the type Exercise605main is not visible
    }
}

6.

package com.test.c06;

/**
 * 练习6:创建一个带有protected数据的类。运用在
 * 第一个类中处理protected数据的方法在相同的文件
 * 中创建第二个类。
 * @author admin11
 * @date 2018年3月20日
 */

class WithProtected {
    protected int i;
}

public class Exercise606 {

    public static void main(String[] args) {
        WithProtected wp = new WithProtected();
        wp.i = 47;
        System.out.println(wp.i);
    }
}

这里写图片描述

7.

扫描二维码关注公众号,回复: 4835282 查看本文章
package com.test.c06;

import com.test.c06.access.Widget;

/**
 * 练习7:根据描述access和Widget的代码片段创建类库。在某个
 * 不属于access类库的类中创建一个Widget实例。
 * @author admin11
 * @date 2018年3月20日
 */
public class Exercise607 {

    public static void main(String[] args) {
        new Widget();
    }
}
package com.test.c06.access;

public class Widget {

    public Widget() {
        System.out.println("Making a Widget");
    }
}

这里写图片描述

8.

package com.test.c06;

import com.test.c06.access.connection.Connection;
import com.test.c06.access.connection.ConnectionManager;

/**
 * 练习8:效仿示例Luch.java的形式,创建一个名为ConnectionManager
 * 的类,该类管理一个元素为Connection对象的固定数组。客户端程序员不能直接
 * 创建Connection对象,而只能通过ConnectionManager中的某个static
 * 方法来获取它们。当ConnectionManager之中不再有对象时,它会返回null引
 * 用。在main()之中检测这些类。
 * @author admin11
 * @date 2018年3月20日
 */
public class Exercise608 {

    public static void main(String[] args) {
        Connection c = ConnectionManager.getConnection();
        while(c != null) {
            System.out.println(c);
            c.doSomething();
            c = ConnectionManager.getConnection();
        }
    }
}
package com.test.c06.access.connection;

public class Connection {

    private static int counter = 0;
    private int id = counter++;
    Connection() {} // 包访问权限

    public String toString() {
        return "Connection " + id;
    }

    public void doSomething() {}
}
package com.test.c06.access.connection;

public class ConnectionManager {

    private static Connection[] pool = new Connection[10];
    private static int counter = 0;
    static {
        for (int i = 0; i < pool.length; i++) {
            pool[i] = new Connection();
        }
    }

    public static Connection getConnection() {
        if(counter < pool.length) {
            return pool[counter++];
        }
        return null;
    }
}

这里写图片描述

9.

package com.test.c06.access.foreign;

/**
 * 练习9:在access/local目录下编译一下文件
 * (假定access/local目录在你的CLASSPATH中):
 * // access/local/PackagedClass.java
 * package access.local;
 * class PackagedClass {
 *      public PackagedClass() {
 *          System.out.println("Craeting s packaged class");
 *      }
 * }
 * 然后在access/local之外的另一个目录中创建下列文件:
 * // access/foreign/foreign.java
 * package access.foreign;
 * public class foreign {
 *    public static void main(String[] args) {
 *      PackagedClass pc = new PackagedClass();
 *    }
 * 
 * }
 * 解释一下为什么编译器会产生错误。如果将foreign类置于access.local
 * 包之中的话,会有所改变吗?
 * @author admin11
 * @date 2018年3月21日
 */
public class foreign {

    public static void main(String[] args) {
        // PackagedClass cannot be resolved to a type
        //PackagedClass pc = new PackagedClass();
        // 编译器会产生错误原因:PackagedClass具有包访问权限,在该包外的其它类中则无法访问;
        // 如果该类用public修饰,则在其它包的其它类中可以访问。
        // 如果将foreign类置于access.local包之中的话,则可以访问PackagedClass,
        // 不会报错,因为在同一个包下。
    }
}
package com.test.c06.access.local;

class PackagedClass {

    public PackagedClass() {
        System.out.println("Craeting s packaged class");
    }
}
package com.test.c06.access.local;

public class foreign {

    public static void main(String[] args) {
        // PackagedClass cannot be resolved to a type
        PackagedClass pc = new PackagedClass();
    }
}

猜你喜欢

转载自blog.csdn.net/summerSunStart/article/details/79637713