サンプル集  >  Python  >  classの継承
classの継承
2023/05/20

classの継承の利用例です。

◆環境
OS Windows 10 Home 22H2 64bit OS x64 プロセッサ
Python 3.9.6
VS Code 1.59.0

クラスを定義します。

Class1st.py
1: 
2: 
3: 
class Class1st(object):
    def info1st(self):
        print("1st class.")

Class1stを継承したクラスClass2ndを定義します。

Class2nd.py
1: 
2: 
3: 
4: 
5: 
from Class1st import Class1st

class Class2nd(Class1st):
    def info2nd(self):
        print("2nd class.")

Class2ndを利用してみます。

keisyoTest.py
1: 
2: 
3: 
4: 
5: 
from Class2nd import Class2nd

x = Class2nd()
x.info2nd()
x.info1st()

実行してみます。

PS C:\python> py .\keisyoTest.py
2nd class.
1st class.

期待通りに動作しました。

▲ PageTop  ■ Home


Copyright (C) 2023 ymlib.com