Python繼承的乙個筆記

2022-01-29 06:30:18 字數 1496 閱讀 5306

1

class

post():23

def__init__

(self):

4 self.title = '

org'

5 self.__decodetitle

();678

def__decodetitle

(self):

9 self.title = "

title1";

1011

class

post2(post):

1213

def__init__

(self):

14 post.__init__

(self);

15def

__decodetitle

(self):

16 self.title = "

title2";

1718 post =post()

19print

(post.title)

2021 post2 =post2()

22print(post2.title)

執行結果:

title1

title1

熟悉c#程式的我,這段程式給我造成深深的困擾.第二個輸出title1,豈不是python中繼承多型的特點沒法使用??????

後來,我才發現是我多慮了.

1

class

post():23

def__init__

(self):

4 self.title = '

org'

5self.decodetitle();67

8def

decodetitle(self):

9 self.title = "

title1";

1011

class

post2(post):

1213

def__init__

(self):

14 post.__init__

(self);

15def

decodetitle(self):

16 self.title = "

title2";

1718 post =post()

19print

(post.title)

2021 post2 =post2()

22print(post2.title)

這樣就會輸出

title1

title2.

我的理解就是 __decodetitle這個方法是私有的,根本不能繼承下來.

我寫這段**的時候哦,用的記事本些的,沒有報錯.沒有警告,所以讓我困惑了好久.

我不知道其他的編譯器遇到這種會不會有提示什麼的.

乙個繼承錯誤

今天在寫乙個類的繼承,在除錯的時候,虛函式的特點並沒有體現出來,讓我很是不解,用了兩個編譯器,結果都是一樣的。類的 是這樣的 1 ifndef classbrass h included 2 define classbrass h included 3 include 4 include 5 6us...

關於繼承的乙個問題

class a virtual a class b public a virtual b class c public b virtual c int fun int n char last4hex unsigned int num,char a 5 int main int argc,char a...

關於繼承的乙個問題

關於繼承的乙個問題 liupq 2009.12.5 下面程式中的f2 函式為什麼是錯誤的 include include using namespace std class abc protected int n class def public abc error void f3 def b pr...