Python的兩種繼承方法

2021-08-14 08:43:27 字數 1258 閱讀 7170

# class animal:   #經典類

class animal(object): #新式類

def __init__(self,name):

self.name = name

def eat(self,foot):

print('can eat',foot)

class relation(object):

def make_friends(self,obj):

print('%s is ****** friends with %s'%(self.name,obj.name))

class dog(animal,relation): #如果它們都有建構函式,在繼承順序上就會先繼承animal的init建構函式

# def __init__(self,age): #這樣直接就會覆蓋父類的所有建構函式

# self.age = age

#所以要這樣

def __init__(self,name,age):

# animal.__init__(self,name) #方法一 經典類的寫法

super(dog,self).__init__(name) #方法二 這種方法比較好 新式類的寫法

self.age = age

def run(self):

print('dog is run soon')

print('%d'%self.age)

# def eat(self): #直接就將父類方法替換了

# print('dog is can eat')

def eat(self,foot):

animal.eat(self,foot) #這樣就可以在父類方法裡面新增方法

print('the dog is can eat')

class cat(animal):

def pashu(self):

print('cat can pashu %s'%self.name)

dog = dog('dog',10)

# dog.eat('麵包')

# dog.run()

cat = cat('cat')

# cat.pashu()

# 下面這個就是多繼承,注意,這裡的cat作為乙個引數傳進去了

# 其實很好理解

# dog繼承了relation,因此可以多繼承

dog.make_friends(cat)

兩種attach to process的方法

背景 今天在做keepalive的實驗,設法模擬keepalive不成功的場景,從而達到 the local tcp will keep sending keep alive packet in an interval of keepaliveinterval for tcpmaxdataretra...

兩種attach to process的方法

背景 今天在做keepalive的實驗,設法模擬keepalive不成功的場景,從而達到 the local tcp will keep sending keep alive packet in an interval of keepaliveinterval for tcpmaxdataretra...

兩種排序方法

題目描述 考拉有n個字串字串,任意兩個字串長度都是不同的。考拉最近學習到有兩種字串的排序方法 1.根據字串的字典序排序。例如 car carriage cats doggies koala 2.根據字串的長度排序。例如 car cats koala doggies carriage 考拉想知道自己的...