class Student(object): pass
class 後面緊接着是類名,即 Student,類名一般是大寫開頭的單詞,緊接着是(object),表示該類是從哪一個類繼承下來的,繼承的概念咱們後面再講,一般,若是沒有合適的繼承類,就使用 object類,這是全部類最終都會繼承的類。python
2.類名稱定義規範:ide
定義好了 Student 類,就能夠根據 Student 類建立出 Student 的實例,建立實例是經過類名+()實現的:函數
bart = Student()
能夠看到,變量 bart 指向的就是一個 Student 的實例,而 Student 自己則是一個類。spa
class Student(object): def __init__(self, name, score): self.name = name self.score = score
注意:特殊方法「__init__」先後分別有兩個下劃線!!!code
bart = Student('Bart Simpson', 59)
和普通的函數相比,在類中定義的函數只有一點不一樣,就是第一個參數永遠是實例變量 self,而且調用時,不用傳遞該參數。除此以外,類的方法和普通函數沒有什麼區別,因此,你仍然能夠用默認對象
class Four(): #類的定義 def sub(self,x,y): return x + y """ class Dog(): def __init__(self,name,age): self.name = name self.age = age def sit(self): print (self.name.title() + ' ' + "is now sitting") def roll_over(self): print (self.name.title() + ' ' + "is now roll over") my_dog = Dog('willie',6) #參數實例化 # your_dog = Dog('lucy',3) my_dog.sit() my_dog.roll_over() """ """ class Four_operations(): def __init__(self,a,b): self.a = int(a) self.b = int(b) def add(self): return self.a + self.b def reduce(self): return self.a - self.b def ride(self): return self.a * self.b def Except(self): return self.a / self.b operation = Four_operations('12','4') print operation.add() print operation.reduce() print operation.ride() print operation.Except() """
class Four(): def sub(self,x,y): return x + y print Four().sub(2,3) class Four_operations(): def __init__(self,a,b): self.a = int(a) self.b = int(b) def add(self): return self.a + self.b def reduce(self): return self.a - self.b def ride(self): return self.a * self.b def Except(self): return self.a / self.b operation = Four_operations('12','4') #實例化 print operation.add() print operation.reduce() print operation.ride() print operation.Except()
class Four(): def sub(self,x,y): return x + y class Five(Four): #Five類繼承了Four類 --> Five 類擁有了 Four 類下的全部函數方法 def jian(self,a,b): return a - b print Five().sub(2,5)
class Father(): def __init__(self,name,sport,sex): self.name = name self.sport = sport self.sex = sex def Surname(self): print self.name + "姓張" def hobby(self): print self.name + "like" + " " + self.sport class Son(Father): def study(self): print self.name + " " + "study very good" def Sex(self): print self.name + " " + "is" + " " + self.sex so = Son('張四',"play basketball","boy") so.Surname() so.hobby() so.study() so.Sex()
class car(): "多繼承" def __init__(self,brand,type,year,mileage): self.brand = brand self.type = type self.year = year self.mileage = mileage def make(self): print self.brand + self.type + "是" + str(self.year) + "生產的!" def update_mileage(self,mile): if mile < self.mileage: print "請勿修改里程數!" class aircraft(): def __init__(self,name,destination): self.name = name self.destination = destination def bound(self): print self.name + "開往" + self.destination class boat(car,aircraft): def __init__(self,brand,type,year,mileage,name,destination): self.brand = brand self.type = type self.year = year self.mileage = mileage self.name = name self.destination = destination my_boat = boat("泰坦","尼克號",2010,500,"泰坦尼克號","寧波") my_boat.make() my_boat.bound()
class Four(): def sub(self,x,y): return x + y class Five(Four): #Five類繼承了Four類 --> Five 類擁有了 Four 類下的全部函數方法 def jian(self,a,b): return a - b def sub(self,x,y): return x * y print Five().sub(3,6)