#include <iostream>
#include <string>
using namespace std;
class Student{
public:
std::string name;
//静态属性 static 数据类型 属性名
static std::string classname;
//静态方法 static 返回值类型 函数
static void sayhello();
};
//赋初始值 数据类型 static 属性名=值
std::string static classname="117";
//类外实现,前面不加static
void Student::sayhello() {
Student::classname="118";
}
int main() {
//输出 类名::属性名
cout<<Student::classname<<endl;
Student::classname="118";
cout<<Student::classname<<endl;
//调用静态方法 类名::函数名
Student::sayhello();
return 0;
}
c++中的静态方法和静态属性
最新推荐文章于 2024-12-13 16:40:44 发布