标签:rand python ugui super lse init self user move
一、类的继承
例:
import random as r
class Fish():
def __init__(self):
self.x=r.randint(0,10)
self.y=r.randint(0, 10)
def move(self):
print("现在的位置是:" ,self.x,self.y)
class Goldfish(Fish):
pass
class Shark(Fish):
def __init__(self):
super().__init__()
self.hungry=True
def eat(self):
if self.hungry:
print("我在吃东西,肚子好饿")
self.hungry=False
else:
print("好饱,吃不下了,呜呜")
fish=Fish()
fish.move()
goldfish=Goldfish()
goldfish.move()
shark=Shark()
shark.move()
shark.eat()
shark.eat()
二、类的多重继承
例:
class Base1:
def Fun1(self):
print("我是Fun1,我是Base1的方法")
class Base2:
def Fun2(self):
print("我是Fun2,我是Base2的方法")
class User(Base1,Base2):
pass
client=User()
client.Fun1()
client.Fun2()
三、类的组合
例:
class Fish:
def __init__(self,x):
self.num=x
class Wugui:
def __init__(self, x):
self.num = x
class Pool:
def __init__(self,x,y):
self.wugui=Wugui(x)
self.fish=Fish(y)
def print_num(self):
print("水池里面一共有乌龟%d 只,小鱼%d 条 " % (self.wugui.num,self.fish.num))
pool=Pool(1,3)
pool.print_num()
标签:rand python ugui super lse init self user move
原文地址:https://www.cnblogs.com/lalalaxixixi/p/9104402.html