Task10:类、对象与魔法方法
程序员文章站
2022-04-05 18:57:36
...
思维导图
代码
#对象是类的实例,对象=属性+方法
class Turtle:#类要以大写字母开头
#以下为属性
color='green'
weight=10
legs=4
shell=True
mouth='大嘴'
def climb(self):
print('我正在努力的向前爬')
def run(self):
print('我正在飞快地向前跑')
tt=Turtle()
print(tt)
tt.climb()
#继承:子类自动共享父类之间的数据和方法的机制
class MyList(list):
pass
lst=MyList([1,5,2,7,8])
lst.append(9)
lst.sort()
print(lst)
#多态:不同对象对同一方法响应不同的行动
class Animal:
def run(self):
raise AttributeError('子类必须实现这个方法')
class People(Animal):
def run(self):
print('人正在走')
class Pig(Animal):
def run(self):
print('pig is walking')
def func(animal):
animal.run()
func(Pig())
class Ball:
def __init__(self,name):
self.name=name
def kick(self):
print('我叫%s,该死的,谁踢我'%self.name)
a=Ball('球A')
b=Ball('球B')
c=Ball('球C')
a.kick()
b.kick()
class JustCounter:
__secretCount=0#私有变量
publicCount=0#公有变量
def count(self):
self.__secretCount+=1
self.publicCount+=1
print(self.__secretCount)
counter=JustCounter()
counter.count()
counter.count()
print(counter.__JustCounter__secertCount)
#类的私有方法实例
class Site:
def __init__(self,name,url):
self.name=name
self._url=url
def who(self):
print('name:',self.name)
print('url:',self._url)
def __foo(self):
print('这是私有方法')
def foo(self):
print('这是公共方法')
self.__foo()
x=Site('熊熊和兔兔的冰激凌世界','Http://123.com')
x.who()
x.foo()
#继承
class people:
name=''
age=0
__weight=0#定义私有属性,外部无法直接进行访问
def __init__(self,n,a,w):
self.name=n
self.age=a
self.__weight=w
def speak(self):
print('%s说:我%d岁。'%(self.name,self.age))
class student(people):
grade=''
def __init__(self,n,a,w,g):#调用父类的构函
people.__init__(self,n,a,w)
self.grade=g
#覆盖父类的方法
def speak(self):
print('%s 说:我%d岁了,我在读%d年级'%(self.name,self.age,self.grade))
s=student('小熊',10,60,3)
s.speak()
#组合
class Turtle:
def __init__(self,x):
self.num=x
class Fish:
def __init__(self,x):
self.num=x
class Pool:
def __init__(self,x,y):
self.turtle=Turtle(x)
self.fish=Fish(y)
def print_num(self):
print('水池里有乌龟%s只,小鱼%s条'%(self.turtle.num,self.fish.num))
p=Pool(2,3)
p.print_num()
# 创建类对象
class Test(object):
class_attr = 100
def __init__(self):
self.sl_attr = 100
def func(self):
print('类对象.类属性的值:', Test.class_attr)
print('self.类属性的值', self.class_attr)
print('self.实例属性的值', self.sl_attr)
a=Test()
a.func()
b=Test()
b.func()
a.class_attr=200
a.sl_attr=200
a.func()
b.func()
Test.class_attr=300
a.func()
b.func( )
#绑定
class CC:
def setXY(self,x,y):
self.x=x
self.y=y
def printXY(self):
print(self.x,self.y)
dd=CC()
print(dd.__dict__)
print(vars(dd))
print(CC.__dict__)
#直接用CC.结果会很奇怪
dd.setXY(4,5)
print(dd.__dict__)
#hasattr()用于判断对象是否包含对应的属性
class Coordinate:
x = 10
y = -5
z = 0
point1 = Coordinate()
print(hasattr(point1, 'x'))
print(hasattr(point1, 'y'))
print(hasattr(point1, 'z'))
print(hasattr(point1, 'no'))
#getattr是用于返回一个对象的属性值
class A(object):
bar = 1
a = A()
print(getattr(a, 'bar')) # 1
print(getattr(a, 'bar2', 3)) # 3是默认值
class A(object):
def set(self,a,b):
x=a
a=b
b=x
print(a,b)
a=A()
c=getattr(a,'set')
c(a='1',b='2')
class A(object):
bar=1
a=A()
print(getattr(a,'bar'))
setattr(a,'bar',5)
print(a.bar)
setattr(a,'age',28)
print(a.age)
上一篇: WPF listbox数据绑定