Object oriented programming - class design confusi

2019-03-19 05:19发布

I am trying to wrap my head around object oriented programming.

My understanding is that we have objects so we can design our programs to mirror real-life objects.

Let's take a class hierarchy:

class Fruit {
    void Eat() {

    }
}

class Apple extends Fruit {

}

Obviously, you can use Fruit polymorphically if Eat() is virtual. But does this make sense? Fruit cannot eat itself!

Should a fruit object rather be passed to a human object which has a Eat() function?

I am trying to figure out the correct way to think about this. How closely, in general, should programming objects mirror real-life objects?

13条回答
对你真心纯属浪费
2楼-- · 2019-03-19 05:54

Most human languages follow a sentence structure (For simple statements) of Subject Verb Object

In OOP languages like c++, not entirely ironically, the object is the object, and it comes first, so the normal order is reversed:

So this is the 'basic' pattern in c++ :-

object.verb( subject );

This is actually rubbish. Most classes are designed with a subject.verb(object) interface. Knowledge of SVO however does allow one to test whether or not a class interface has been designed to be the correct way around (in this case, it has not).

That said, there are a large number of human languages that are naturally OVS, or some other variant where the typical english order is reversed. When dealing with internationally developed software other developers might have a different idea as to the correct and normal order of subjects, and objects in a simple statement.

查看更多
登录 后发表回答