1. 程式人生 > >python基礎入門之python的類

python基礎入門之python的類

類的例項方法

例項方法的第一個引數必須是”self”。
例項方法只能通過類例項進行呼叫,這時候”self”就代表這個類例項本身。通過”self”可以直接訪問例項的屬性,有了init方法,在建立例項的時候,就不能傳入空的引數了,必須傳入與init方法匹配的引數,但self不需要傳。 ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 class A(object): # 類的初始化 def __init__(self, name): self.name = name print("init class A") def hello(self): print("hello {0}"
.format(self.name)) a = A("shan") a.hello() # 輸出 init class A hello shan

繼承

繼承
Python中,同時支援單繼承與多繼承,實現繼承之後,子類將繼承父類的屬性。

一般語法如下:

?
1 2 class SubClassName(ParentClass1 [, ParentClass2, ...]): class_suite
繼承的例子 ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 class Animal(object): def __init__(self, name): print("你現在正在初始化一個Animal") def run(self): print("Animal can run.") class Bird(Animal):   #繼承Animal def __init__(self): print("bird") def fly(self): print("Bird can fly.") class Cat(Animal):   #繼承Animal def __init__(self, name, sex): self.name = name
self.sex = sex super(Cat, self).__init__(self.name) print("我是一隻貓,啦啦啦啦") def jiao(self): print("miao miao miao miao") def run(self): print("我是一隻貓,會上樹來會跑路.") class BianYi(Cat, Bird):   #繼承Cat,Bird pass cat = Cat("mao", "man") # 輸出 你現在正在初始化一個Animal 我是一隻貓,啦啦啦啦

類中的呼叫方法

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 class DbArgs(object): # 只有類本身才可以呼叫 __host = str("1.1.1.1") __port = str() __username = str() __password = str() __dbname = str() # 任何人可以呼叫 name = "ajing" # 只能例項自己呼叫 _host = "asdlfjasdl" def getHost(self): return self.__host def setHost(self, host): self.__host = host dbArgs = DbArgs() print(dbArgs.getHost()) dbArgs.name = "就是要改你,怎麼的" print(dbArgs.name) print(dbArgs._host) # 輸出 1.1.1.1 就是要改你,怎麼的 asdlfjasdl

類的應用例子

本例項主要是結合類的初始化,例項化,也使用了之前學的開啟檔案和寫檔案。 ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 import codecs class Student(object): def __init__(self, id, name, score):   #初始化 id,name,score self.id = id self.name = name self.score = score class InitStduents():    def __init__(self):   # 初始化中定義一個list,執行init方法,init方法中新增list資料 self.students = list() self.init() def init(self): self.students.append(Student(1001, "aaa", 59)) self.students.append(Student(1002, "bbb", 96)) self.students.append(Student(1003, "ccc", 87)) self.students.append(Student(1004, "ddd", 89)) self.students.append(Student(1005, "eee", 33)) self.students.append(Student(1006, "fff", 85)) self.students.append(Student(1007, "ggg", 78)) self.students.append(Student(1008, "hhh", 97)) self.students.append(Student(1009, "iii", 31)) self.students.append(Student(1010, "jjj", 93)) def sort(self):   # 類中的sort方法(list的排序) return sorted(self.students, key=lambda student: student.score) def writeFile(self, newStudents):   # 類中writeFile方法(把內容寫進檔案) with codecs.open("sortStudent.txt", "w")as f: for i in newStudents: f.write("id = {0}".format(i.id)) f.write("\t") f.write("name = {0}".format(i.name)) f.write("\t") f.write("score = {0}".format(i.score)) f.write("\n") def main(): students = InitStduents() newStudents = students.sort() http://cz.qwangxiao.com/ students.writeFile(newStudents) if __name__ == "__main__": main() # 檢視newStudents id = 1009   name = iii  score = 31 id = 1005   name = eee  score = 33 id = 1001   name = aaa  score = 59 id = 1007   name = ggg  score = 78 id = 1006   name = fff  score = 85 id = 1003   name = ccc  score = 87 id = 1004   name = ddd  score = 89 id = 1010   name = jjj  score = 93 id = 1002   name = bbb  score = 96 id = 1008   name = hhh  score = 97
使用類的方法編寫之前的求階乘和 ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 class JinCinCount(object): def __init__(self, n): self.n = n def jc(self, n): result = 1 if n == 0: return result else: for i in range(1, n+1): result *= i return result def count(self): count = 0 for i in range(0, int(self.n) + 1): count += self.jc(i) print("count = {0}".format(count)) def main(): n = input("Please inpurt a number: ") jinCinCount = JinCinCount(int(n)) jinCinCount.count() if __name__ == "__main__": main() # 輸出 Please inpurt a number: 5 count = 154