python入门经典(二)

在student.py中

class student(object):
    """description of class"""
    __privateParm = 0       #私有变量
    publicParm = "ha"       #公开变量
    def __init__(self, number=0,grade = 'k', sex = 'male',description=""):
        self.number = number
        self.grade = grade
        self.sex = sex
        self.description = description

    def changeDescription(self):
        if not self.description:
            description = raw_input("please input a description:")
        self.description = description
    ##覆盖内置函数,重载运算符
    def __lt__(self,other): 
        if self.number < other.number:
            return True
        else:
            return False


    ##定义类自身输入函数
    def __str__(self):
        return "myNum:{number}, myGrade:{grade}, mySex:{sex},myDescprition:{description}".format(number = self.number,\
            grade = self.grade, sex = self.sex, description = self.description)

#-----------------------------------------------------------------------------------------
#类的继承
# ----------------------------------------------------------------------------------------
class RStudent(student):
    def __init__(self, number, grade, sex, description, height):
        #用C++的方法理解是:先用super调用父类的构造函数
        super(RStudent,self).__init__(number = number, grade = grade, sex = sex, description = description)
        self.height = height

在pythonLearning1.py中

from student import student  ##调用类名
from student import RStudent
stu1 = student(2,'A','female')
stu1.changeDescription()
stu2 = student()
c = stu1print c
print stu1.publicParm            #可以访问公开变量
print stu1 
stu3 = RStudent(2,'A','female',stu1.description,7)
stu3.height = 170
print stu3.height

结果:
python入门经典(二)_第1张图片

你可能感兴趣的:(Python)