Class inheritance code instances in Python, and python inheritance code instances

Source: Internet
Author: User

Class inheritance code instances in Python, and python inheritance code instances

Compared with C ++, Python is simpler and more efficient. Below is a simple example of Python inheritance.

Copy codeThe Code is as follows:
#! /Usr/bin/python
# Filename: pyclass. py

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:

Copy codeThe Code is as follows:
[Root @ localhost hhl] # python pyclass. py
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:

Copy codeThe Code is as follows:
// Filename: class. cpp
# Include <string. h>
# Include <iostream>
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:" <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,)
{
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,)
{
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,500 00 );

S. tell_s ();
T. tell_t ();

Return 0;
}

Running effect:

Copy codeThe Code is as follows:
[Root @ localhost hhl] #./class
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...


Who can use the popular method to inherit the python class? Tell the younger brother about the inheritance of the class.

A general example is:
There is already a class called bird
Its flying methods/functions

Then you implement another sparrow class.
If no class is inherited
Then you need:
First implement the Flying Method
Then other methods (and attributes) specific to Sparrow are implemented separately)

This method is similar to the method of flying. It is obvious that it is a common method used by all species of birds.
Therefore, to avoid this, every other bird and every other bird must implement this method separately.
Therefore, we get a base class, that is, the basic class, the main class.
It implements some common things that everyone shares.
Including many methods and many attributes

Then the other child classes
After this base class is integrated
You don't have to repeat and reimplement the basic methods and attributes.
You only need to implement the unique features of your class.

I don't know if you understand it.

For more information, see my summary:
[Arrangement] basic object-oriented knowledge: Class, Object, and Instance)

(No post address is provided here. You can find the post address by searching the post title on google)

How do I know the method to be overwritten after python inherits

In Python, the concept corresponding to a virtual class is an abstract base class (ABC). In general, there is no need to override the class.

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.