class phone:
'这个是手机类,专门标记手机的属性和功能'
初始化函数__init__() #这个做可以被对象方法调用
变成对象属性,只能被对象调用,而不能被类调用
def __init__(self,brand_name,color,price,size,os):
self.brand_name=brand_name
self.color =color#对象属性只能被对象来访问
phone.price =price#类属性可以通过类和对象来访问
self.size = size
self.os = os ##赋值给对象属性,方便后期使用
def call(self,name='xiaocc'):##对象方法
print('我可以给{}打电话'.format(name))
return 666
@staticmethod ##静态方法
def send_message(content):
#静态方法里面调用对象方法
##没有传self,就不能用self来访问
phone().call()#创建一个对象来调用对象方法
print('我可以发:{}'.format(content))
@classmethod ##类方法
def watch_video(cls,*args):
#类方法里面调用对象方法,静态方法
cls.send_message("hello world")
cls().send_message("python13")#创建一个对象来调用对象方法
phone().send_message('晚上好呀')#创建一个对象来调用对象方法
print('看视频{}'.format(args))
def music(self):
print('听音乐')
def take_photo(self): #对象方法里面调用静态方法、类方法的用法
self.send_message('晚上好')
self.watch_video('西游记')
self.call()
print('我可以拍照')
def phone_info(self):
#在函数里想访问类的属性要用self 或者类
print('手机的品牌是{},价格是{},颜色是{},操作系统是{}'
.format(self.brand_name,self.price,self.color,self.os))
##如果创建了初始化函数,创建对象的时候就必须要传参,不然就报错
对象拥有里面所有属性和方法的访问权,访问全部
Phone('华为','4500','black','android').call('spring')
类的继承
class ApplePhone(Phone): #继承父类
def measure(self):
print("测身高")
ApplePhone("iphonex","black","9999","5.5","ios").call()
#父类里面有初始化函数,子类在创建对象的时候要传参,除非有默认值
#父类不可以调用子类的measure方法