程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python super() method query

編輯:Python

The question is in the comments of this code

class Dog: kennel = 0 def __init__(self, breed): self.breed = breed Dog.kennel += 1 def __str__(self): return self.breed + " says: Woof!"class SheepDog(Dog): def __str__(self): return super().__str__() + " Don't run away, Little Lamb!"class LowlandDog(SheepDog): def __str__(self): return Dog.__str__(self) + " I don't like mountains!"# Used here Dog class Why add self, Other images super().__str__() No need to add ?class GuardDog(Dog): def __str__(self): return super().__str__() + " Stay where you are, Mister Intruder!"rocky = LowlandDog("Collie")luna = GuardDog("Dobermann")print(rocky.kennel) # there ouput yes 2, But I think it's 1, It's a little confusing ?print(rocky)

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved