标签:
#include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> using namespace std; class Singleton { public: static Singleton* getInstance(); private: Singleton(); Singleton(const Singleton&); Singleton& operator=(const Singleton&); static Singleton* instance; }; Singleton::Singleton() { } Singleton::Singleton(const Singleton&) { } Singleton& Singleton::operator=(const Singleton&) { } Singleton* Singleton::instance = new Singleton(); Singleton* Singleton::getInstance(){ return instance; } int main(){ Singleton* singleton1 = Singleton::getInstance(); Singleton* singleton2 = Singleton::getInstance(); return 0; } package dsssd; class Emperor { private static Emperor emperor = null; private Emperor() { } public static Emperor getInstance() { if (emperor == null) { emperor = new Emperor(); } return emperor; } public void getName() { return ; } } class aaaa { public static void main(String[] args) { Emperor emperor1 = Emperor.getInstance(); emperor1.getName(); Emperor emperor2 = Emperor.getInstance(); emperor2.getName(); } }
标签:
原文地址:http://www.cnblogs.com/13224ACMer/p/5563103.html