python面向对象例子

来源:互联网 发布:java 读取表单文件 编辑:程序博客网 时间:2024/06/04 18:02
#!/usr/bin/python# -*- coding: UTF-8 -*-__author__ = 'Administrator'#面向对象class Person:    count=0;    def __init__(self,name,age):        self.name=name;        self.age=age;        Student.count+=1;    def __del__(self):        print("调用析构函数");    def displayCount(self):        print("总人数:%d " %Student.count);    def __repr__(self):        print("调用__repr__");    def __str__(self):        print("调用__str__");        return "name="+self.name+",age="+str(self.age);    def __cmp__(self, other):        print("调用__cmp__");        return cmp(self.age,other.age);    def displayStudent(self):        print("name="+self.name+",age="+str(self.age));    def test(self):        print("test");        passclass Student(Person):    money=100;    __city="beijing"# 私有变量    def displayStudent(self):        print("name="+self.name+",age="+str(self.age)+",money="+str(self.money)+",__city="+self.__city);if __name__=="__main__":    stu1=Student("xiaoming",22);    stu1.displayCount();    stu1.displayStudent();    stu2=Student("xiaohua","11");    stu2.displayCount();    stu2.displayStudent();    person1 =Person("xiaohong","11");    person1.displayCount();    person1.displayStudent();    print(stu1);    print(stu2);    print(person1);    print(person1==stu2)    person1.test();    #print(stu2.__city);

0 0
原创粉丝点击