Discuss / Python / day14-6月14日

day14-6月14日

Topic source

Promethues

#1 Created at ... [Delete] [Delete and Lock User]

###类属性和实例属性

class Student2(object):

    name = 'angel'   #类属性

s = Student2()

print(s.name)           #angel

print(Student2.name)    #angel

s.name = 'violet'     #实例属性

print(s.name)           #violet  先查实例属性,无则再查类属性

print(Student2.name)    #angel

del s.name  #删除实例属性

#practice  给Student2增加一个类属性,记录递增的学号

class Student2(object):

    count = 0

    def __init__(self,name):

        self.name = name

        Student2.count += 1

        self.count = Student2.count

print(Student2.count)

s = Student2('angel')

print(s.count)

a =  Student2('violet')

print(s.count)

print(a.count)

c = Student2('aaa')

print(s.count)

print(a.count)

print(c.count)


  • 1

Reply