简体   繁体   中英

I am a bit confused about the behaviour of objects in OOP

I have read that behavior of an object is the action performed by the object(in real life situation) but in programming, context is it not the operation/action performed on the object as described by the method.

For example in the code below we have a method to display the full name of the employee. In this case, the action is performed by the object or is it the action is performed on the object?. How can displaying full name be a behavior of an object when we compare it to the behavior of a dog object(Like barking, sleeping, etc). Thanks for your help

class employee:

    raise_value=1.05
    def __init__(self,first,last,pay):

        self.first=first
        self.last=last
        self.pay=pay

    def emp_fullname(self):
        return "Full name is {} {}".format(self.first,self.last)

    def set_raise(self):
        self.new_sal=float(self.pay)*employee.raise_value
        return 'My new salary is {}'.format(self.new_sal)

e1=employee("Chyanit","Singh","60000")
e2=employee("Parag","Singh","40000")

e1.emp_fullname()

The behaviour of an object is always in response to an external stimulus. In OO, a message is broadcast by "something" and your object responds in some way. If the message is a request for information, it would trigger the knowledge responsibility of the object; if the message is an event or a request to do something, it would trigger a behaviour responsibility. The operations would depend on the state of the object and the nature of the event. Sometimes an event contains a "snapshot" of the current overall state of the system that gives a context for the object operations to execute. Note that whatever the object does, it does not affect its external context.

There is a third responsibility which is to provide a service. Most OOP programming actually caters for services, which are just transforms of the contexts passed to them without any effect on the objects themselves. There is very little behaviour or knowledge responsibility in them.

In your employee class, you have both knowledge and behaviour responsibilities. An instance of the class would be able to respond to a request for the full name of the employee and to "behave" in such a way as to change its internal state, that is, getting a new salary. The only thing I would change is renaming set_raise to raise_salary to make it look less of a setter method.

Methods are performed by or on an object. It doesn't have to be one or the other. GetFullName() and SetFullName(first,last) are both methods. The former is performed by the object; like a dog barking, it gives you something. The latter is performed on the object like when you name your new dog or give it a bone.

This question may or may not help you understand what a method is: What's the difference between a method and a function?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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