C/Cxx-面向对象中Message的概念

Programming Abstractions in C++ pp.129遇到了面向对象中messages这个概念:

在这里插入图片描述

  1. “In the object-oriented world, objects communicate by sending information and requests from one object to another. Collectively, these transmissions are called messages. The act of sending a message corresponds to having one object invoke a method that belongs to a different object. For consistency with the conceptual model of sending messages, the object that initiates the method is called the sender, and the object that is the target of that transmission is called the receiver.” How to understand this sentence?

This passage describes a fundamental concept in object-oriented programming (OOP) — the idea of objects communicating with each other through the mechanism of sending and receiving messages. Here’s a breakdown to help understand this concept:

Objects: In OOP, an object is an instance of a class (一个对象是一个类的实例). A class defines a blueprint for an object, including its data (attributes or properties) and behaviors (methods or functions).
Messages: When we say objects communicate by sending messages, we’re referring to the process of one object calling a method of another object. In OOP, a message is not like a text message or an email; instead, it’s a metaphor for invoking methods on objects.
Sending a Message: This corresponds to one object (the sender) calling or invoking a method that belongs to another object (the receiver). For example, if we have an object dog of class Dog and we call dog.bark(), we are sending a bark message to the dog object.
Method Invocation: The method that is called or invoked on the receiver object is defined within the class of that object. It determines how the object responds to the message. Using the previous example, the bark method defined in the Dog class dictates what happens when the bark message is sent to a Dog object.
Sender and Receiver: In the context of a message:
The sender is the object that initiates the method call.
The receiver is the object whose method is being called.

Conceptual Model: The idea of “sending messages” is more of a conceptual model in OOP. It helps to think about objects as independent entities that interact by sending requests (messages) to each other, rather than directly accessing each other’s data or state.

class Dog {
public:
   void bark() {
       std::cout << "Woof!" << std::endl;
   }
};

int main() {
   Dog myDog;        // 'myDog' is an object of class 'Dog'
   myDog.bark();     // Sending a 'bark' message to 'myDog'
}

In this example:

  • myDog.bark() is akin to sending a bark message to the myDog object.
  • myDog is the receiver of the message, and the main function (or the runtime environment that calls main) can be thought of as the sender.

This model of communication encapsulates (封装) behavior within objects and promotes a design where objects interact through well-defined interfaces, a key principle in object-oriented design.

  • 22
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ocodotial

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值