Design mode Singleton - birth articles
Mom is never given to her kid, but a daughter is always worried, this is not recently, I don't know that from there, I will not stop in my ear. Saying a child, I have to mention the family planning policy of our country, "I only have a good, the children are the same", "less birth, Qiji, happiness", etc. These slogans are full, family planning policies are indeed For my country's control of the population, it has set up the horses, but I feel that many people really want to be born in this large group of education, medical industrialization, etc., at least I think so. Hey, it seems that it is far away, well, let us return to start.
1, we define children (Child) class:
Public class child {private static child mychild = null; // My child private child () {// 生 孩子 (mychild == null) {system.out.println ("you still No children, can be born! "); Mychild = new child ();} else {system.out.println (" You already have children, can not be regenerated! ");} Return mychild;}} 2, write test class:
Public class test {
Public static void main (string args []) {
Child.getChild ();
Child.getChild (); // When you have a child and then want to have one, you will not be regenerated.
}
}
3, description:
A: Definition: The role of the Singleton mode is to ensure that a class class Class exists in a Java application.
B: In fact, this example can also be implemented by using the synchronized keyword. The brother's article is clear about this, and there is not much to say it here.
4, thank you: I would like to thank ZDR29473, Ling Han, FlylyKe and other major netizens, now have a modification of the code, I hope everyone will pay more valuable advice, let us make progress together, thank you again.