class Student(object): pass
Class is followed by class name, i.e. Student. The class name usually starts with uppercase words, followed by (object), indicating which class the class inherits from. We will talk about the concept of inheritance later. Generally, if there is no suitable inheritance class, use the object class, which is the class that all classes will eventually inherit.
- Class definition class
- Class followed by class name plus () plus:
2. Class name definition specification:
- Don't name it after a pure number
- Do not name with reserved characters (keywords) in python
- Do not name the file
- Special characters are not allowed
- Be brief and understand the meaning of the name
- When there are more than one word in the class name, hump (capitalize the first letter of each word) -- > xinfangshuo ()
After defining the Student class, you can create an instance of the Student according to the Student class. The instance is created by the class name + ():
bart = Student()
As you can see, the variable bart points to an instance of Student, while Student itself is a class.
class Student(object): def __init__(self, name, score): self.name = name self.score = score
Note: the special method "init" has two underscores before and after!!!
bart = Student('Bart Simpson', 59)
Compared with ordinary functions, the functions defined in the class are only different, that is, the first parameter is always the instance variable self, and the parameter is not passed when calling. In addition, there is no difference between the methods of a class and ordinary functions, so you can still use the default
class Four(): #Class definition 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) #Parameter instantiation # 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() """
- When there is a parameter in the init ialization method in the class, the parameter needs to be passed into the instance bracket during instantiation
- When there is no initialization method in the class or there is no parameter in init, you do not need to pass the parameter into the instance bracket during instantiation, but when calling the method
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') #instantiation print operation.add() print operation.reduce() print operation.ride() print operation.Except()
- An object uses the properties and methods of another object. The inherited class is also called the parent class
- Multiple inheritance is a subclass inheriting multiple parents
class Four(): def sub(self,x,y): return x + y class Five(Four): #Five Class inherited Four class --> Five Class owned Four All function methods under class 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 + "Surname Zhang" 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('Zhang Si',"play basketball","boy") so.Surname() so.hobby() so.study() so.Sex()
class car(): "Multiple inheritance" 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 + "yes" + str(self.year) + "Produced!" def update_mileage(self,mile): if mile < self.mileage: print "Do not modify mileage!" class aircraft(): def __init__(self,name,destination): self.name = name self.destination = destination def bound(self): print self.name + "Bound for" + 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("Titan","Nick",2010,500,"Titanic","Ningbo") my_boat.make() my_boat.bound()
class Four(): def sub(self,x,y): return x + y class Five(Four): #Five Class inherited Four class --> Five Class owned Four All function methods under class def jian(self,a,b): return a - b def sub(self,x,y): return x * y print Five().sub(3,6)