1.目的:保证一个类只可创建一个对象
2.实现:
①构造函数私有化
类外不可创建对象
构造函数私有的类不可被继承
②类的静态成员:
类的静态成员属于类而不属于对象
类的静态成员不需要通过对象/指向对象的指针引用
类的静态成员在引用时,要遵循访问权限
3.类的静态成员
①类的静态数据成员在类内定义,类外初始化
类内定义: static 数据类型 静态成员名;
类外初始化: 数据类型 类名::静态成员名 = 初始值;
②静态数据成员必须初始化,且只可在类外初始化
③在编译阶段,系统就会为类的静态数据成员分配单元,存储值
#include<bits/stdc++.h>
using namespace std;
class My{
public:
static int sx;
};
int My::sx = 3;
int main(){
//cout<<sx;//错误 未定义sx
cout<<My::sx<<endl;
My::sx = My::sx + 2;
cout<<My::sx<<endl;
}
4.静态数据成员的访问:
————静态数据成员不通过对象访问
①静态数据成员为私有成员: 类名::静态数据成员名
————静态方法
静态方法定义: static 数据类型 静态方法名(参数)
{
}
①静态方法不通过对象调用
②在静态方法中,可调用静态数据
5.区分静态数据和非静态数据
静态数据属于类,直接初始化
非静态数据属于对象,通过构造函数初始化,通过对象调用
前提是访问权限要够
#include<bits/stdc++.h>
using namespace std;
class My{
private:
static int sx;
};
int My::sx = 3;
int main(){
cout<<My::sx<<endl;//访问权限不够
}
#include<bits/stdc++.h>
using namespace std;
class My{
private:
static int sx;
public:
static void Print(){
cout<<sx<<endl;
}
};
int My::sx = 3;
int main(){
My::Print();
//My::sx = My::sx + 2;//访问权限不够
}
6.声明类My,有数据成员x(默认值为0)
①声明构造函数初始化x
②创建对象my1(3),my2(5),new My(7)
要求:将三个对象的x成员之和存入一个静态数据sum中,然后输出sum(通过静态方法AddPrint来实现)
/*#include<bits/stdc++.h>
using namespace std;
class My{
private:
int x;
static int sum;
public:
My(){
x = 0;
}
My(int x){
this->x = x;
}
static void AddPrint(int t1,int t2,int t3){
sum = t1 + t2 + t3;
cout<<sum<<endl;
}
int getx(){
return x;
}
};
int My::sum = 0;
int main(){
My my1(3),my2(4);
My *p1 = new My(7);
My::AddPrint(my1.getx(),my2.getx(),p1->getx());
}*/
#include<bits/stdc++.h>
using namespace std;
class My{
private:
int x;
static int sum;
public:
My(){
x = 0;
}
My(int x){
this->x = x;
}
static void AddPrint(My t1,My t2,My t3){
sum = t1.x + t2.x + t3.x;
cout<<sum<<endl;
}
};
int My::sum = 0;
int main(){
My my1(3),my2(4);
My *p1 = new My(7);
My::AddPrint(my1,my2,*p1);
}
7.单例模式代码
#include<bits/stdc++.h>
using namespace std;
class Singleton{
private:
static Singleton *Instance;
Singleton(){
cout<<"Singleton"<<endl;
}
public:
static Singleton *Create(){
if(!Instance){
Instance = new Singleton();
}
return Instance;
}
};
Singleton *Singleton::Instance = NULL;
int main(){
Singleton *p = Singleton::Create();
Singleton *q = Singleton::Create();
Singleton *m = Singleton::Create();
}
package 设计模式;
import java.util.Scanner;
//即最多只能创建一个实例
//懒汉式,线程不安全
public class 单例模式 {
private static int x;
private static 单例模式 Singleton = null;
private 单例模式(){
x = 0;
System.out.println("1111");
}
private 单例模式(int x){
this.x = x;
System.out.println("2222");
}
public static 单例模式 Singleton(){
if (Singleton == null){
Singleton = new 单例模式();
}
return Singleton;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
单例模式 p = 单例模式.Singleton();
单例模式 q = 单例模式.Singleton();
单例模式 m = 单例模式.Singleton();
System.out.println(p.x);
System.out.println(q.x);
System.out.println(m.x);
}
}
package 单例模式;
//单例模式可以延迟实例化
//懒汉式,线程不安全
/*public class Singleton {
private static Singleton uniqueInstance;
private Singleton(){
System.out.println("This is SingleTonn");
}
public static Singleton getInstance(){
if (uniqueInstance==null){
uniqueInstance = new Singleton();
}
return uniqueInstance;
}
}*/
/*处理多线程:通过增加Synchronized关键字到getInstance()方法中,这样迫使每个线程在进入这个方法前
要先等待别的线程离开该方法。也就是说不会有两个线程可以同时进入这个方法。 但是,同步会降低性能。
*/
import com.sun.org.apache.xerces.internal.parsers.CachingParserPool;
//如果将getInstance()的程序使用在频繁运行的地方,就不能使用加锁或者延迟创建对象的方式。
//懒汉式,线程安全
/*public class Singleton{
private static Singleton uniqueInstance = null;
private Singleton(){}
public static synchronized Singleton getInstance(){
if (uniqueInstance==null){
uniqueInstance = new Singleton();
}
return uniqueInstance;
}*/
//饿汉式,线程安全
/*public class Singleton{
private static Singleton uniqueInstance = new Singleton();//保证了线程安全
private Singleton(){}
public static Singleton getInstance(){
return uniqueInstance;
}
}*/
//第二种方案是:用“双重检查加锁”,在getInstance()中减少使用同步
//首先检查是否实例已经创建了,如果还没创建,才进行同步。这样就保证了,只有第一次会同步。
/*public class Singleton{
//volatile关键字确保:当uniqueInstance变量被初始化成Singleton实例时,多个线程正确地处理uniqueInstance变量
private volatile static Singleton uniqueInstance;
private Singleton(){}
public static Singleton getInstance(){
if (uniqueInstance == null){//只有第一次才彻底执行这里的代码
synchronized (Singleton.class){
if (uniqueInstance == null){//进入区块后,再检查一次。如果仍是null,才创建实例
uniqueInstance = new Singleton();
}
}
}
return uniqueInstance;
}
}*/
//注意:你如果使用多个类加载器,可能会导致单件(例/身)模式失效而产生多个实例
//静态(类级)内部类
//类级内部类,也就是静态的成员式内部类,该内部类的实例与外部类的实例
// 没有绑定关系,而且只有被调用到时才会装载,从而实现了延迟加载
public class Singleton{
private Singleton(){}
private static class SingletonXxx{
//静态初始化器,由JVM来保证线程安全
private static Singleton singleton = new Singleton();
}
public static Singleton getInstance(){
return SingletonXxx.singleton;
}
}