码迷,mamicode.com
首页 > 其他好文 > 详细

如何让一个类不能被实例化

时间:2017-03-05 12:18:13      阅读:1005      评论:0      收藏:0      [点我收藏+]

标签:工具   double   extends   类型   div   openjdk   row   open   this   

本文涉及的概念:
1.在面向对象编程中,在什么场景下会需要做到让一个类不能被实例化
2.让一个类不能被实例化的方法
 
 
什么样的场景会需要让一个类不能被实例化
工具类(utility class),实例化对它们没有意义的工具类。这时候,就要做到不让该类被实例化
java.lang.Math:
把与Math相关的基本类型的值和相关的方法组合到该Math类中。Math类不需要实例化,使用了私有构造器。不过,它可以在Math类中内部被实例化。
使用私有构造器,没有子类;使用final修饰,没有子类。
package java.lang;
import java.util.Random;
 
public final class Math {
 
  /**
  * Don‘t let anyone instantiate this class.
  */
  private Math() {}
 
  /**
  * The {@code double} value that is closer than any other to
  * <i>e</i>, the base of the natural logarithms.
  */
  public static final double E = 2.7182818284590452354;
 
  public static final double PI = 3.14159265358979323846;
 
  public static double sin(double a) {
  return StrictMath.sin(a); // default impl. delegates to StrictMath
  }
 
....    
  public static double toRadians(double angdeg) {
  return angdeg / 180.0 * PI;
  }
 
  public static float scalb(float f, int scaleFactor) {
  return sun.misc.FpUtils.scalb(f, scaleFactor);
  }
}
 
 
让一个类不能被实例化的方法
0.不写显式的构造器---不可行,编译器会自动提供一个公有的,无参的缺省的构造器
1.抽象类的方式---不可行
子类可以继承父类,从而实例化
public abstract class Parent {
 
public Parent() {
}
 
public static void main(String[] args){
 
// Parent one = new Parent();
}
}
 
public class Child extends Parent {
 
public Child() {
}
 
}
 
 
2.使用私有构造器----可行,需要在构造器中添加限制
在使用私有构造器的基础下,再在构造方法中返回一个异常,因为虽然外部类无法实例化该类,但是内部类可以实例化该类;另外,通过反射的方式,还是可以实例化该类:通过反射的方式来实例化Elvis类(Elvis类有私有构造器)
  1.         Class<?> classType = Elvis.class;  
  2.         Constructor<?> c = classType.getDeclaredConstructor(null);  
  3.         c.setAccessible(true);  
  4.         Elvis e1 = (Elvis)c.newInstance();  
 
 
public class Elvis{
 
     private Elvis(){
         throw new AssertionError();
     }
...
}
 
 
引用:
 
 
 

如何让一个类不能被实例化

标签:工具   double   extends   类型   div   openjdk   row   open   this   

原文地址:http://www.cnblogs.com/ttylinux/p/6504822.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!