【1.99】类和实例的属性扩展

1、注意点访问,以及 实例化 修改 对 类没有影响     

country = "中国"     #这个不会再下面的代码中使用  因为都是点 在访问
class Chinese:
    country = "China"
    def __init__(self,name):
        self.name = name

    def play_ball(self,ball):
        print("%s 正在打%s"%(self.name,ball))

p1=Chinese("yaoming")
print(p1.country)   #China
print(Chinese.country)  #China


# 使用实例化后的对象 来修改其中的数据属性,这里改的是p1 的数据属性 和类的无关 (增删改查都讲了)
# 类的没有改  ,只不过你做的就是给 p1 添加了一个 和类相同名字  country 的属性
p1.country = "新中国"
print(p1.country)       #新中国
print(Chinese.country)  #China
例如

2、但是类改变就会对 实例产生影响

country = "中国"     #这个不会再下面的代码中使用  因为都是点 在访问
class Chinese:
    country = "China"
    def __init__(self,name):
        self.name = name

    def play_ball(self,ball):
        print("%s 正在打%s"%(self.name,ball))

p1=Chinese("yaoming")
print(p1.country)   #China
print(Chinese.country)  #China


# 类改变 就会体现在 实例化的对象上 
Chinese.country = "中华人民共和国"
print(p1.country)       #中华人民共和国
print(Chinese.country)  #中华人民共和国
例如

 3、有点就是属性字典作用域 ,没有点就是 全局作用域

country = "中国"     #这个不会再下面的代码中使用  因为都是点 在访问
class Chinese:
    country = "China"
    def __init__(self,name):
        self.name = name
        self.country = country    #但是这里没有点 就是在访问全局变量 中国 就不是China

    def play_ball(self,ball):
        print("%s 正在打%s"%(self.name,ball))

p1=Chinese("yaoming")
print(p1.country)       # 中国
print(Chinese.country)  #China
例如
country = "中国"     #这个不会再下面的代码中使用  因为都是点 在访问
class Chinese:
    country = "China"
    def __init__(self,name):

        self.name = name
        print(country)            #但是这里没有点 就是在访问全局变量 中国 就不是China
        self.country = country    #但是这里没有点 就是在访问全局变量 中国 就不是China


    def play_ball(self,ball):
        print("%s 正在打%s"%(self.name,ball))



#实例化就是自动触发 init 函数  运行它 这里就有打印信息 中国
p1=Chinese("姚明")   # 中国,这里是实例化的字典 作用域  也就是 但是这里的访问没有点 就访问到全局
print(p1.name)       # 姚明
print(p1.country)    # 中国       #这里是实例化的字典 作用域  也就是 但是这里的访问没有点 就访问到全局
print(Chinese.country)    # China   这里是类的字典 访问的就是类的数据属性
注意访问范围,点很重要
country = "中国"     #这个不会再下面的代码中使用  因为都是点 在访问
language = "汉语"
class Chinese:
    global language     #没有global 就是访问 外面的变量  language  汉语 有就访问类的变量 han yu
    language = "han yu"
    country = "China"
    def __init__(self,name):
        print (language)
        self.name = name
        print(country)            #但是这里没有点 就是在访问全局变量 中国 就不是China
        self.country = country    #但是这里没有点 就是在访问全局变量 中国 就不是China

    def play_ball(self,ball):
        print("%s 正在打%s"%(self.name,ball))



#实例化就是自动触发 init 函数  运行它 这里就有打印信息 中国
p1=Chinese("姚明")   # hanyu  中国,这里是实例化的字典 作用域  也就是 但是这里的访问没有点 就访问到全局



-------------------分割线----------------------------------------------------------------------------------



country = "中国"     #这个不会再下面的代码中使用  因为都是点 在访问
language = "汉语"
class Chinese:
    
    #global language    #没有global 就是访问 外面的变量  language  汉语 有就访问类的变量 han yu
    language = "han yu"
    country = "China"
    def __init__(self,name):
        print (language)
        self.name = name
        print(country)            #但是这里没有点 就是在访问全局变量 中国 就不是China
        self.country = country    #但是这里没有点 就是在访问全局变量 中国 就不是China

    def play_ball(self,ball):
        print("%s 正在打%s"%(self.name,ball))



#实例化就是自动触发 init 函数  运行它 这里就有打印信息 中国
p1=Chinese("姚明")   # 汉语  中国,这里是实例化的字典 作用域  也就是 但是这里的访问没有点 就访问到全局
注意类里用 global 定义变量 那么变量就真的全局了,没有点访问就要先找类里有没全局的变量,没有就去外面访问

 

4、_init_ 中不能return 值 可以return None  但是没有意义 

另外这个内置函数中还可以很多逻辑、数据,但是最好不要定义逻辑、 输入、输出,这样就将逻辑 输入输出耦合在一块儿 了

最好你可以将输入输出 定义到另外的函数  ,不然你在init 定义了这些逻辑 输入输出 显得很不易于读   也显得很low

特定函数 完成 特定功能,init 就是完成实例的结构字典,但是又在里面完成了输入输出的方法 这是不好的

所以 要注意  规范   函数里面就只是写 逻辑  不要写 输入输出 

可以在实例前 来定义输入输出 

也可以定义专门的函数来 输入输出  然后调用  再然后调用 实例化 

country = "中国"     #这个不会再下面的代码中使用  因为都是点 在访问
class Chinese:
    country = "China"
    def __init__(self):
        name = input("请输入名字:")   #一般不这样写,一看就是low  就是小学生干活,特定函数写特定功能
        self.name = name
        self.country = country    #但是这里没有点 就是在访问全局变量 中国 就不是China

    def play_ball(self,ball):
        print("%s 正在打%s"%(self.name,ball))


