Python Day 21 面向对象 (面向对象的三大特性(二)继承,多态,封装,几个装饰器函数)
Python Day 21 面向对象 (面向对象的三大特性(二)继承,多态,封装,几个装饰器函数)
https://mubu.com/doc/1AqL_M0IbW
继承之钻石继承
多态
封装
几个装饰器函数
@classmethod 可以通过类使用被装饰的方法
@staticmethod 可以使用类.方法调用,而且不需要将self作为参数传进去
class MyFlies: __file_name="log" # @classmethod def open_file(cls,filename): print(cls.__file_name) with open(filename,"rb") as f: return f.read() @staticmethod def look_file(filename): print("根本不知道__file_name的存在") with open(filename,"rb") as f: return f.read() def self_open_file(self,filename): print(self.__file_name) with open(filename, "rb") as f: return f.read() new_file = MyFlies() print(new_file.open_file('log')) print(MyFlies.open_file('log')) # print(new_file.look_file("mylog.log")) # print(new_file.open_file("mylog.log")) # print(new_file.self_open_file("mylog.log")) # print(MyFlies.open_file("mylog.log")) # print(MyFlies.look_file("mylog.log"))