Thinking in java Chapter6 访问权限控制

Posted erinchen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Thinking in java Chapter6 访问权限控制相关的知识,希望对你有一定的参考价值。

访问权限控制的等级,从最大权限到最小权限:public proteced 包访问权限 private

6.1 包:库单元

public class FullQualification {
    public static void main(String[] args) {
        java.util.ArrayList list = new java.util.ArrayList<>();
    }
}
import java.util.ArrayList;

public class SingleImport {
    public static void main(String[] args) {
        ArrayList list = new ArrayList();
    }
}

编译单元 .java 每个编译单元只能有一个public类

6.1.1 代码组织

库名 小写

6.1.2 创建独一无二的包名

sudo cd /usr/libexec
./java_home
/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home

6.1.3定制工具库

import java.io.PrintStream;

public class Print {
    // Print with a newline
    public static void print(Object obj){
        System.out.println(obj);
    }
    //Print a newline by itself
    public static void print(){
        System.out.println();
    }
    //Print with no line break
    public static void printnb(Object obj){
        System.out.print(obj);
    }
    //The new Java SE5 printf() (from C)
    public static PrintStream printf(String format,Object... args){
        return System.out.printf(format,args);
    }
}

import static chapter6access.Print.print;

public class PrintTest {
    public static void main(String[] args) {
        print("avaliable from now on!");
        print(100);
    }
}

6.1.4 用import改变行为

c语言解决跨平台的条件编译,由于java跨平台,没有必要
java 条件编译的调试用户,修改被导入的package()方法如:调试版本-发布版

6.2 Java访问权限修饰词

public proteced 包访问权限(不提供任何访问权限修饰词,默认) private

6.2.1包访问权限

6.2.2public 接口访问权限

package chapter6access.subfolder; //处于子目录下

public class Cookie {
    public Cookie(){
        System.out.println("Cookie constructor");
    }
    void bite(){
        System.out.println("bite");
    }
}

package chapter6access;

import chapter6access.subfolder.Cookie;

public class Dinner {
    public static void main(String[] args) {
        Cookie x = new Cookie();
//        x.bite(); //处于子目录下,can't access,若不是子目录,同目录下,则享有包管理权限。
    }
}

6.2.3 private:你无法访问

private 多线程 重要(21章)

class Sundae {
    private Sundae(){
    }//不能通过构造器创建Sundae对象,必须调用makeASundae方法达到目的。
    //会阻碍对此类对继承
    static Sundae makeASundae(){
        return new Sundae();
    }
}

public class IceCream{
    public static void main(String[] args) {
//        Sundae x = new Sundae();
        Sundae x = Sundae.makeASundae();
    }
}
别名机制 类引用是private,其他对象也可以拥有该对象public引用

6.2.4 protected: 继承访问权限

package chapter6access.subfolder;

public class Cookie {
   public Cookie(){
       System.out.println("Cookie constructor");
   }
   protected void bite(){ //如果没有 protected ,即 默认包管理权限,则继承的非同级包,无法使用。
       System.out.println("bite");
   }
}

import chapter6access.subfolder.Cookie;

public class ChocolateChip extends Cookie {
   public ChocolateChip(){
       System.out.println("ChocolateChip constructor");
   }
   public void chomp(){
       bite(); //can't access bite
   }

   public static void main(String[] args) {
       ChocolateChip x = new ChocolateChip();
       x.chomp();
   }
}
main 是成员方法,因此可以访问一切
package chapter6access.subfolder;

public class E05 {
    public int a;
    private int b;
    protected int c;
    int d;//package access
    public void f1(){}
    private void f2(){}
    protected void f3(){}
    void f4(){} ////package access

    public static void main(String[] args) {
        E05 e05 = new E05();
        e05.a =1;
        e05.b =1;
        e05.c =1;
        e05.d =1;
        e05.f1();
        e05.f2();
        e05.f3();
        e05.f4();

    }

}

在当前包下创建单独的类,则不能访问私有成员

package chapter6access.subfolder;

public class E05Class {
    public E05Class(){
        E05 e05 = new E05();
        e05.a =1;
//        e05.b =1; //can't access :private
        e05.c =1;
        e05.d =1;
        e05.f1();
//        e05.f2(); //can't access :private
        e05.f3();
        e05.f4();
    }

}

在不同的包下,或使用包引入语句,只能访问 公有成员
package chapter6access;

import chapter6access.subfolder.E05;

public class E05Class {
    public E05Class(){
        E05 e05 = new E05();
        e05.a =1;
//        e05.b =1; //can't access :private
//        e05.c =1; //can't access :protected
//        e05.d =1; //can't access :package
        e05.f1();
//        e05.f2(); //can't access :private
//        e05.f3(); //can't access :protected
//        e05.f4(); //can't access :package
    }

}


package chapter6access.subfolder;

class WithProtected {
    protected int a; //also means  package access  在相同的包内,可以访问protected 

}

public class E06Class{
    public static void main(String[] args) {
        WithProtected wp = new WithProtected();
        wp.a = 47;
        System.out.println("wp.a = " + wp.a);
    }
}

6.3 接口和实现

具体实现的隐藏
封装
public protected 包访问权限 private 创建类的形式

类的访问权限

类既不能是private 也不是protected.(内部类除外),只有包访问权限 或 public


class Soup1{
    private Soup1(){} //必须编写默认构造器,系统不会自动创建默认构造器
    public static Soup1 makeSoup(){
        return new Soup1();//返回soup1对象的一个引用,可以做额外工作,如计数等
    }
}
class Soup2{
    private static Soup2 ps1 = new Soup2();//单例模式 private static创建, 有且仅有一个,且通过access 访问
    public static Soup2 access(){
        return ps1;
    }
    public void f(){}
}
public class Lunch {
    void testPrivate(){
//        Soup1 soup = new Soup1();
    }
    void testStatic(){
        Soup1 soup = Soup1.makeSoup();
    }
    void testSingleton(){
        Soup2.access().f();
    }
}
这个蛮有意思
import chapter6access.connection.Connection;
import chapter6access.connection.ConnectionManager;

public class E08_ConnectionManager {

    public static void main(String[] args) {
        Connection c = ConnectionManager.getConnection();
        while (c != null) {
            System.out.println(c);
            c.doSomething();
            c = ConnectionManager.getConnection();

        }
    }
}


package chapter6access.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;
    }
}


package chapter6access.connection;

public class Connection {
    private static int counter = 0;
    private int id = counter++;
    Connection(){}
    public String toString(){
        return "Connection " + id;
    }
    public void doSomething(){}
}

以上是关于Thinking in java Chapter6 访问权限控制的主要内容,如果未能解决你的问题,请参考以下文章

Thinking in Java

Thinking in java 16/3/5

Thinking in java 16/3/6

Thinking in Java

thinking in java ----reading note

thinking in java ----reading note