p1=Chinese()    #请输入名字:姚明
print(p1.name)       # 姚明
一般不这样写输入输入
country = "中国"     #这个不会再下面的代码中使用  因为都是点 在访问
class Chinese:
    country = "China"
    def __init__(self,name):

        self.name = name
        self.country = country    #但是这里没有点 就是在访问全局变量 中国 就不是China

    def play_ball(self,ball):
        print("%s 正在打%s"%(self.name,ball))


#可以这样写:

name = input("请输入名字:")
p1=Chinese(name)    
print(p1.name)       # 姚明
可以在实例化前写输入
country = "中国"     #这个不会再下面的代码中使用  因为都是点 在访问
class Chinese:
    country = "China"
    def __init__(self,name):

        self.name = name
        self.country = country    #但是这里没有点 就是在访问全局变量 中国 就不是China

    def play_ball(self,ball):
        print("%s 正在打%s"%(self.name,ball))


#也可以这样写:
def shilihua_shuru():
    name = input("请输入名字:")
    return name

p1=Chinese(shilihua_shuru())   #请输入名字:姚明
print(p1.name)       # 姚明 
也可以定义一个函数来写输入,然后用实例化调用这个函数

 5、实例调用类中字典属性,然后对其修改,这会修改其类的字典属性

class Chinese:
    country = "China"
    li = [1,2,3]
    def __init__(self,name):
        self.name = name

    def play_ball(self,ball):
        print("%s 正在打%s"%(self.name,ball))

p1=Chinese("yaoming")
print(p1.li)   #[1, 2, 3]
print(Chinese.li)  #[1, 2, 3]

#修改前  类 和 实例化的字典属性
print(p1.__dict__)    #{'name': 'yaoming'}
print(Chinese.__dict__)
#{'__module__': '__main__', 'country': 'China', 'li': [1, 2, 3], '__init__': <function Chinese.__init__ at 0x0094EF60>, 'play_ball': <function Chinese.play_ball at 0x0094EFA8>, '__dict__': <attribute '__dict__' of 'Chinese' objects>, '__weakref__': <attribute '__weakref__' of 'Chinese' objects>, '__doc__': None}




#我们修改p1的数据属性  也就是修改p1 的字典属性   也就可以说是修改 p1 的作用域(专业上没有作用域一说)
#但是这里用实例来修改 只是增加了p1的字典属性 增加了数据属性
#不会修改类的数据属性
p1.li = [4,5,6]
print(p1.__dict__)    #{'name': 'yaoming', 'li': [4, 5, 6]}
print(Chinese.__dict__)
#{'__module__': '__main__', 'country': 'China', 'li': [1, 2, 3], '__init__': <function Chinese.__init__ at 0x01ECEF60>, 'play_ball': <function Chinese.play_ball at 0x01ECEFA8>, '__dict__': <attribute '__dict__' of 'Chinese' objects>, '__weakref__': <attribute '__weakref__' of 'Chinese' objects>, '__doc__': None}
实例属性修改,不会影响类的字典属性
class Chinese:
    country = "China"
    li = [1,2,3]
    def __init__(self,name):
        self.name = name

    def play_ball(self,ball):
        print("%s 正在打%s"%(self.name,ball))

p1=Chinese("yaoming")
print(p1.li)   #[1, 2, 3]
print(Chinese.li)  #[1, 2, 3]

#修改前  类 和 实例化的字典属性
print(p1.__dict__)    #{'name': 'yaoming'}
print(Chinese.__dict__)
#{'__module__': '__main__', 'country': 'China', 'li': [1, 2, 3], '__init__': <function Chinese.__init__ at 0x0094EF60>, 'play_ball': <function Chinese.play_ball at 0x0094EFA8>, '__dict__': <attribute '__dict__' of 'Chinese' objects>, '__weakref__': <attribute '__weakref__' of 'Chinese' objects>, '__doc__': None}




# #我们修改p1的数据属性  也就是修改p1 的字典属性   也就可以说是修改 p1 的作用域(专业上没有作用域一说)
# #但是这里用实例来修改 只是增加了p1的字典属性 增加了数据属性
# #不会修改类的数据属性
# p1.li = [4,5,6]
# print(p1.__dict__)    #{'name': 'yaoming', 'li': [4, 5, 6]}
# print(Chinese.__dict__)
# #{'__module__': '__main__', 'country': 'China', 'li': [1, 2, 3], '__init__': <function Chinese.__init__ at 0x01ECEF60>, 'play_ball': <function Chinese.play_ball at 0x01ECEFA8>, '__dict__': <attribute '__dict__' of 'Chinese' objects>, '__weakref__': <attribute '__weakref__' of 'Chinese' objects>, '__doc__': None}


#如果我们队实例调用类的字典属性中的数据属性,然后对数据属性修改 会修改类的属性
p1.li.append([4,5,6])
print(p1.__dict__)    #{'name': 'yaoming'}
print(Chinese.__dict__)
#{'__module__': '__main__', 'country': 'China', 'li': [1, 2, 3,[4,5,6]], '__init__': <function Chinese.__init__ at 0x01ECEF60>, 'play_ball': <function Chinese.play_ball at 0x01ECEFA8>, '__dict__': <attribute '__dict__' of 'Chinese' objects>, '__weakref__': <attribute '__weakref__' of 'Chinese' objects>, '__doc__': None}

#现在实例再调用Chinese的数据属性 li 就会是修改后的字典属性  值 了   [1, 2, 3,[4,5,6]]
print(p1.li)   #[1, 2, 3, [4, 5, 6]]
实例调用类属性,然后修改对应属性值,会修改类属性的值,实例再次调用,其值也会是修改后的值

 

posted @ 2016-05-09 13:51  科学小怪癖  阅读(133)  评论(0)    收藏  举报