Home > Backend Development > Python Tutorial > Python classes and inheritance explained

Python classes and inheritance explained

黄舟
Release: 2016-12-24 17:18:20
Original
1429 people have browsed it

Compared with inheritance writing in C++, Python is more concise and efficient. Here is a simple Python inheritance example. EClass Member:

DEF __init __ (Self, name, Age):

Self.name = name

Self.age = Age

Print 'Member init:%s'%self.name

DEF TELL ( Self): in Print 'Name:%S, Age:%D'%(Self.name, Self.age),

Class Student (Member):

DEF __init __ (Self, Name, Age, Marks):

                                                             ‐            Member.__init__                                                            self.marks                                              

                                                                                                                                                                             def  tell(self):                                   " Member.__init__(self, name, age)

                                                                           salary = salary

Print 'Teacher Init:%s'%seld.name

deftell (self):

member.tell (self)

print' salary:%d'%seld.salarly

s = Student('Tom', 20, 80)

t = Teacher('Mrs.Huang', 30, 50000)

members = [s, t]

for mem in members:

mem.tell()

Running effect:

[root@localhost hhl]

Member init:Tom

Student init:Tom

Member init:Mrs.Huang

Teacher init:Mrs.Huang

Name:Tom,Age: 20 Marks :80

Name:Mrs.Huang,Age:30 Salary:50000

We also write a C++ example with the same effect:

#include

#include

using namespace std ;

class Member

{ public:

Member(char *n, int a);

void tell();

private:

char name[10];

int age;

};

Member::Member(char *n, int a)

{

memcpy(name, n, sizeof(name));

age = a;

cout<< init:" <

}

void Member::tell()

{

    cout<<"Name:"<

}   

class Student:public Member   

{   

public:   

    Student(char *n, int a, int m);   

    void tell_s();   

private:   

    int marks;   

};   

Student::Student(char *n, int a, int m):Member(n, a)   

{   

    marks = m;   

    cout<<"Student init:"<

}   

void Student::tell_s()   

{   

    Member::tell();   

    cout<<"Marks:"<

}   

class Teacher:public Member   

{   

public:   

    Teacher(char *n, int a, int s);   

    void tell_t();   

private:   

    int salary;   

};   

Teacher::Teacher(char *n, int a, int s):Member(n, a)   

{   

    salary = s;   

    cout<<"Teacher init:"<

}   

void Teacher::tell_t()   

{   

    Member::tell();   

    cout<<"Salary:"<

}   

int main(void)   

{   

    Student s("Tom", 20, 80);   

    Teacher t("Mrs.Huang", 30, 50000);   

    s.tell_s();   

    t.tell_t();   

    return 0;   

}  

运行效果:

[root@localhost hhl]  

Member init:Tom   

Student init:Tom   

Member init:Mrs.Huang   

Teacher init:Mrs.Huang   

Name:Tom,Age:20,Marks:80   

Name:Mrs.Huang,Age:30,Salary:50000  

这两者的运行效果是一样的,但是python更简洁些。。。

 以上就是Python类与继承讲解的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template