'''
***************
Phone 对象初次实验
***************
'''
class Phone():
Price=4999;
Brand='HuaWei'
def __init__(self):
self.Brand='小米'
self.Price=1000
def Call(self):
print('正在给自己打电话')
yupeng=Phone()
print(yupeng.Price)#调用__init__函数
print(yupeng.Brand)
yupeng.Call()
'''
***************
Person 对象初次实验
了解 __init__方法的作用
了解 对象方法的作用
了解 类方法的写法和作用域
了解静态方法的写法和作用域
***************
'''
class Person():
__JG='jiangxi' #私有属性
Jiguan='beijing' #共有属性
def __init__(self,name,age):
self.name=name
self.age=age
def eat(self):
print('self--------->',self)
#output:<__main__.Person object at 0x01A1E088>
print('{}正在吃红烧肉'.format(self.name))
print('{}岁的少年正在喝酒'.format(self.age))
def run(self):
print('{}正在院子里面跑步'.format(self.name))
print('吃完了继续跑步')
self.eat()
# Person.__JG='Hebei' # 可以访问
# cls.__JG 可以访问
# __JG 不能访问
# self.__JG 可以访问
@classmethod
def test(cls):
print('cls--------->',cls)
#output:<class '__main__.Person'>
@classmethod
def updateJg(cls,jg):
cls.__JG=jg #可以访问
# self.__JG 不能访问
# Person.__JG='Hebei' #可以访问
print('修改后的籍贯为:{}'.format(cls.__JG))
@staticmethod
def getClass():
print('这是静态方法!')
# print(self.name) 无法访问self.name
print(Person.__JG)#可访问私用属性,静态方法不需要传递cls和self参数,可直接访问类的属性
Lj=Person('李杰',18)
# Lj.eat()
# Lj.test()
# Lj.run()
# Person.test()
# print(Person.JG) AttributeError: type object 'Person' has no attribute 'JG'
Lj.updateJg('Hunan')
Lj.getClass()