类和对象

##类和对象

x = 520
type(x)
<class 'int'>
y="fish"
type(y)
<class 'str'>
class c:
def hello():
print("nh")


class c:
def get_self(self):
print(self)


C=c()
C.get_self()
<__main__.c object at 0x0000029505821D20>
c
<class '__main__.c'>

class A:
x=520
def hello(self):
print("你好,我是A")


class B(A):
pass

b=B()
b.x
520
b.hello()
你好,我是A
class B(A):
X=880
def hello(self):
print("你好,我是B")

b=B()
b.x
520
b.hello()
你好,我是B
isinstance(b,B)
True
isinstance(b,A)
True
issubclass(A,B)
False
issubclass(B,A)
True

#多重继承
class B:
x=880
y=520
def hello(self):
print("你好,我是A")


class C(A,B):
pass

c=C()
c.x

#组合
class turtle:
def say(self):
print("不积跬步无以至千里")

class cat:
def say(self):
print("喵喵喵")


class dog:
def say(self):
print("万钢王")


class gardon:
t=turtle()
c=cat()
d=dog()
def say(self):
self.t.say()
self.c.say()
self.d.say()


g=gardon()
g.say()
不积跬步无以至千里
喵喵喵
万钢王

 

c.__dict__
{}
class C:
x=100
def set_x(self,v):
x=v


c=C()
c.set_x(250)
c.x
100
C.x
100
C.x-250
-150
C.x=250
c.x
250
c.__dict__
{}
c.y
Traceback (most recent call last):
File "<pyshell#125>", line 1, in <module>
c.y
AttributeError: 'C' object has no attribute 'y'


class C:
pass

c.x=250
c.y="jinh"
c.z=[1,2,3]
print(c.x)
250
print(c.y)
jinh
print(c.z)
[1, 2, 3]

#构造函数
class C:
def __init__(self,x,y):
self.x=x
self.y=y
def add(self):
return self.x+self.y
def mul(self):
return self.x*self.y


c=C(2,3)
c.add()
5
c.mul()
6
c.__dict__
{'x': 2, 'y': 3}
d=C(4,5)
d.__dict__
{'x': 4, 'y': 5}
d.add()
9
d.mul()
20

#重写
class D(C):
def __init__(self,x,y,z):
C.__init__(self,x,y)
self.z=z
def add(self):
return C.add(self)+self.z
def mul(self):
return C.mul(self)*self.z


d=D(2,3,4)
d.add()
9
d.mul()
24


class A:
def __init__(self):
print("哈喽,我是A")


class B1(A):
def __init__(self):
A.__init__(self)
print("哈喽,我是B1")


class B2(A):
def __init__(self):
A.__init__(self)
print("哈喽,我是B2")

class C(B1,B2):
def __init__(self):
B1.__init__(self)
B2.__init__(self)
print("哈喽,我是C")


c=C()
哈喽,我是A
哈喽,我是B1
哈喽,我是A
哈喽,我是B2
哈喽,我是C

#super函数
class B1(A):
def __init__(self):
super().__init__()
print("哈喽,我是B1")


class B2(A):
def __init__(self):
super().__init__()
print("哈喽,我是B2")


class C(B1,B2):
def __init__(self):
super().__init__()
print("哈喽,我是C")


c=C()
哈喽,我是A
哈喽,我是B2
哈喽,我是B1
哈喽,我是C

#mixin语句
class B1(A):
def __init__(self):
super().__init__()
print("哈喽,我是B1")


class B2(A):
def __init__(self):
super().__init__()
print("哈喽,我是B2")


class C(B1,B2):
def __init__(self):
super().__init__()
print("哈喽,我是C")


c=C()
哈喽,我是A
哈喽,我是B2
哈喽,我是B1
哈喽,我是C
C.mro()
[<class '__main__.C'>, <class '__main__.B1'>, <class '__main__.B2'>, <class '__main__.A'>, <class 'object'>]
B1.mro()
[<class '__main__.B1'>, <class '__main__.A'>, <class 'object'>]

