Java - Thinking in Java 第6章 習題

來源:互聯網
上載者:User

標籤:mystra   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 [email protected] [] */
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

因為未加限定詞的類是包存取權限的, 包外的類無法調用, 所以產生編譯錯誤. 而把類置於類內, 則可以訪問.

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

Java - Thinking in Java 第6章 習題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.