//對象與對象之間的成員變量是相互獨立的.要想共用數據,則需要使用靜態成員或靜態方法 //#只要在類中聲明靜態成員變量,即使不定義對象,也可以為靜態成員變量分配空間,進而可以使用靜態成員變量.(因為靜態成員變量在對象創建之前就已經被分配了內存空間) //#靜態成員變量雖然在類中,但它並不是隨對象的建立而分配空間的,也不是隨對象的撤銷而釋放(一般的成員在對象建立時會分配空間,在對象撤銷時會釋放).靜態成員變量是在程序編譯時分配空間,而在程序結束時釋放空間. //#初始化靜態成員變量要在類的外面進行.初始化的格式如下:數據類型 類名::靜態成員變量名 = 初值; //#不能用參數初始化表,對靜態成員變量進行初始化. //#即可以通過類名來對靜態成員變量進行引用,也可以通過對象名來對靜態成員變量進行引用.
//普通成員函數和靜態成員函數的區別是:普通成員函數在參數傳遞時編譯器會隱藏地傳遞一個this指針.通過this指針來確定調用類產生的哪個對象;但是靜態成員函數沒有this指針,不知道應該訪問哪個對象中的數據,所以在程序中不可以用靜態成員函數訪問類中的普通變量.
#include <iostream> using namespace std; class CShop { public: CShop(int size); void ShowSize(); static void ShowPrice(); //聲明靜態成員函數用來顯示價格 static int ChangePrice(int price); //聲明靜態成員函數用來更改價格 private: int m_size; //聲明一個私有成員變量 static int m_price; //聲明一個私有靜態成員變量 }; CShop::CShop(int size) { m_size = size; } void CShop::ShowSize() { cout << "商品數量:" << m_size << endl; } void CShop::ShowPrice() { cout << "商品價格:" << m_price << endl; } int CShop::ChangePrice(int price) { m_price = price; return m_price; } int CShop::m_price = 100; //初始化靜態成員變量 int main(int argc, char * argv[]) { CShop::ShowPrice(); CShop::ChangePrice(200); CShop::ShowPrice(); CShop shop(50); shop.ShowSize(); shop.ShowPrice(); return 0; }
#include<iostream> #include<string> using namespace std; class player { string playerName; //玩家姓名 int attack; //攻擊力 int defense; //防御力 int health = 100; //生命值 int maxHealth = 100; //最大生命值 public: static int playerNumber; //構造函數 總數+1 player(int attackParam, int defenseParam, string nameParam) { attack = attackParam; defense = defenseParam; playerName = nameParam; ++playerNumber; } //析構 總數減1 ~player() { --playerNumber; } //獲取攻擊力 int getAttack() { return attack; } //設置攻擊力 void setAttack(int attackParam) { attack = attackParam; } //獲取防御力 int getDefense() { return defense; } //設置防御力 void setDefense(int defenseParam) { defense = defenseParam; } //改變生命值 void changeHealth(int healthParam) { health += healthParam; if (health > maxHealth) { health = maxHealth; } else if (health <= 0) { health = 0; cout << "Player " << playerName << " is Dead ! " << endl; delete this; } } //顯示屬性 void displayProperty() { cout << "名字:" << playerName << endl; cout << "攻擊力:" << attack << endl; cout << "防御力:" << defense << endl; cout << "生命值:" << health << endl; } void attackPlayer(player* otherPlayer) { otherPlayer->changeHealth(-this->attack); } static int displayNumber() { cout << playerNumber << endl; return playerNumber; } }; int player::playerNumber = 0; int main() { player* player01 = new player(50, 50, "GSL"); player* player02 = new player(70, 60, "LJL"); player01->displayProperty(); player02->displayProperty(); if (player01) { player01->attackPlayer(player02); } if (player02) { player02->attackPlayer(player01); } cout << player::playerNumber << endl; if (player01) { player01->attackPlayer(player02); } if (player02) { player02->attackPlayer(player01); } cout << player::playerNumber << endl; system("pause"); return 9696969; }
https://blog.csdn.net/ttt301/article/details/52326067
https://blog.csdn.net/longyanbuhui/article/details/71404308
