首页 > 编程语言 > 详细

Effective Java 英文 第二版 读书笔记 Item 4:Attempting to enforce noninstantiability by making a class abstract does not work.

时间:2015-09-04 17:04:41      阅读:188      评论:0      收藏:0      [点我收藏+]

 

The class can be subclassed and the subclass instantiated.Futhermore,it misleads the user into thinking the class was designed for inheritance(继承). There is,however,a simple idiom to ensure noninstantiability.A default constructor is generated only if a class contains no explicit constructors,so a class can be made noninstantiable by including a private constructor.

 

package noninstantiability;

//Noninstantiable utility class
public class UtilityClass {
    //Suppress default constructor for noninstantiability
    
    private UtilityClass(){
        throw new AssertionError();
    }

    
}

 

Effective Java 英文 第二版 读书笔记 Item 4:Attempting to enforce noninstantiability by making a class abstract does not work.

原文:http://www.cnblogs.com/linkarl/p/4781874.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!