who is the big boss ???????????? CLASS vs OBJECT. 🤜🤛
Who is the big boss? CLASS vs OBJECT 🤜🤛
We encounter these two concepts, OOP, quite often while studying. But is there a difference between them? Hello everyone! I hope you are doing well. Last time, we said that “Each Object is an instance of a Class.” Let’s figure out what this sentence means and explain it together!
我们在学习 OOP 时经常会遇到这两个概念。那么,它们之间有什么区别呢?大家好!希望你们一切都好。上次我们提到:“每个对象(Object)都是类(Class)的一个实例。” 让我们一起来解读这句话,深入探讨一下吧!
To begin with, let’s define what a “Class” is. Simply put, it is what sets the rules and details; it is the general blueprint that explains how things will look. You could call it an “engineering plan.” In other words, it is just a design on paper where we set the rules. After that, we take as many copies as we need from it.
首先,让我们定义什么是“类”(Class)。简单来说,它负责制定规则和细节;它是解释事物外观的通用蓝图。你可以把它称为“工程图纸”。换句话说,它只是纸上的设计,我们在上面设定规则。之后,我们可以根据需要从中创建任意数量的副本。
So, we have understood that as long as the Class is the blueprint and the drawings, the Object is the real version that we will deal with—it is the actual work.
因此,我们明白了:如果说类(Class)是蓝图和图纸,那么对象(Object)就是我们将要处理的真实版本——也就是实际的工作成果。
Now, let’s move to the important question: What do we write inside this blueprint? Or what are the rules inside it? Let’s find out together. Since we agreed that the Class is the “Big Boss,” and the Object comes from it, it must contain details that define the shape and movement of any object.
现在,让我们进入重要的问题:我们在这个蓝图中写什么?或者说里面有哪些规则?让我们一起来看看。既然我们一致认为类(Class)是“大老板”,而对象(Object)由它产生,那么它必然包含定义任何对象形状和行为的细节。
These rules simply consist of two things, nothing else:
- Attributes: These are the numbers or data that describe it (e.g., name, age, color, or price).
- Methods: These are the behaviors or functions it can perform (e.g., printing a message, calculating an equation, or changing a specific state).
这些规则简单来说只包含两件事:
- 属性 (Attributes): 描述事物的数字或数据(例如:姓名、年龄、颜色或价格)。
- 方法 (Methods): 它能执行的行为或功能(例如:打印消息、计算公式或改变特定状态)。
I want to tell you something important so that if you come across these terms anywhere, you won’t get confused:
- Attributes (where we store data) might be referred to as: Properties, Fields, Variables, Data Members, Data Items, and State.
- Methods (which execute commands and functions) might be referred to as: Functions, Behaviors, Operations, Actions, and Member Functions.
我想告诉大家一件重要的事,这样如果你们在任何地方遇到这些术语,就不会感到困惑了:
- 属性(存储数据的地方)可能被称为:Properties, Fields, Variables, Data Members, Data Items 或 State。
- 方法(执行命令和功能的地方)可能被称为:Functions, Behaviors, Operations, Actions 或 Member Functions。
By the way, the beauty of the Class is that you write the Class once, but you can create as many Objects as you want. Each Object takes the same attributes and methods but with its own completely independent data.
顺便说一下,类(Class)的精妙之处在于:你只需编写一次类,但可以根据需要创建任意数量的对象。每个对象都拥有相同的属性和方法,但拥有各自完全独立的数据。
And so we don’t forget, I want to remind you of a very important point regarding memory: The Class does not take up any space in memory, while the Object is what takes up real space. Why? Because the Class doesn’t have actual values! When we define an attribute like “Name” or “Age” inside a Class, we are simply stating the “name of the attribute” without giving it a real value. The computer won’t reserve space for something that has no value. But as soon as we create an Object and give it real values (like Name = “Ahmed” and Age = “20”), the computer immediately reserves real space in memory to store this data.
为了不遗忘,我想提醒大家关于内存的一个非常重要的点:类(Class)本身不占用任何内存空间,而对象(Object)才会占用实际空间。为什么呢?因为类没有实际值!当我们在类中定义像“姓名”或“年龄”这样的属性时,我们只是声明了“属性名称”,而没有赋予它真实的值。计算机不会为没有值的东西预留空间。但一旦我们创建了一个对象并赋予它真实的值(例如:姓名 = “Ahmed”,年龄 = 20),计算机就会立即在内存中预留实际空间来存储这些数据。
A smart question might come to your mind now: Does the Object have to use all the attributes and functions written inside the Class? The answer is simply: No.
- For attributes: You don’t have to provide values for everything (it takes a Default Value, like 0 or null).
- For methods: The Object only calls what it needs from them.
现在你可能会想到一个聪明的问题:对象(Object)必须使用类(Class)中编写的所有属性和函数吗?答案很简单:不需要。
- 对于属性:你不必为所有内容提供值(它会采用默认值,如 0 或 null)。
- 对于方法:对象只会调用它所需要的方法。
In the end: “You write the Class once, but you live with the Objects.”
最后:“你编写一次类,但你与对象共存。”