标签:多继承 -- col sim 实例变量 集合 sel 括号 就是
class Student(object): pass
class 后面紧接着是类名,即 Student,类名通常是大写开头的单词,紧接着是(object),表示该类是从哪个类继承下来的,继承的概念我们后面再讲,通常,如果没有合适的继承类,就使用 object类,这是所有类最终都会继承的类。
2.类名称定义规范:
定义好了 Student 类,就可以根据 Student 类创建出 Student 的实例,创建实例是通过类名+()实现的:
bart = Student()
可以看到,变量 bart 指向的就是一个 Student 的实例,而 Student 本身则是一个类。
class Student(object): def __init__(self, name, score): self.name = name self.score = score
注意:特殊方法“__init__”前后分别有两个下划线!!!
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)
标签:多继承 -- col sim 实例变量 集合 sel 括号 就是
原文地址:https://www.cnblogs.com/Mr-ZY/p/11777272.html