python (encapsulation call / Inheritance / polymorphism of class)

Keywords: Python

I. definition of class
Class: a collection used to describe objects with the same properties and methods. It defines the properties and methods that are common to each object in the collection. Object is an instance of a class
 
Object: the object instantiated by a class is called an object
 
Relationship between object and class: the relationship between class and object is just like the relationship between mold and casting. The result of class instantiation is object, and the abstraction of object is class.
 
Class and instance
The most important concept of object-oriented is Class and Instance. It must be kept in mind that classes are abstract templates, such as Student classes. Instances are concrete "objects" created according to classes. Each object has the same method, but its data may be different.
 
Take Student class as an example. In Python, class is defined by class keyword:
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.

1. Class definition:
  • 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.

Because classes can play the role of templates, we can force some properties that we think must be bound to be filled in when creating instances. By defining a special init method, when creating an instance, bind the attributes such as name, score, etc
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!!!

Note that the first parameter of the init method is always self, which means the created instance itself. Therefore, within the init method, you can bind various attributes to self, because self points to the created instance itself.
 
With the init method, when you create an instance, you cannot pass in empty parameters. You must pass in parameters that match the init method, but self does not need to pass in. The Python interpreter will pass in the instance variables by itself:
 
 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

Parameters, variable parameters, key parameters, and named key parameters.
 
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()
"""

 

Class call
Call the method under the class, which must be called through the instance / class name () of the class
  • 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()

 

III. inheritance
1. Single inheritance and multiple inheritance
  • 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()

 

Polymorphism (overriding parent method)
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)

 

Posted by jburbage on Sat, 02 Nov 2019 04:22:58 -0700