python daily learning 2

inherit

Python is also an object-oriented language.

One of the characteristics of object-oriented is the reuse of code , and one of the ways to achieve reuse is through the inheritance mechanism. Similar to the concept in java, there is the concept of supertype and subtype.
Example from the book now:
There are two types of people in schools: teachers, students.

They have some common characteristics: name, age, address .

Teachers also have some unique characteristics: salary, courses, vacations ; students' unique characteristics: grades, tuition fees .
Of course, two classes can be written separately: the teacher's class and the student's class.

But if you want to add another feature, such as gender, you need to add this attribute to the teacher's class, and you also need to add this attribute to the student's class. This is more complicated.
Is there any way to optimize it?
You can extract the common characteristics of teachers and students and create a new class, SchoolMember. In this category are some characteristics common to school members: name, age, address, gender, etc.

Create a new teacher's class: Teacher,

Student's class: Student, which inherits the SchoolMember class respectively, and also adds their own attributes.

The base class code SchoolMember is as follows:

class SchoolMember:
    # Represents members of any school
    def __init__(self, name, age):
        self.name = name
        self.age = age
        print 'Initialized SchoolMember:{}'.format(self.name)

    def tell(self):
        print 'Name:"{}" Age:"{}"'.format(self.name, self.age)

 Teacher class Teacher code is as follows:

class Teacher(SchoolMember):
    # represents a teacher
    def __init__(self, name, age, salary):
        SchoolMember.__init__(self, name, age)
        self.salary = salary
        print 'Initialized Teacher:{}'.format(self.name)

    def tell(self):
        SchoolMember.tell(self)
        print 'Salary: "{:d}"'.format(self.salary)

 The student class Student code is as follows:

class Student(SchoolMember):
    def __init__(self, name, age, marks):
        SchoolMember.__init__(self, name, age)
        self.marks = marks
        print 'Initialized Student: {}'.format(self.name)

    def tell(self):
        SchoolMember.tell(self)
        print 'Marks: "{:d}"'.format(self.marks)

 The calling code is as follows:

t = Teacher('Mrs. Shriviya', 40, 3000)
s = Student ('Swaroop', 25, 75)
print ''

members = [t, s]
for member in members:
    member.tell()

 The result after running is as follows:

---------------------------------------------------------------

Initialized SchoolMember:Mrs. Shriviya
Initialized Teacher:Mrs. Shriviya
Initialized SchoolMember:Swaroop
Initialized Student: Swaroop

Name:"Mrs. Shriviya" Age:"40"
Salary: "3000"
Name:"Swaroop" Age:"25"
Marks: "75"

 

---------------------------------------------------------------

Notice:

The difference between inheritance in java and python :

In java , a.class inherits b.class, then in the constructor of subclass a, no matter what happens, the constructor of parent class b.

For the subclass of python , if the subclass , such as the Teacher class, has written the constructor __init__(), it needs to explicitly call the constructor __init__(self) of the parent class, otherwise it will not be called.

But if the subclass , Teacher class, does not define the constructor __init__() , then the constructor of the parent class will be called by default .

Multiple inheritance: If the class is defined,

class Teacher(class1,class2...), wrote multiple inheritance tuples, this is multiple inheritance.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326174747&siteId=291194637