1. 程式人生 > >Python基礎-對象篇

Python基礎-對象篇

ada span name abi people pan 父類 spa int

 1 class people():
 2     def __init__(self,name):
 3         self.name = name
 4         self.age = 21
 5 
 6     def sit(self):
 7         print(%s坐下 % self.name)
 8 
 9     def say(self):
10         print(%s說話 % self.name)
11 
12     def edit_age(self,age):
13         self.age = age
14 
15 jack = people(
jack) 16 jack.sit() 17 jack.say() 18 jack.age = 5 19 print(jack.age) 20 jack.edit_age(30) 21 print(jack.age) 22 23 class son(people): 24 def __init__(self,name,sex,*age): 25 super().__init__(name,*age) 26 self.sex = sex 27 28 def eat(self): 29 print(%s在吃 % self.sex)
30 # 重寫父類 31 def say(self): 32 print(%s在說話,是個%s的 % (self.name,self.sex)) 33 34 shabi = son(dada,) 35 shabi.edit_age(100) 36 print(shabi.age) 37 shabi.eat() 38 shabi.say()

Python基礎-對象篇