Python class and inheritance explained

Source: Internet
Author: User
Python is simpler and more efficient than C + + inheritance, so write a simple Python inheritance example below.

Class Member:

def __init__ (self, Name, age):

Self.name = Name

Self.age = Age

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

def tell (self):

print ' name:%s,age:%d '% (Self.name, self.age),

Class Student (Member):

def __init__ (self, name, age, marks):

Member.__init__ (self, name, age)

Self.marks = Marks

print ' Student init:%s '% self.name

def tell (self):

Member.tell (self)

print ' marks:%d '% self.marks

Class Teacher (Member):

def __init__ (self, name, age, salary):

Member.__init__ (self, name, age)

Self.salary = Salary

print ' Teacher init:%s '% self.name

def tell (self):

Member.tell (self)

print ' salary:%d '% self.salary

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

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

Members = [s, T]

For MEM in Members:

Mem.tell ()

Operating 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 C + + examples of the same effect:

#include <string.h>

#include <iostream>

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<< "Member init:" <<name<<endl;

}

void Member::tell ()

{

cout<< "Name:" <<name<< "," << "Age:" <<age<< ",";

}

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:" <<n<<endl;

}

void student::tell_s ()

{

Member::tell ();

cout<< "Marks:" <<marks<<endl;

}

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:" <<n<<endl;

}

void teacher::tell_t ()

{

Member::tell ();

cout<< "Salary:" <<salary<<endl;

}

int main (void)

{

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

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

S.tell_s ();

t.tell_t ();

return 0;

}

Operating 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

It works the same, but Python is more concise ...

The above is the Python class and inheritance to explain the content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.