Advantages of using self in python class attributes and object attributes and ordinary methods

The following shows the advantages of class and object class attributes The second half of the object attributes introduces the advantages of the common method self of the class

//#定义类
class Student:
    name='xiaoming'  #这是类属性
    age=18
print("------------------------对对象属性进行操作----------------")
#使用类,创建对象
print(Student())  #打印出Student类的地址
f1=Student()

print(f1)     #打印Student类的地址
f3=Student()
print(f3)     #
print(f1.name)  #这是对象属性
f1.name='hong'  #改变对象属性 但不会改变类属性
print(f1.name)

f2=Student()
print(f2)    #打印Student类的地址
print(f2.name)  #继承类属性的对象属性
#f2.name='hai'  #改变对象属性
print('---------------对类属性进下操作----------------------------')
print(Student.name)
Student.name='dema'  #改变类属性
print(Student.name)
print(f2.name)  #类属性被修改后,f2继承现在新的类属性

print('------------分割线-------------------------')
print('--------------以下为类self详解----------------')
#类方法 :
# 种类:普通方法 类方法 静态方法 魔术方法
# def 方法名(self[参数,参数]):
class Phone:
    brand='xiaomi'
    price=4999
    type='mate 90'

    # def普通方法
    def call(self):    #
        print('self:----',self)
        print("正在打电话.......")
        print('留言',self.note)      #我并没有在def中定义note  却可以通过外部对象定义note来给def传值
        print("正在访问通讯录 涉及到self的优点")
        for key,values in self.address.items():  #我在def中没有定义address 却可以通过对象定义adress来给def传值
            print(key,values)
#print(Phone())
phone1=Phone()   #创建对象引用类
#print(Phone())
print(phone1,'------------phone1-----------')  #地址还是类原来的地址
print(phone1.brand)
phone1.note='我是phone1的专有对象属性'
phone1.address={
    
    'name':'小明','age':20}
phone1.call()   #发现phone1调用类中的普通方法 普通方法中的self地址为类地址

print("*"*30)
phone2=Phone()
print(phone2,'-------phone2')  #self就是phone2  ,因此创建对象时 类中的def中必须有self
phone2.note='我是phone2的专有'  #注意要给每个调用call()的对象都设置上note address 否者会报错
phone2.address={
    
    'name':'小红','age':15}
phone2.call()

print('------------------self的优点-------------------')
#总结1:        self的优点 可以在外边设置专有对象属性   如下所示
phone1.note='我是phone1的专有对象属性'
phone1.call()





> 以下为程序运行结果 注意比对各个输出的值

//------------------------对对象属性进行操作----------------
<__main__.Student object at 0x0000016D63882828>
<__main__.Student object at 0x0000016D63882828>
<__main__.Student object at 0x0000016D63960F60>
xiaoming
hong
<__main__.Student object at 0x0000016D63960FD0>
xiaoming
---------------对类属性进下操作----------------------------
xiaoming
dema
dema
------------分割线-------------------------
--------------以下为类self详解----------------
<__main__.Phone object at 0x0000016D63960F98> ------------phone1-----------
xiaomi
self:---- <__main__.Phone object at 0x0000016D63960F98>
正在打电话.......
留言 我是phone1的专有对象属性
正在访问通讯录 涉及到self的优点
name 小明
age 20
******************************
<__main__.Phone object at 0x0000016D63969048> -------phone2
self:---- <__main__.Phone object at 0x0000016D63969048>
正在打电话.......
留言 我是phone2的专有
正在访问通讯录 涉及到self的优点
name 小红
age 15
------------------self的优点-------------------
self:---- <__main__.Phone object at 0x0000016D63960F98>
正在打电话.......
留言 我是phone1的专有对象属性
正在访问通讯录 涉及到self的优点
name 小明
age 20

Guess you like

Origin blog.csdn.net/weixin_43516990/article/details/108674106