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;
    }
  }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值