============================ RESTART: D:/Desk/11.py ============================
This is a text.
MySubClass.mro()
[<class '__main__.MySubClass'>, <class '__main__.LoggerMixin'>, <class '__main__.Displayer'>, <class 'object'>]

#多态
class Cat:
def __init__(self,name,age):
self.name=name
self.age=age
def intro(self):
print("我是一只小猫咪,我叫{self.name},今年{self.age}岁")
def say(self):
print("mua")

class Dog:
def __init__(self,name,age):
self.name=name
self.age=age
def intro(self):
print("我是一只小狗,我叫{self.name},今年{self.age}岁")
def say(self):
print("油耗")


class Pig:
def __init__(self,name,age):
self.name=name
self.age=age
def intro(self):
print("我是一只小猪,我叫{self.name},今年{self.age}岁")
def say(self):
print("oink")


c=Cat("web",4)
d=Dog("步步",7)
p=Pig("大肠",6)
def animal(x):
x.intro()
x.say()


animal(c)
我是一只小猫咪,我叫{self.name},今年{self.age}岁
mua
animal(p)
我是一只小猪,我叫{self.name},今年{self.age}岁
oink
animal(d)
我是一只小狗,我叫{self.name},今年{self.age}岁
油耗

#属性访问
class C:
def __init__(self,name,age):
self.name=name
self.__age=age
def __getattribute__(self,attrname):
print("拿来吧你")
return super().__getattribute__(attrname)
def __getattr__(self,attrname):
if attrname == "you":
print("I love you")
else:
raise AttributeError(attrname)


c =C("小甲鱼",18)
c.you
拿来吧你
I love you
c.x
拿来吧你
Traceback (most recent call last):
File "<pyshell#23>", line 1, in <module>
c.x
File "<pyshell#20>", line 12, in __getattr__
raise AttributeError(attrname)
拿来吧你
拿来吧你
AttributeError: x
class D:
def __setattr__(self,name,value):
self.__dict__[name]=value


d=D()
d.name="小甲鱼"
d.name
'小甲鱼'
class D:
def __setattr__(self,name,value):
self.__dict__[name]=value
def __delattr__(self,name):
del self.__dict__[name]


d=D()
d.name="小甲鱼"
d.__dict__
{'name': '小甲鱼'}
del d.name
d.__dict__
{}

class D:
def __getitem__(self,index):
print(index)


d=D()
d[2]
2
d[2:8]
slice(2, 8, None)
s="I love you"
s[2:6]
'love'
s[slice(2,6)]
'love'
s[7:]
'you'
s[slice(7,None)]
'you'
s[::4]
'Ivo'
s[slice(None,None,4)]
'Ivo'
class C:
def __init__(self,data):
self.data=data
def __getitem__(self,index):
return self.data[index]
def __setitem__(self,index,value):
self.data[index]=value


c=C([1,2,3,4,5])
c[1]
2
c[1]=1
c[1]
1
c[2:4]
[3, 4]
c[2:4]=[2,3]
c[:]
[1, 1, 2, 3, 5]
class C:
def __init__(self,data):
self.data=data
def __getitem__(self,index):
return self.data[index]*2


c=C([1,2,3,4,5])
for i in c:
print(i,end='')


246810
x=[1,1,2,3,4]
dir(x)
['__add__', '__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
next(x)
Traceback (most recent call last):
File "<pyshell#71>", line 1, in <module>
next(x)
TypeError: 'list' object is not an iterator
for i in x:
print(i,end='')


11234

_=iter(x)
while True:
try:
i=_.__next__()
except StopIteration:
break
print(i,end='')


11234
class Double:
def __init__(self,start,stop):
self.value=start-1
self.stop=stop
def __iter__(self):
return self
def __next__(self):
if self.value ==self.stop:
raise StopIteration
self.value +=1
return self.value*2


d=Double(1,5)
for i in d:
print(i,end='')


46810

posted @   奋斗吧-少年  阅读(25)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示