or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing
xxxxxxxxxx
Python
實作前導課程:class & object Part II
2022 / 4 / 28
Tony
Class Attributes
@classmethod
@staticmethod
Class Attributes
前面都只有創建綁定在物件上的屬性
但其實class可以創建綁定class的屬性
這樣整個class都共用一個屬性
不會因為不同的物件而改變
Example
@classmethod
有時你會需要class的函式操作的東西是class attributes
且不想要透過創造物件來執行
這種時候就可以使用 @classmethod
使用方式:寫在你不想用物件呼叫的函式上方
這時引數self會變成cls,class的意思
Example
@staticmethod
有時候,class只是用來整理function
例如你把一堆數學相關的function
丟進一個Math的class
這種時候你根本不想要呼叫物件來執行function
只想單純呼叫
這種時候就可以用@staticmethod
使用方式:寫在該函式上方