Java - Thinking in Java 第6章 习题

###1

package one;

/**
 * 包内类
 * <p/>
 * Created by C.L.Wang on 15/8/4.
 */
public class One {
    public One() {
        System.out.println("This is one!");
    }
}
/**
 * Output:
 This is one!
 */
import one.One;

/**
 * 包外类
 * <p/>
 * Created by wang on 15/8/4.
 */
public class Two {
    public static void main(String[] args) {
        new One();
    }
}

###2

package one;

/**
 * 同名类
 * <p/>
 * Created by wang on 15/8/4.
 */
public class Vector {
    public Vector() {
        System.out.println("wang's vector");
    }
}
import one.Vector;
import java.util.*; // 未使用

/**
 * 测试类名冲突
 * <p/>
 * Created by wang on 15/8/4.
 */
public class TestVector {
    public static void main(String[] args) {
        Vector vector = new Vector();
        java.util.Vector vector2 = new java.util.Vector();
        System.out.println(vector);
        System.out.println(vector2);
    }
}
/**
 * Output:
 wang's vector
 one.Vector@24a37368
 []
 */

###3

package debug;

import java.util.Arrays;

/**
 * 输出信息
 * Created by wang on 15/8/4.
 */
public class Log {
    public static void v(String[] strs) {
        System.out.println(Arrays.toString(strs));
    }
}
package debugoff;

/**
 * 不输出
 * Created by wang on 15/8/4.
 */
public class Log {
    public static void v(String[] strs) {
        System.out.println("null");
    }
}
import static debug.Log.*;
//import static debugoff.Log.*; // 无输出

/**
 * 测试输出
 * Created by wang on 15/8/4.
 */
public class TestLog {
    public static void main(String[] args) {
        v(args);
    }
}
/**
 * Output:
 * (1) null
 * (2) [My, Name, is, Girl, !]
 */

###4

package one;

/**
 * protected的访问权限
 * Created by wang on 15/8/4.
 */
public class PackProtected {
    protected void sayHelloProtected () {
        System.out.println("Hello Protected");
    }

    public void sayHelloPublic () {
        System.out.println("Hello Public");
    }

    // 包外无法访问
    void sayHelloPackage() {
        System.out.println("Hello Package");
    }
}
import one.PackProtected;

/**
 * protected跨越包继承
 *
 * Created by wang on 15/8/4.
 */
public class TestProtected extends PackProtected {
    public static void main (String[] str) {
        TestProtected t = new TestProtected();
        t.sayHelloProtected();
        t.sayHelloPublic();
    }
}
/**
 * Output:
 Hello Protected
 Hello Public
 */

###5

package one;

/**
 * 测试访问权限
 * Created by wang on 15/8/4.
 */
public class Access {
    public void SayAccessPublic() {
        System.out.println("Public");
    }

    private void SayAccessPrivate() {
        System.out.println("Private");
    }

    protected void SayAccessProtected() {
        System.out.println("Private");
    }

    void SayAccess() {
        System.out.println("Package");
    }
}
import one.Access;

/**
 * 测试访问权限异常
 * Created by wang on 15/8/4.
 */
public class CallAcess {
    public static void main(String[] args) {
        Access a = new Access();
        a.SayAccessPublic();
//        a.SayAccessPrivate();
//        a.SayAccessProtected();
//        a.SayAccess();
    }
}
/**
 * Output:
 * (1) Public
 * (2) SayAccessPrivate() 可以在 one.Access 中访问 private
 * (3) SayAccessProtected() 可以在 one.Access 中访问 protected
 * (4) SayAccess() 在 one.Access 中不是公共的;无法从外部软件包中对其进行访问
 */

###6

/**
 * 类内处理Protected数据
 * <p/>
 * Created by wang on 15/8/4.
 */
class Data {
    protected int d = 11;
}

class ProcessData {
    public void changeData(Data data) {
        data.d = 22; // 类内处理protected数据
    }
}

public class ProtecedData {

    public static void main(String[] strs) {
        Data data = new Data();
        System.out.println(data.d);
        new ProcessData().changeData(data);
        System.out.println(data.d);
    }
}
/**
 * Output:
 11
 22
 */

###7

package access;

/**
 * 包类
 * Created by wang on 15/8/4.
 */
public class Widget {
    public void talk() {
        System.out.println("I am a girl!");
    }
}
import access.Widget;

/**
 * 调用外部类
 * Created by wang on 15/8/4.
 */
public class TestWidget {
    public static void main(String[] args) {
        Widget widget = new Widget();
        widget.talk();
    }
}
/**
 * Output:
 * I am a girl!
 */

###8

/**
 * 管理类
 * Created by wang on 15/8/4.
 */
class Connection {
    private static int count = 0; // 统计创建数

    private Connection() {
        count++;
    }

    public static Connection makeConnection() {
        return new Connection();
    }

    public int getCount() {
        return count--;
    }
}

public class ConnectionManager {

    int countOfConnection = 4;

    Connection[] ca = new Connection[countOfConnection];

    // 非静态实例初始化
    {
        for (int i = 0; i < countOfConnection; ++i) {
            ca[i] = Connection.makeConnection();
        }
    }

    public Connection getConnection(int i) {
        if (i < countOfConnection) {
            return ca[i];
        } else {
            return null;
        }
    }

    public static void main(String[] args) {
        ConnectionManager c = new ConnectionManager();
        for (int i = 0; i < c.countOfConnection + 1; ++i) {
            Connection connection = c.getConnection(i);
            if (connection != null) {
                System.out.println("num = " + connection.getCount());
            } else {
                System.out.println("null");
            }
        }
    }
}
/**
 * Output:
 num = 4
 num = 3
 num = 2
 num = 1
 null
 */

###9
因为未加限定词的类是包访问权限的, 包外的类无法调用, 所以产生编译错误. 而把类置于类内, 则可以访问.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ManonLegrand

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值