java in a class do not want to be inherited how to do?

Method one: the class final

Method two: declaration of the class constructor is private, class provides a static method, to complete the initialization of the class. The following code:

public class Base{
    private Base(){}
    public static Base ini(){
        Base a=new Base();
        return a;
    } 
    public static void main(String[] args) {
        Base base=Base.ini();
    }
}

 

Guess you like

Origin www.cnblogs.com/Luck-365/p/11745411.html