代码改变世界

理解面向对象程序设计 1

2012-10-21 20:34  atois  阅读(199)  评论(0)    收藏  举报
  • 面向对象中的重要概念:类(class)与对象(object)
  • In object-oriented programming, a class is a construct that is used to create instances of itself – referred to as class instances, class objects, instance objects or simply objects. A class defines constituent members which enable its instances to have state and behavior.[1] Data field members (member variables or instance variables) enable a class instance to maintain state. Other kinds of members, especially methods, enable the behavior of class instances. Classes define the type of their instances.[2]

    A class usually represents a noun, such as a person, place or thing, or something nominalized. For example, a "Banana" class would represent the properties and functionality of bananas in general. A single, particular banana would be an instance of the "Banana" class, an object of the type "Banana".

    类是一个抽象的概念,包含有数据和对数据的控制

    The concept of classes

    A class is the structure of an object, meaning the definition of all items that an object is made up of. An object is therefore the "result" of a class. In reality, an object is an instance of a class, which is why can use interchange the terms object or instance (or even occurrence).

    A class is made up of two parts:

    • Attributes (often called member data): this is the data that refers to the object's state
    • Methods (often referred to as member functions): these are functions that can be applied to objects

    If we have a class called car, the objects Peugeot 406 and Renault 18 will be instances of this class. There may also be other objects Peugeot 406, differentiated by their model number. Furthermore: two instances of a class may have the same attributes but may be considered separate distinct objects. In a real world scenario: two T-shirts may be identical, but they are however distinct from each other. However, if we mix them together it is impossible to distinguish one from the other.