类与对象
class phone: brand_name='oppo' color ='black' price =2900 size = '5.5' os = 'Android' 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('我可以拍照') phone.send_message('晚安咯') phone.watch_video("变形金刚") phone.call('哈哈')#这个可以通过传参来访问 def phone_info(self): #在函数里想访问类的属性要用self 或者类 print('手机的品牌是{},价格是{},颜色是{},操作系统是{}' .format(self.brand_name,phone.price,self.color,self.os)) #属性可以通过类和对象来访问 p = phone() print(p.brand_name) print(p.size) print(p.color) print(p.os) print(p.price) #print(phone.brand_name) p.call()#self是对象本身,必须由对象来调用 #phone.call() phone.call('华华')#要想用类访问类的方法,必须要传一个参数才行 ##对象方法:只能通过对象来调用 ##静态方法:@staticmethod 放在函数上面来标记/装饰 ##可以用类以及对象来调用 # phone.send_message() # p.send_message() ##类方法:@classmethod 放在函数上面来标记/装饰 ##可以用类以及对象来调用 # phone.watch_video() # p.watch_video() #类方法和普通函数有啥区别? #除了对象方法必须有self,类方法必须有cls参数以外,其他并无区别 # 参数类型:位置参数、默认参数、关键字参数、动态参数 #return 支持 #必须通过该类的对象或者该类来访问 不能直接访问 # print(p.call())#打印返回值 # p.send_message('你好') # p.watch_video("海王","无名之辈") #p.phone_info() p.take_photo() p.watch_video() p.send_message("111")