The Combination of python Object-Oriented Programming

Posted by MrSarun on Sun, 16 Jun 2019 02:05:35 +0200

We talked about class-oriented and object-oriented inheritance, and we know what is the relationship between inheritance and what.

However, there is another relationship between classes, which is composition.

Let's start with two examples: Firstly, two classes are defined, one is teacher class. Teacher class has the characteristics of name, age, year of birth, month and day, courses taught, walking and teaching skills.

class Teacher:
    def __init__(self,name,age,year,mon,day):
        self.name=name
        self.age=age
        self.year=year
        self.mon=mon
        self.day=day

    def walk(self):
        print("%s is walking slowly"%self.name)

    def teach(self):
        print("%s is teaching"%self.name)

Define a student class, which has the characteristics of name, age, year of birth, month and day of birth, group name of study, walking and learning skills.

class Student:
    def __init__(self,name,age,year,mon,day):
        self.name=name
        self.age=age
        self.year=year
        self.mon=mon
        self.day=day

    def walk(self):
        print("%s is walking slowly"%self.name)

    def study(self):
        print("%s is studying"%self.name)

Based on the inheritance of classes, you can reduce the code a little.

Define a human being, and then let teachers and students inherit human characteristics and skills:

class People:
    def __init__(self,name,age,year,mon,day):
        self.name=name
        self.age=age
        self.year=year
        self.mon=mon
        self.day=day

    def walk(self):
        print("%s is walking"%self.name)

class Teacher(People):
    def __init__(self,name,age,year,mon,day,course):
        People.__init__(self,name,age,year,mon,day)
        self.course=course

    def teach(self):
        print("%s is teaching"%self.name)

class Student(People):
    def __init__(self,name,age,year,mon,day,group):
        People.__init__(self,name,age,year,mon,day)
        self.group=group

    def study(self):
        print("%s is studying"%self.name)

Then the teachers and students are instantiated to get a teacher and a student.

t1=Teacher("alex",28,1989,9,2,"python")
s1=Student("jack",22,1995,2,8,"group2")

Now it's easy to know the names of t1 and s1, age, year of birth, month and day, but you want to print them out once. The birthday of t1 or s1 is not so easy. At this time, string splicing is needed. Is there any better way?

That's combination.

Inheritance is a relationship between a subclass and a parent class, while composition is a relationship between one class and another.

It can be said that everyone has a birthday, but can not say that people are birthdays, so we need to use the function of combination. You can define the date of birth and the date of birth in another class, and then use the class of teacher or student with that date. Combined, it's easy to get the birthday of teacher t1 or student s1, and it's no longer so troublesome as string splicing. Let's look at the following code:

class Date:
    def __init__(self,year,mon,day):
        self.year=year
        self.mon=mon
        self.day=day

    def birth_info(self):
        print("The birth is %s-%s-%s"%(self.year,self.mon,self.day))

class People:
    def __init__(self,name,age,year,mon,day):
        self.name=name
        self.age=age
        self.birth=Date(year,mon,day)

    def walk(self):
        print("%s is walking"%self.name)

class Teacher(People):
    def __init__(self,name,age,year,mon,day,course):
        People.__init__(self,name,age,year,mon,day)
        self.course=course

    def teach(self):
        print("%s is teaching"%self.name)

class Student(People):
    def __init__(self,name,age,year,mon,day,group):
        People.__init__(self,name,age,year,mon,day)
        self.group=group

    def study(self):
        print("%s is studying"%self.name)
t1=Teacher("alex",28,1989,9,2,"python")
s1=Student("jack",22,1995,2,8,"group2")

In this way, you can use the same method as before to call the name, age and other characteristics of teacher t1 or student s1. And walking, teaching or learning skills.

print(t1.name)
t1.walk()
t1.teach()

The output is:

alex    
alex is walking
alex is teaching

How can we know their birthdays?

print(t1.birth)

The output is:

<__main__.Date object at 0x0000000002969550>

This birth is inherited from the parent People by the subclass Teacher, and the parent People's birth is combined with the Date class. Yes, so this birth is an object. Under the Date class, there is a birth_info skill, which allows you to pass Call the birth_info function property under Date to know the birthday of Teacher t1.

t1.birth.birth_info()

The results are as follows:

The birth is 1989-9-2

Similarly, the same method is used for the birthday of the student who wants to know the example s1:

s1.birth.birth_info()

The results are as follows:

The birth is 1995-2-8

Composition is the use of one class to another class, thus putting several classes together. Composite functionality is also designed to reduce duplication of code.

Topics: Python