java 16:可見性修飾符及單例模式

可見性修飾是用來修飾一個方法中的數據域跟方法能否在類之外被使用。

You can use the public visibility modifier for classes, methods, and data fields to denote that
they can be accessed from any other classes. If no visibility modifier is used, then by default
the classes, methods, and data fields are accessible by any class in the same package. This is
known as package-private or package-access.

In addition to the public and default visibility modifiers, Java provides the private and
protected modifiers for class members. This section introduces the private modifier. The
protected modifier will be introduced in §11.13, “The protected Data and Methods.”
The private modifier makes methods and data fields accessible only from within its own class

也就是說,用public修飾的能夠在其他類外的地方使用到,當沒有使用可見性修飾符時候,默認的就是該域可以在同一個包下的類中使用到,當使用了private修飾時候,就只能在類內使用了。出了類,其他地方都是見不到的,訪問不了的。如下所說:


要注意,可見性修飾是爲了在類外的其他類中能否訪問到該類,如果類是public的,那麼能否訪問到該類中的數據域,方法。而這些可見性修飾在本身類中的使用時完全沒區別的。

<span style="color:#ff0000;">public</span> class Foo
{
	<span style="color:#ff0000;">private</span> boolean x;
	
	public static void main(String [] args)
	{
		Foo foo=new Foo();
		System.out.println("foo.x"+<span style="color:#ff0000;">foo.x</span>);
		foo.x=true;
		System.out.println("foo.covert()="+<span style="color:#ff0000;">foo.convert</span>());
		
	}
	<span style="color:#ff0000;">private</span> int convert()
	{
		return x?1:0;
	}

	
}


但是在類之外的一個類中要去使用它,那麼就會有問題。如下:

public class Test
{
	public static void main(String [] args)
	{
		Foo foo=new Foo();//可以,因爲Foo類用public修飾
		foo.x=true; //不行,訪問不到
		foo.convert(); //不行,訪問不到
	}
}

The private modifier applies only to the members of a class. The public modifier can apply
to a class or members of a class. Using modifiers public and private on local variables would
cause a compile error.

要注意的是,private 只能用來修飾類中的成員,而不能用來修飾類。另外,在局部變量(例如方法中使用的變量)是不用加可見性修飾的,如果在局部變量中使用public,private等修飾符是會出現編譯錯誤的,可見性修飾是針對類,類的成員的。

單例模式的實現

In most cases, the constructor should be public. However, if you want to prohibit the user from
creating an instance of a class, use a private constructor. For example, there is no reason to create
an instance from the Math class, because all of its data fields and methods are static. To prevent
the user from creating objects from the Math class, the constructor in java.lang.Math is
defined as follows:
private Math() { 
}

一般情況下,我們可見性修飾是爲了類中的數據域被類外惡意進行修改,但是一般都不會阻止在類外創建某個類的對象,所以,一般情況下,我們的構造函數是public的,但是,如果有些情況下,你不想某個類在類外被創建各種各樣的對象。你可以使用private來修飾構造函數,這樣的話,這個構造函數就不能被類外所見了,就像Math方法,他的所有方法跟數據域都是static,那麼根本就沒有使用對象的必要。所以他會將他的構造函數用private修飾

在這裏順便說一下,設計模式中很重要的一個模式-單例模式,有時候爲了線程安全,爲了節省系統資源,如果不是需要很多的對象。可以讓一個類只有一個對象。

他的實現方式有很多,將構造函數用private修飾就是一種:如下:

  public class Singleton {
    private final static Singleton INSTANCE = new Singleton();
 
    // Private constructor suppresses   
    private Singleton() {}
 
    // default public constructor
    public static Singleton getInstance() {
        return INSTANCE;
    }
  }


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章