public class Class { // 静态成员变量 public static int count = 0; // 成员变量 private int id; private String name; // 构造方法重载 public Class() { this.id = ++count; this.name = "default"; } public Class(String name) { this.id = ++count; this.name = name; } // 成员方法重载 public void print() { System.out.println("MyClass " + this.id + ": " + this.name); } public void print(int times) { for (int i = 0; i < times; i++) { System.out.println("MyClass " + this.id + ": " + this.name); } } // 静态成员方法 public static void printCount() { System.out.println("Total count: " + count); } } public class Example01 { public static void main(String[] args) { // 创建对象 Class obj1 = new Class(); Class obj2 = new Class("Alice"); // 调用成员方法 obj1.print(); obj2.print(2); // 调用静态成员方法和静态成员变量 Class.printCount(); System.out.println("Total count from obj1: " + obj1.count); }代码解释
时间: 2023-06-15 11:05:23 浏览: 73
这段 Java 代码定义了一个名为 Class 的类,其中包含了静态成员变量 count 和成员变量 id 和 name,以及构造方法和成员方法的重载,还包含了一个静态成员方法 printCount。在主函数 Example01 中,首先创建了两个 Class 类的对象 obj1 和 obj2,然后分别调用了它们的 print 方法打印出对象的信息。接着调用了 Class 类的静态成员方法 printCount 打印出对象创建的总数。最后,还尝试通过 obj1 访问静态成员变量 count,但这是不被允许的,因为静态成员变量属于类本身,而不是类的实例。
相关问题
1. 完善类Student,使用静态成员变量统计在程序运行期间Student类的对象数目,在main中通过调用成员函数输出当前对象数目。 #include <iostream> using namespace std; class Student{ private: int id; string name; public: Student(int no,string s):id(no),name(s){} }; int main(){ Student lm(202011050001 ,"李美"); return 0; }
可以在类的定义中添加一个静态成员变量,用于统计对象数目。同时,在类的构造函数和析构函数中分别对静态成员变量进行加减操作,实现统计对象数目的功能。下面是完整的代码示例:
```
#include <iostream>
using namespace std;
class Student {
private:
int id;
string name;
static int count; // 静态成员变量,用于统计对象数目
public:
Student(int no, string s) : id(no), name(s) {
count++; // 每创建一个对象,对象数目加1
}
~Student() {
count--; // 每销毁一个对象,对象数目减1
}
static int getCount() { // 静态成员函数,用于获取对象数目
return count;
}
};
int Student::count = 0; // 静态成员变量需要在类外进行初始化
int main() {
Student lm(202011050001, "李美");
cout << "当前对象数目:" << Student::getCount() << endl;
return 0;
}
```
运行结果:
```
当前对象数目:1
```
#include<iostream> using namespace std; class Student { private: int age=0; //年龄 string name="NoName"; //姓名 public: static int count; //静态成员,表示学生人数 Student(int a, string); Student() { //age = 0; //name = "NoName"; count++; }; ~Student() {}; void Print(); }; int Student::count = 0;//类外初始化 Student::Student(int a, string n) { this->age = a; this->name = n; } void Student::Print() { cout << "Name=" << this->name << ", " << "age=" << this->age << endl; } int main() { cout << "count=" << Student::count << endl; Student s1, * p = new Student(23, "ZhangHong"); //Student s1; Student *p=new Student(23,"ZhangHong"); s1.Print(); p->Print(); delete p; s1.Print(); Student Stu[4]; cout << "count=" << Student::count << endl; return 0; }
这段代码定义了一个学生类(Student),其中包含一个表示年龄的私有成员变量(age)、一个表示姓名的私有成员变量(name)和一个表示学生人数的静态成员变量(count)。类中定义了两个构造函数,一个是有参构造函数,另一个是无参构造函数,有参构造函数用于初始化年龄和姓名,无参构造函数用于初始化count,并且将count加1。类中还有一个打印函数Print(),用于输出学生的姓名和年龄。最后在main函数中,通过无参构造函数和有参构造函数创建两个学生对象s1和p,并分别调用Print()函数输出它们的信息,然后通过delete释放指针p所指向的内存,再次调用s1的Print()函数输出它的信息,最后创建一个包含4个学生对象的数组Stu,并输出当前的学生人数count。
该代码的输出结果如下:
```
count=0
Name=NoName, age=0
Name=ZhangHong, age=23
Name=NoName, age=0
count=5
```
可以看到,最初的学生人数count为0,s1的名字和年龄都是默认值,p的名字是“ZhangHong”,年龄是23。释放p所指向的内存后,s1的名字和年龄仍然是默认值。最终,学生人数为5,包括s1、p和Stu数组中的4个学生对象。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)