马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
国庆归来,学习类与对象开始P59-P61 ###########################################P59 对象与类
'''
20221008
'''
class Turtle:
head=1
eyes=2
legs=4
shell=True
def crawl(self):
print('11')
def run(self):
print('22')
def bite(self):
print('33')
def eat(self):
print('44')
def sleep(self):
print('55')
t1 =Turtle()
print(t1.head)
t1.crawl()
t2=Turtle()
t2.legs=321
print(t2.legs)
print(t1.legs)
t1.mouth=5
print(t1.mouth)
print(dir(t1))
print(dir(t2))
x=520
print(type(x))
y='Fishc'
print(type(y))
class C:
def hello(self):
print(self)
c=C()
print(c)
c.hello()
###########################################P60 对象与类
class A:
x=520
def hello(self):
print('你好,我是A')
class B(A):
pass
b=B()
print(b.x)
b.hello()
class B(A):
x=880
def hello(self):
print('你不好,我是B')
b=B()
b.hello()
print(isinstance(b,B))
print(isinstance(b,A))
print(issubclass(A,B))
print(issubclass(B,A))
class B:
x=520
y=250
def hello(self):
print('你好,我是B')
class C(A,B):
pass
c=C()
print(c.x)
print(c.y)
c.hello()
class Turtle:
def say(self):
print('不不不')
class Cat:
def say(self):
print('喵喵喵')
class Dog:
def say(self):
print('汪汪汪')
class Garden:
t=Turtle()
c=Cat()
d=Dog()
def say(self):
self.t.say()
self.c.say()
self.d.say()
g=Garden()
print(g)
g1=Garden
print(g1)
g.say()
###########################################P60 对象与类
d =C()
d.x=2500
c.x=250
print(d.__dict__)
print(c.__dict__)
d.y=660
print(d.__dict__)
class C:
def set_x(self,v):
self.x=v
c=C()
print(c.__dict__)
c.set_x(5200)
print(c.__dict__)
class C:
x=100
def set_x(self,v):
x=v
c=C()
c.set_x(250)
print(c.x)
C.x=2500
print(c.x)
|