简体   繁体   English

有关CLASSES的基本问题

[英]Basic question about CLASSES

If I create a class called Person, 如果我创建一个名为Person的类,

the class should only know about the instantiated object? 该类应该只知道实例化的对象? So if I have 5 Person objects, should they know about each other? 因此,如果我有5个Person对象,他们是否应该彼此了解?

A class is simply a definition or a template. 类仅仅是定义或模板。

So it is like asking if a blueprint for a house knows about the houses that were created from that blueprint. 因此,这就像询问房屋的蓝图是否了解根据该蓝图创建的房屋。

Then furthermore it is like asking if 5 houses built from the same blueprint know about each other. 再者,这就像询问是否根据同一蓝图建造的5栋房屋彼此了解。

In both cases the answer is no. 在两种情况下,答案是否定的。

No, actually every time an object is created a memory is allocated for storing its variables, But they can share the member functions of that class. 不,实际上每次创建对象时,都会分配一个内存来存储其变量,但是它们可以共享该类的成员函数。 Means the member functions will be the same for every objects. 意味着每个对象的成员函数都相同。 But if the variable is declared as STATIC then there will be a single copy. 但是,如果将变量声明为STATIC,则将只有一个副本。 For example if a variable 'count' is declared as static then we can use it to check how many objects are created by incrementing it every time an object is created. 例如,如果将变量“ count”声明为静态变量,那么我们可以使用它来检查每次创建对象时将其递增以检查创建了多少个对象。 This can be done in the constructor of the class 这可以在类的构造函数中完成

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM