1. Historically, emperors had only one person in history. In order to ensure their uniqueness, the ancients used to increase the "anti-counterfeiting logo" approach, such as the seal. The simpler way is to limit the creation of the emperor. In this case, the singleton pattern is used to guarantee the uniqueness of the emperor. The instance runs as follows:
Code:
(1)Empreror.java: * (Definition of the Emperor Class)
1 Packagehimi.only;2 3 Public classEmpreror {4 Private StaticEmpreror Empreror =NULL;5 PrivateEmpreror () {6 7 }8 Public Staticempreror getinstance () {9 if(Empreror = =NULL) {TenEmpreror =Newempreror (); One } A returnEmpreror; - } - the Public voidGetName () { -System.out.println ("The first emperor in Chinese history: Qin Shihuang won the Government"); - } - +}
(2) Define a testing class test:
Package himi.only; Public class Test { publicstaticvoid main (string[] args) { Empreror.getinstance (). GetName (); }}
2. Operation result :
Java Basic Knowledge Hardening 13:java single case model use