When to make class and function?

后端 未结 6 567
面向向阳花
面向向阳花 2020-12-17 18:10

I am a beginner to programming when I start to code I just start writing and solve the problem. I write whole program in a single main function. I don\'t know when to make c

6条回答
  •  北荒
    北荒 (楼主)
    2020-12-17 18:45

    Have a look at

    Procedure, subroutine or function?, Object-oriented programming

    An object is actually a discrete bundle of functions and procedures, all relating to a particular real-world concept such as a bank account holder or hockey player in a computer game. Other pieces of software can access the object only by calling its functions and procedures that have been allowed to be called by outsiders.

    Basically, you use a function/procedure/method to encapsulate a specific section of code that does a specific job, or for reusibility.

    Classes are used to encapsulate/represent an object with possibly its own data, and specific function/procedure/method that makes sense to use with this object.

    In some languages classes can be made static, with static function/procedure/method which can then be used as helper function/procedure/method

提交回复
热议问题