标签:ack sel 动物 数据 variable 抛出异常 abstract war 自己
面向过程编程:类似于工厂的流水线
优点:逻辑清晰
缺点:扩展性差
面向对象编程:核心是对象二字,对象属性和方法的集合体,面向对象编程就是一堆对象交互
优点:扩展性强
缺点:逻辑非常乱
对象:属性和方法的集合体
类:一系列相同属性和方法的集合体
现实世界中先有对象后有类,python中先有类,再实例化出对象
先对象本身-->类-->父类-->父类的父类-->object-->自己定制的元类-->type
class People:
pass
?
p1 = Peolple()
p1.name = ‘nick‘
?
p2 = People()
p2.name = ‘tank‘
class People:
def eat(self):
print(self, ‘eat....‘)
?
p1 = Peolple()
p1.eat()
p1.name = ‘nick‘
?
p2 = People()
p2.eat()
p2.name = ‘tank‘
lis = [1,2,3] # lis = list([1,2,3])
?
class foo:
def __init__(self,name):
self.name = name
?
f = foo(‘name‘)
?
lis.append(4) # 对象调对象绑定的方法,会自动传参
list.append(lis,4) # 类调用对象绑定的方法,必须得传参
继承父类,则会有父类的所有属性和方法
class ParentClass1():
pass
class ParentClass2():
pass
?
class SubClass(ParentClass1,ParentClass2):
pass
继承父类的同时自己有init,然后也需要父类的init
class ParentClass1():
def __init__(self,name):
pass
?
class SubClass(ParentClass):
def __init__(self,age):
# 1. ParentClass1.__init__(self,name)
# 2. super(SubClass,self).__init__(name)
self.age = age
类对象可以引用/当做参数传入/当做返回值/当做容器元素,类似于函数对象
class ParentClass1():
count = 0
def __init__(self,name):
pass
?
class SubClass(ParentClass):
def __init__(self,age):
self.age = age
?
pc = ParentClass1()
sc = SubClass()
?
sc.parent_class = pc # 组合
sc.parent_class.count # 0
新式类:继承object的类,python3中全是新式类
经典类:没有继承object的类,只有python2中有
在菱形继承的时候,新式类是广度优先(老祖宗最后找);经典类深度优先(一路找到底,再找旁边的)
一种事物的多种形态,动物-->人/猪/狗
# 多态
import abc
?
class Animal(metaclass=abc.ABCmeta):
def eat():
print(‘eat‘)
?
class People(Animal):
def eat():
pass
?
class Pig(Animal):
def eat():
pass
def run():
pass
?
class Dog(Animal): # 报错
def run():
pass
# 多态性
peo = People()
peo.eat()
peo1 = People()
peo1.eat()
pig = Pig()
pig.eat()
?
def func(obj):
obj.eat()
?
class Cat(Animal):
def eat():
pass
cat = Cat()
?
func(cat)
鸭子类型:只要长得像鸭子,叫的像鸭子,游泳像鸭子,就是鸭子.
隐藏属性,只有类内部可以访问,类外部不可以访问
class Foo():
__count = 0
def get_count(self):
return self.__count
f = Foo()
f.__count # 报错
f._Foo__count # 不能这样做
把方法变成属性引用
class People(): def __init__(self,height,weight): self.height = height self.weight = weight @property def bmi(self): return weight/(height**2) @bmi.setter def bmi(self,value) print(‘setter‘) @bmi.deleter def bmi(self): print(‘delter‘) peo = People peo.bmi
没有任何装饰器装饰的方法就是对象的绑定方法, 类能调用, 但是必须得传参给self
被 @classmethod 装饰器装饰的方法是类的绑定方法,参数写成cls, cls是类本身, 对象也能调用, 参数cls还是类本身
被 @staticmethod 装饰器装饰的方法就是非绑定方法, 就是一个普通的函数
isinstance判断是否为类的实例化对象,会检测父类,而type不会检测父类
issubclass,判断是否为其子类
hasattr:通过字符串判断是否类属性存在
getattr:通过字符串获取类属性
setattr:通过字符串修改类属性
delattr:通过字符串删除类属性
class Foo: def __init__(self): print(‘Foo()会触发我‘) def __call__(self): print(‘Foo()()/f()会触发我‘) f = Foo() f()
class Foo: def __new__(self): print(‘new‘) obj = object.__new__(self) return obj def __init__(self): print(‘init‘) f = Foo()
元类用来造类的
元类()-->类-->init
元类()()-->对象--->call
类分为几部分:类名/类体名称空间/父类们
class Mymeta(type): def __init__(self,class_name,class_bases,class_dic): # 控制类的逻辑代码 super().__init__(class_name,class_bases,class_dic) def __call__(self,*args,**kwargs): # 控制类实例化的参数 obj = self.__new__(self) # obj就是实例化的对象 self.__init__(obj,*args,**kwargs) print(obj.__dict__) # 控制类实例化的逻辑 return obj class People(metaclass=Mymeta): def __init__(self,name,age): self.name = name self.age = age
NAME = ‘nick‘ AGE = 18 class People(): __instance = None @classmethod def from_conf(cls): if cls.__instance: return cls.__instance cls.__instance = cls(NAME,AGE) return cls.__instance
People.from_conf()
People.from_conf()
NAME = ‘nick‘ AGE = 18 def deco(cls): cls.__instance = cls(NAME,AGE) def wrapper(*args,**kwargs): if len(args) == 0 and len(kwargs) == 0: return cls.__instance res = cls(*args,**kwargs) return res return wrapper @deco class People(): def __init__(self,name,age): self.name = name self.age = age
peo1 = People()
peo2 = People()
NAME = ‘nick‘ AGE = 18 class Mymeta(type): def __init__(self,class_name,class_bases,class_dict): super().__init__(class_name,class_bases,class_dict) self.__instance = self(NAME,AGE) def __call__(self,*args,**kwargs): if len(args) == 0 and len(kwargs) == 0: return self.__instance obj = object.__new__(self) self.__init__(obj,*args,**kwargs) return obj class People(metaclass=Mymeta): def __init__(self,name,age): self.name = name self.age = age peo1 = People() peo2 = People()
x = 10 y = 20 c = 30 try: 1/0 except Exception as e: print(e)
抛出异常
raise KeyboardInterrupt(‘中断捕捉‘)
判断某一行代码是否有问题
标签:ack sel 动物 数据 variable 抛出异常 abstract war 自己
原文地址:https://www.cnblogs.com/zrx19960128/p/11074711.html