欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python中类的继承代码实例

程序员文章站 2023-12-14 09:48:16
相对于c++的继承编写,python更简洁,而且效率也是很高的,下面编写一个简单python的继承例子。 复制代码 代码如下: #!/usr/bin/python&nb...

相对于c++的继承编写,python更简洁,而且效率也是很高的,下面编写一个简单python的继承例子。

复制代码 代码如下:

#!/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, 50000)  
 
members = [s, t]  
for mem in members:  
        mem.tell() 

运行效果:

复制代码 代码如下:

[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 

我们同样编写同样效果的c++例子:

复制代码 代码如下:

//filename: class.cpp  
#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;  

运行效果:

复制代码 代码如下:

[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 

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

上一篇:

下一篇: