python开发基础(五)面向对象--类属性和实例属性

# 类属性的增删改查
import time


class Chinesepeople:

    country = 'China'
    person_type = '人'

    def __init__(self, name):
        self.name = name

    def play_ball(self, ball):
        print('%s正在玩%s' % (self.name, ball))

    def eat_foot(self, foot):
        print('%s正在吃%s' % (self.name, foot))


# 查看类属性
print(Chinesepeople.country)
# 修改类属性
Chinesepeople.country = 'CHINA'
print(Chinesepeople.country)
# 增加类属性
Chinesepeople.time_now = time.asctime()
# 删除类属性
print(Chinesepeople.__dict__)
del Chinesepeople.person_type
print(Chinesepeople.__dict__)

person1 = Chinesepeople('jack')
person1.play_ball(ball='ball')
print(person1.time_now)
# 实例属性的增删改查
class Chinese:

    def __init__(self, name):
        self.name = name

    def play_ball(self, ball):
        print('%s正在完%s' % (self.name, ball))


person1 = Chinese('alex')

# 查看
print(person1.name)
# 增加
person1.age = 18
print(person1.age)
# 修改
person1.age = 20
print(person1.age)
print(person1.__dict__)
# 删除
del person1.age
print(person1.__dict__)

 

上一篇:XStream工具的使用,XML转实体(XML、POJO、JSON都可以互转,我没试但是我觉得可以),xstream标签样式设置(最后)


下一篇:JavaseLearn19-JVM内存图