python面向对象例子

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/earbao/article/details/52046187
#!/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");
        pass

class 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);

展开阅读全文

没有更多推荐了,返回首页