# 2020/11/23 計算機程式設計
###### tags: `Python`
```python=
from inspect import getsource
getsource(某class)#印出該東西的定義
```
### Wrong Case
```python=
class stuff:
counter=0
def func():
counter+=1##undefined -> Wrong!!
print(counter)
for i in range(5):
stuff.func()
```
### Correct Case
```python=
class stuff:
counter=0
def func():
stuff.counter+=1##Correct!!
print(stuff.counter)
for i in range(5):
stuff.func()
```
### Example1(繼承class)
```python=
class A:
var1=1
var2=2
class B(A):#繼承A的物件
var3=3
class C(B):#繼承B的物件
var4=4
var1='1_prime'#修改C中var1的值
B.var3='3_prime'
print(A.var1)
print(C.var1)
print(C.var3)
output-----------------
1
1_prime
3_prime
```
### Example2
```python=
x= int(3.0)
```
其實也是建立一個名為x的int類別object
### Example3(分class and obj)
```python=
class bike:
tire=2
a=bike()
b=bike()
c=bike()
print('1:',bike.tire,a.tire,b.tire,c.tire)
a.tire=6
print('2:',bike.tire,a.tire,b.tire,c.tire)
bike.tire=4#概念有點像改default value,若沒有被改過則等於這個
print('3:',bike.tire,a.tire,b.tire,c.tire)
bike.color='red'
print('4:',bike.color,a.color,b.color,c.color)
a.brake='disk'
print('5:',a.brake)
print('6:',bike.brake)
output-----------------
1: 2 2 2 2
2: 2 6 2 2
3: 4 6 2 2
4: red red red red
5: disk
6:Error(Undefined)
```
### Example4(class的函式 與 obj的函式)
#### case 1
```python=
class stuff:
id=3
def showid():
print(stuff.id)
print(stuff.id)
stuff.showid()
a=stuff()
print(a.id)
a.showid()
output-----------------
3
3
3
Error
```
#### case 2
```python=
class stuff:
id=3
def showid(self):
print(self.id)
print(stuff.id)
stuff.showid()
a=stuff()
print(a.id)
a.showid()
output-----------------
3
Error
3
3
```
#### case 3
```python=
class stuff:
id=3
def showid(self):
print(self.id)
print(stuff.id)
a=stuff()
stuff.showid(a)#不能使用stuff.showid() 因為要傳參數
#硬要可以使用stuff.showid(stuff)
print(a.id)
a.showid()
output-----------------
3
3
3
3
```
#### case 4
```python=
class stuff:
id=3
def showid_obj(self):
print(self.id)
def showid_class():
print(stuff.id)
stuff.showid_class()
a=stuff()
a.id=4
a.showid_obj()
output-----------------
3
4
```
#### case 5
```python=
class stuff():
id=3
def showid_obj(self): print(self.id)
@classmethod
def showid_class(cls): print(cls.id)
a=stuff()
a.id=5
a.showid_obj()
stuff.showid_class()
a.showid_class()
output-----------------
5
3
3
```
### Example 5(@classmethod)
```python=
class parent():
id=3
@classmethod
def showid(cls): print(cls.id)
class child(parent):
id=5
parent.showid()
child.showid()
output-----------------
3
5
```
### Example 6(@staticmethod)
當沒有要用到一個obj的其他資料時
the variables don't vary from different objs
```python=
class phone():
model='galaxy'
@staticmethod
def shutdown():
print('Bye now')
phone.shutdown()
a=phone()
a.shutdown()
output-----------------
Bye now
Bye now
```
### Example 7(__init__())
#### case 1
```python=
class stuff():
def __init__(self):
print(1)
a=stuff()##會跑一次
a.__init__()##會跑一次
stuff.__init__()##會跑一次
output-----------------
1
1
1
```
#### case 2
```python=
class stuff():
def __init__(self,a,b,c):
print('sum=',a+b+c)
a=stuff(3,4,5)
a.__init__(1,1,2)
b=stuff('3','4','5')
b.__init__('1','1','2')
output-----------------
12
4
345
112
```
### Example 8
```python=
class character:
def __init__(self,ln,fn):
self.__lastname=ln#private
self.__firstname=fn#private
def self_introduction(self):
prinf(self.__lastname,self.__lastname)
待續...
```
### Example 9
```python=
class character:
def __del__(self):
print(1)
a=stuff()
print(0)
output-----------------
0
1
```
### Example 10
#### case 1
```python=
class myset:
def __init__(self):self__set=set()#O(1)
def len(self):return len(self.__set)#O(1)
def max(self):return max(self.__set)#O(n)
def membership(self,x):return x in self.__set#O(n)
def insert(self,x):return self.__set.add(x)#O(n)
def delete(self,x):return self.__set.discard(x)#O(n)
```
#### case 2
```python=
class myset:
def __init__(self):
def len(self):
return
def max(self):
return
def membership(self,x):#O(logl)
排序後binary search
def insert(self,x):
return
def delete(self,x):
return
```
### Example 11
如果是被import則不執行
```python=
if __name__=__main__:
...
```