Python class and inheritance

Source: Internet
Author: User
Compared with C ++, Python is simpler and more efficient. Below is a simple example of Python inheritance. Compared with C ++, Python is simpler and more efficient. Below is a simple example of Python inheritance.

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,500 00)

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

# Include

# Include

Using namespace std;

Class Member

{

Public:

Member (char * n, int );

Void tell ();

Private:

Char name [10];

Int age;

};

Member: Member (char * n, int)

{

Memcpy (name, n, sizeof (name ));

Age =;

Cout <"Member 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,)

{

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,)

{

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,500 00 );

S. tell_s ();

T. tell_t ();

Return 0;

}

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

The two have the same running effect, but python is more concise...

The above is the explanation of Python class and inheritance. For more information, see PHP Chinese network (www.php1.cn )!

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.