以下内容来自于网课学习笔记。
使用的环境:
- Window10+64位操作系统
- PyCharm+Python3.7
一、类与对象
class Ball:
def setName(self,name):
self.name=name
def kick(self):
print("我叫%s" %self.name)
a=Ball()
a.setName("A")
b=Ball()
b.setName("B")
a.kick()
b.kick()
结果:
我叫A
我叫B
二、__init__构造
- init(self,param1)
结果:class Ball: def __init__(self,name): self.name = name def kick(self): print("我叫%s" % self.name) a = Ball('tt') a.kick()
我叫tt
- init(self,param1,param2…)
class Ball: def __init__(self, name): self.name = name def __init__(self, name, name2, name3): self.name = name self.name2 = name2 self.name3 = name3 def kick(self): print("%s" % self.name) print("%s" % self.name2) print("%s" % self.name3) a = Ball('tt', "rr", "ww") a.kick()
三、封装
公有,私有
在Python中定义私有变量只需要在变量名数名前加上两个下划线“_”,那么这个函数或变量就会为私有的了。
class WaitFoF:
name1="WW"
__name="TT"
def getPrivateName(self):
print(self.__name)
w=WaitFoF()
print(w.name1)
print(w.__name)
w.getPrivateName()
结果:
对象访问私有变量时出错。
但在类内使用是正确的。
四、继承
1. 单继承
class Parent:
def hello(self):
print("正在调用父类的方法...")
# 括号内的类为父类
class Child(Parent):
pass
p=Parent()
p.hello()
c=Child()
c.hello()
2. 多继承
class Base1:
def foo1(self):
print("正在调用Base1······")
class Base2:
def foo2(self):
print("正在调用Base2······")
class C(Base1,Base2):
pass
c=C()
c.foo1()
c.foo2()
五、多态
如果子类中定义与父类同名的方法或属性,则会自动覆盖父类对应的方法或属性。
class Parent:
def hello(self):
print("正在调用父类的方法...")
# 括号内的类为父类
class Child(Parent):
def hello(self):
print("正在调用子类的方法...")
p=Parent()
p.hello()
c=Child()
c.hello()
六、组合
将几个没有纵向继承关系的类组合到一起。
# 乌龟类
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):
#turtle是一个Turtle对象
#fish是一个Fish对象
self.turtle = Turtle(x)
self.fish = Fish(y)
def print_num(self):
print("水池中有乌龟 %d只,小鱼%d条"%(self.turtle.num,self.fish.num))
pool=Pool(30,10)
pool.print_num()
结果:
水池中有乌龟 30只,小鱼10条
七、子类中使用父类方法的方式
📢📢 调用未绑定的父类方法
📢📢 使用super函数
import random as r
class Fish:
# 初始座标
def __init__(self):
# 0~10之间
self.x=r.randint(0,10)
self.y=r.randint(0,10)
# 一路向西游
def move(self):
self.x-=1
print("Fish: ["+str(self.x)+","+str(self.y)+"]")
class Goldfish(Fish):
pass
class Shark(Fish):
def __init__(self):
self.hungry=True
def eat(self):
if self.hungry==True:
print("Shark: is wait for eating")
self.hungry=False
else:
print("Shark: is not hungry")
print("Fish:")
fish=Fish()
fish.move()
fish.move()
fish.move()
print("Goldfish:")
goldfish=Goldfish()
goldfish.move()
goldfish.move()
print("Shark:")
shark=Shark()
shark.eat()
shark.eat()
shark.move()
由于Shark类重载了init方法,会覆盖父类的方法,因此Shark类中不存在x。
在子类中使用父类方法的方式:
-
调用未绑定的父类方法
class Shark(Fish): def __init__(self): # 调用未绑定的父类方法 Fish.__init__(self) self.hungry=True def eat(self): if self.hungry==True: print("Shark: is wait for eating") self.hungry=False else: print("Shark: is not hungry") print("Fish:") fish=Fish() fish.move() fish.move() fish.move() print("Goldfish:") goldfish=Goldfish() goldfish.move() goldfish.move() print("Shark:") shark=Shark() shark.eat() shark.eat() shark.move()
-
使用super函数
import random as r class Fish: # 初始座标 def __init__(self): # 0~10之间 self.x=r.randint(0,10) self.y=r.randint(0,10) # 一路向西游 def move(self): self.x-=1 print("Fish: ["+str(self.x)+","+str(self.y)+"]") class Goldfish(Fish): pass class Garp(Fish): pass class Salmon(Fish): pass class Shark(Fish): def __init__(self): # 使用super方法 super().__init__() self.hungry=True def eat(self): if self.hungry==True: print("Shark: is wait for eating") self.hungry=False else: print("Shark: is not hungry") print("Fish:") fish=Fish() fish.move() fish.move() fish.move() print("Goldfish:") goldfish=Goldfish() goldfish.move() goldfish.move() print("Shark:") shark=Shark() shark.eat() shark.eat() shark.move()