您的位置:首页 > 编程语言 > Python开发

python面向对象例子

2016-07-27 15:47 375 查看
#!/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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: