> For the complete documentation index, see [llms.txt](https://jen-hsuan-hsieh.gitbook.io/python/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jen-hsuan-hsieh.gitbook.io/python/chapter1notes-from-research/chapter-2-write-python-code/38class.md).

# 3.5.Class

* 目的
  * 提供開發者**自定義**客製化的物件類別, 來描述我們想描述的物體, 這是**物件導向**
* 概念
  * 1.類別中有**方法**, **屬性**. 將定義好的一些函式及變數捆成一包, 這一包可以想成一個類別, **函式**就是**方法**, **變數**就是**屬性**&#x20;
  * 2.類別被使用前必須被**實例化**, 我們必須要有**實例(instance)**&#x624D;可以使用
    * 舉例如言, 我們想描述車子, 那要描述車子類別可以分為:
      * 1.屬性: 車上的輪子, 車子大小, 車子顏色等&#x20;
      * 2.方法: 車子加速的方式等
* Python中定義類別的方法
  * **class**
  * ***init***: 每個class中必備的函式, 控制物件被初始化時要做的事
* 建立物件實例
* 舉車子的例子為例

  ```
        class car:
            """constructor"""
            def __init__(self, wheel_size, color, speed):
                self.wheel_size = wheel_size
                self.color = color
                self.speed = speed
            """Function to accelerate"""
            def accelerate(self):
                self.speed = self.speed * 4

        """create a instance"""
        new_car = car(10, "red", 50)
        print new_car.color
        print new_car.wheel_size
        print new_car.speed
        new_car.accelerate()
        print new_car.speed
  ```
