1 Packagecom.design;2 3 /**4 * Test Class5 * @authorAdministrator6 *7 */8 Public classTest {9 /**Ten * Main function One * @paramargs A */ - Public Static voidMain (string[] args) { -Singleton First_inst =singleton.getinstance (); theFirst_inst.setstring ("8288282"); - -Singleton Sencond_inst =singleton.getinstance (); -System.out.println ("Sencond_inst:" +sencond_inst.getstring ()); +Sencond_inst.setstring ("I am a single instance"); - +Singleton Third_inst =singleton.getinstance (); ASystem.out.println ("Third_inst:" +third_inst.getstring ()); at } - } - - /** - * Single Instance - * @authorAdministrator in * - */ to classSingleton + { - /** the * Get Instance * * @return $ */Panax Notoginseng Public StaticSingleton getinstance () - { the returnMyinst; + } A the /** + * Get String - * @return $ */ $ PublicString getString () - { - returnstr; the } - Wuyi /** the * Set String - * @paramStr Wu */ - Public voidsetString (String str) About { $ This. str =str; - } - - /** A * Create an instance + */ the Private StaticSingleton Myinst =NewSingleton (); - $ /** the * Test Code the */ the PrivateString str; the - /** in * Privatization Constructor the */ the PrivateSingleton () About { thestr = ""; the } the}
sencond_inst:8288282
Third_inst: I am a single instance
Create---Single-instance Singleton