__init__的用法
#類定義
class people:
#定義基本屬性
name = ''
age = 0
#定義私有屬性,私有屬性在類外部無法直接進行通路
__weight = 0
#定義構造方法
def __init__(self,n,a,w):
# ***self*** in python is similar with ***this*** in c++. It is a pointer of instance of the objectin in class
# the __init__() method is called the constructor and is always called when an object is created.
# Constructors are generally used for instantiating an object.
self.name = n
self.age = a
self.__weight = w # to define a private member, prefix __ to member name
def speak(self):
print("%s 說: 我 %d 歲。" %(self.name,self.age))
# 執行個體化類
p = people('runoob',10,30)
p.speak()