python类的介绍1

来源:互联网 发布:软件著作权 评职称 编辑:程序博客网 时间:2024/05/02 07:41
#!/usr/bin/python
#-*- coding:utf8 -*-


class Student:
    name = ""
    sex = "F"
    def __init__(self):
        self.name = "Hello"  #默认的是Hello
    def setName(self, name):
        self.name = name
    def getName(self):
        return self.name

    @classmethod  #表示是类方法 1
    def setSex(self,sex):
        self.sex = sex

    def getSex(self):
        return self.sex

    method = classmethod(getSex)  #类方法的第二种声明

class SmallStudent(Student):  #表示集成Student
    """
    This is a small student's class
    """
    age = 0
    def __init__(self):
        self.age = 10;
    
    @staticmethod
    def setAge(age):
        SmallStudent.age = age  #please pay attention

    def getAge():
        return SmallStudent.age #***

    agemethod = staticmethod(getAge)
    



#内置类
class Fruit:
    class Apple:
        def getColor(self):
            return "red"
    class Pear:
        def getColor(self):
            return "yellow"


if __name__ == "__main__":
    stu = Student()
    stu.name = "Jack"
    #stu.setName("Jack")
    print stu.getName()
    
    smallStu = SmallStudent()
    print SmallStudent.__bases__  #输出父类组成的元组
    print smallStu.__dict__   #输出属性组成的字典
    print smallStu.__module__  #输出类的模块名
    print smallStu.__doc__   #输出文档介绍


    #类方法
    Student.setSex("M")
    print Student.method()


    SmallStudent.setAge('12')
    print SmallStudent.agemethod()


    f = Fruit()
    app = f.Apple()
    pea = f.Pear()
    print app.getColor()
    print pea.getColor()
#第二种调用方式
    app1 = Fruit.Apple()

    print app1.getColor()


QQ交流群: 204944806

0 0