抽象类
抽象类简单的说就是不完整的类,学术语言描述就是具有抽象方法的类就叫抽象类
抽象方法
抽象方法同样简单的说就是不完整的方法,学术语言描述就是没有具体方法体的方法就叫抽象方法
元类
元类就是用来产生类的类
元类的作用
元类的作用就是用来产生类
查看一个类的元类的方法
class User(object):
pass
if __name__=="__main__":
print(type(User))
print(User.__class__)
~~~
执行结果如下:
~~~python
<class 'type'>
<class 'type'>
抽象类的特点:
抽象类的作用
import abc
class User(metaclass=abc.ABCMeta):
@abc.abstractmethod
def user_add(self):
pass
@abc.abstractclassmethod
def user_update(cls):
pass
@abc.abstractmethod
def user_del():
pass
@abc.abstractstaticmethod
def user_find(self):
pass
import abc
class User(metaclass=abc.ABCMeta):
id = 9999
@abc.abstractmethod
def user_add(self):
pass
@abc.abstractclassmethod
def user_update(cls):
pass
@abc.abstractmethod
def user_del(self):
pass
@abc.abstractstaticmethod
def user_find():
pass
class MyUser(User):
def __init__(self):
pass
def user_add(self):
print("user add")
def user_del(self):
print("user del")
def user_find(self):
print("user find")
@classmethod
def user_update(cls):
print("user update")
myuser = MyUser()
print(myuser.id)
myuser.user_add()
myuser.user_del()
myuser.user_find()
MyUser.user_update()
执行结果如下:
9999
user add
user del
user find
user update
import abc
class User(metaclass=abc.ABCMeta):
id=888
@abc.abstractmethod
def user_add(self):
pass
@abc.abstractclassmethod
def user_update(cls):
pass
@abc.abstractmethod
def user_del(self):
pass
@abc.abstractstaticmethod
def user_find():
pass
class User1(User):
def user_add(self):
print("user add")
class User2(User1):
@staticmethod
def user_del():
print("user del")
class User3(User2):
@classmethod
def user_update(cls):
print("user update")
class User4(User3):
def user_find(self):
print("user find")
user = User4()
print(user.id)
user.user_add()
User4.user_del()
user.user_update()
user.user_find()
执行结果如下:
888
user add
user del
user update
user find