首页 > 代码库 > Python类的继承示例

Python类的继承示例

class Pet:
  __name = ""
  
  def __init__(self, name):
  	self.__name = name
  	
  def bark(self):
  	return "Pet : " + self.__name
  	
  def getName(self):
  	return self.__name;
  	
  	
class Dog(Pet):
	__age = -1
	
	def __init__(self, name, age):
		Pet.__init__(self, name)
		self.__age = age
		
	def bark(self):
		tmp = Pet.bark(self)
		return tmp + "\nDog : " + self.getName() + ", age:" + str(self.__age)
		
if __name__ == '__main__':
	d = Dog("martin", 30)
	print(d.bark())
		

Python类的继承示例