ทำความรู้จักกับภาษา python (45) : การสืบทอด Class (คลาส)

แชร์ความรู้ภาษา Python ไพทอน การเขียนโปรแกรมภาษาไพทอน

Moderator: mindphp, ผู้ดูแลกระดาน

dawthana
PHP Super Hero Member
PHP Super Hero Member
โพสต์: 604
ลงทะเบียนเมื่อ: 07/12/2016 10:55 am

ทำความรู้จักกับภาษา python (45) : การสืบทอด Class (คลาส)

โพสต์ที่ยังไม่ได้อ่าน โดย dawthana »

การสืบทอด Class (คลาส) จะมี 2 แบบ การสืบทอด Class Inheritance (คลาส อินเฮริแท็น) กับ การสืบทอด Class Inheritance แบบ Multiple (มัลติเปิล)
การเขียนโปรแกรม python (ไพทอน) แบบ OOP (โอโอพี) หรือ Object-Oriented (ออบเจ็ค โอเรียนเทด) สามารถทำการสืบทอดความสามารถของ class ที่สร้างไว้แล้ว (class แม่) มาให้กับ class ที่จะสร้างใหม่ (class ลูก)

รูปแบบการสืบทอด Class Inheritance
class Parent :
  • def method_name():
    …………………………..
    …………………………..
    …………………………..
    class Child(Parent):
    def method_name():
    …………………………..
    …………………………..
    …………………………..
ตัวอย่างรูปแบบการสืบทอด Class Inheritance
เป็นการเรียกใช้คลาส โดยให้ c = child (ไชล์ด)
c.childMethod (ซี.ไชล์ดเมดธอด) เป็นการเรียกใช้เมดธอดของคลาสลูกแล้วทำการปริ้น Calling child constructor (คอลลิ่ง ไชล์ด คอนสตรัคเตอร์)
c.parentMethod (ซี.แพเร็นทเมดธอด) เป็นการเรียกใช้เมดธอดของคลาส parent แล้วทำการปริ้น Calling child method (คอลลิ่ง ไชล์ด เมดธอด)
c.parentMethod2 (ซี.แพเร็นทเมดธอด2) เป็นการเรียกใช้เมดธอด 2 ของคลาส parent (แพเร็นท) แล้วทำการปริ้น Calling parent method2 (คอลลิ่ง แพเร็นท เมดธอด2)
c.setAttr(400) (ซีเก็ทแอคทิบิวต์) กับ c.getAttr (ซีเก็ทแอคทิบิวต์) เป็นการเรียกใช้เมดธอดของคลาส parent โดยกำหนดค่า parentAttr = 400 ก็จะได้ค่าที่กำหนดเข้าไปเป็น ('Parent attribute :', 400)
c.childgetAttr() (ซี.ไชล์ดแอคทิบิวต์) เป็นการเรียกใช้เมดธอดของคลาส parent แล้วทำการปริ้น 'Attr in parent from child', 300 (แอคทิบิวต์ อิน แพเร็นท ฟอร์ม ไชล์ด)
Inheritance_1.png
Inheritance_1.png (87.86 KiB) Viewed 2524 times
การสืบทอด Class Inheritance แบบ Multiple คือคลาสลูกสามารถมีคลาสแม่ได้หลายๆคลาส
รูปแบบการสืบทอด Class Inheritance แบบ Multiple โดยใส่ , คั่นระหว่างคลาสแม่
class ParentA :
  • def method_name() :
    …………………………..
    …………………………..
    class ParentB :
    def method_name() :
    …………………………..
    …………………………..
    class Child(ParentA, ParentB) :
    def method_name() :
    …………………………..
    …………………………..
ตัวอย่างรูปแบบการสืบทอด Class Inheritance แบบ Multiple
class_Inheritance_Multiple_1.png
class_Inheritance_Multiple_1.png (75.99 KiB) Viewed 2524 times
ตัวอย่างการเรียกใช้ Class Inheritance แบบ Multiple
class_Inheritance_Multiple_class.png
class_Inheritance_Multiple_class.png (33.45 KiB) Viewed 2524 times
ฟังก์ชันสำหรับดูการสืบทอด Class Inheritance
มีไว้สำหรับดูการสืบทอด
- ฟังก์ชัน issubclass(sub, sup) (อิสซับคลาส) จะคืนค่าเป็น boolean (บลูลีน) เป็นจริงถ้า sub เป็นคลาสลูก
- ฟังก์ชัน isinstance(sub, Class) (อินสแต้นคลาส) จะคืนค่าเป็น boolean ถ้า object (อ๊อบเจค) ที่ต้องการตรวจสอบถูกสร้างมาจาก class (ชื่อคลาส) ซึ่งจะเป็น Class แม่ หรือ Class ลูกก็ได้

ตัวอย่างการใช้งานฟังก์ชัน issubclass
เหมือนเป็นการถามว่า คลาส child เป็นคลาสลูกของคลาส perent หรือป่าว ถ้าเป็นก็จะคืนค่ามาว่าจริงหรือเท็จ ดังภาพ
issubclass.png
issubclass.png (83.04 KiB) Viewed 2524 times
ตัวอย่างการใช้งานฟังก์ชัน instanceเหมือนเป็นการถามว่า c เป็น object ของคลาส child หรือป่าว ถ้าเป็นก็จะคืนค่ามาว่าจริงหรือเท็จ ดังภาพ แต่ในภาพ c เป็น object ของทั้งคลาส child และ คลาส parent เพราะว่าคลาส child สืบทอดมาจากคลาส parent
instance.png
instance.png (98.69 KiB) Viewed 2524 times
ศึกษาข้อมูลมาจาก https://www.youtube.com/watch?v=qDvQ-7qJuoE&t=624s
dawthana
PHP Super Hero Member
PHP Super Hero Member
โพสต์: 604
ลงทะเบียนเมื่อ: 07/12/2016 10:55 am

Re: ทำความรู้จักกับภาษา python (45) : การสืบทอด Class (คลาส)

โพสต์ที่ยังไม่ได้อ่าน โดย dawthana »

สามารถศึกษาเพิ่มเติมได้ที่ https://www.mindphp.com/vdo-tutorial-python/ ... ython.html
Four
PHP Super Hero Member
PHP Super Hero Member
โพสต์: 813
ลงทะเบียนเมื่อ: 08/01/2018 9:55 am

Re: ทำความรู้จักกับภาษา python (45) : การสืบทอด Class (คลาส)

โพสต์ที่ยังไม่ได้อ่าน โดย Four »

โค้ด: เลือกทั้งหมด

class test:
    a = 100
    def __init__(self):
        print("constructor")
    def testmethod(self):
        print("method")
    def set(self, attr):
        test.a = attr
    def get(self):
        print(test.a)

class test2(test):
    def __init__(self):
        print("test2 constructor")

    def test2get(self):
        print(self.a)

print(issubclass(test2, test))
print(issubclass(test, test2))
ผลรัน

รูปภาพ
I am slow walker, but I never walk back. (Abraham Lincoln)
ภาพประจำตัวสมาชิก
Jom07
PHP Super Hero Member
PHP Super Hero Member
โพสต์: 514
ลงทะเบียนเมื่อ: 08/01/2018 9:56 am

Re: ทำความรู้จักกับภาษา python (45) : การสืบทอด Class (คลาส)

โพสต์ที่ยังไม่ได้อ่าน โดย Jom07 »

โค้ด: เลือกทั้งหมด

class Parent:
    parentAttr = 200
    def __init_(self):
        print('Calling parent constructor')
    def parentMethod(self):
        print ('Calling parent method')
    def parentMethod2(self):
        print ('Calling parent method2')
    def setAttr(self, attr):
        Parent.parentAttr = attr
    def getAttr(self):
        print ('parent attribute :', Parent.parentAttr)
class Child(Parent):
    def __init__(self):
        print ('calling child constructor')
    def childMethod(self):
        print ('calling child method')
    def childgetAttr(self):
        print ('Attr in parent calling form child', self.parentAttr)
c = Child()
c.childMethod()
c.parentMethod()
c.parentMethod2()
c.setAttr(400)
c.getAttr()
c.childgetAttr()
ผลรัน

รูปภาพ

ศึกษาข้อมูลจาก :https://www.youtube.com/watch?v=qDvQ-7q ... M&index=45
รูปภาพ
Vlogsize
PHP Newbie
PHP Newbie
โพสต์: 2
ลงทะเบียนเมื่อ: 29/01/2018 11:09 am

Re: ทำความรู้จักกับภาษา python (45) : การสืบทอด Class (คลาส)

โพสต์ที่ยังไม่ได้อ่าน โดย Vlogsize »

เป็น code ที่เข้าใจง่ายมากเลยครับ
rangsan
PHP Hero Member
PHP Hero Member
โพสต์: 199
ลงทะเบียนเมื่อ: 30/04/2018 9:44 am

Re: ทำความรู้จักกับภาษา python (45) : การสืบทอด Class (คลาส)

โพสต์ที่ยังไม่ได้อ่าน โดย rangsan »

การสืบทอด Class (คลาส)

โค้ด: เลือกทั้งหมด

class family:
    familyInherlit = 100
    parentAttribute = 50
    def __init__(self):
        print "Calling family Constructure"
    def familyMethod(self):
        print "Calling family Medthod"
    def familyMethod2(self):
        print "Calling family Medthod2"
    def setInher(self, inher):
        family.familyInherlit = inher
    def getInher(self):
        print "Family Inherlit : ", family.familyInherlit
    def pullAttr(self):
        print "Family Attribute : ",family.parentAttribute
        
class son(family):
    def __init__(self):
        print "Calling child constructure"

    def sonMethod(self):
        print "Calling child Method"
    def childgetInher(self):
        print "Inherlit in family calling form son ",self.familyInherlit
    def childgetAttr(self):
        print "Attribute in family calling form son : ",self.parentAttribute

child = son()
child.sonMethod()
child.familyMethod()
child.familyMethod2()
child.setInher(100)
child.getInher()
child.childgetInher()
child.pullAttr()
child.childgetAttr()

print(isinstance(child,family))
print(isinstance(child,son))
ผลการรัน
output_inherlit_class.png
output_inherlit_class.png (7.02 KiB) Viewed 2351 times
ศึกษาจาก : https://www.youtube.com/watch?v=qDvQ-7q ... lzdKrpxsMM
It’s never too late to start again.
ภาพประจำตัวสมาชิก
jirawoot
PHP VIP Members
PHP VIP Members
โพสต์: 3129
ลงทะเบียนเมื่อ: 17/06/2019 10:30 am

Re: ทำความรู้จักกับภาษา python (45) : การสืบทอด Class (คลาส)

โพสต์ที่ยังไม่ได้อ่าน โดย jirawoot »

สืบทอด class inheritance

โค้ด: เลือกทั้งหมด

class Parent:
    parentAttr = 300
    def __init_(self):
        print('Calling parent constructor')
    def parentMethod(self):
        print ('Calling parent method')
    def parentMethod2(self):
        print ('Calling parent method2')
    def setAttr(self, attr):
        Parent.parentAttr = attr
    def getAttr(self):
        print ('parent attribute :', Parent.parentAttr)
class Child(Parent):
    def __init__(self):
        print ('calling child constructor')
    def childMethod(self):
        print ('calling child method')
    def childgetAttr(self):
        print ('Attr in parent calling form child', self.parentAttr)
c = Child()
c.childMethod()
c.parentMethod()
c.parentMethod2()
c.setAttr(400)
c.getAttr()
c.childgetAttr()
ผลลัพธ์
Selection_002.png
Selection_002.png (8.21 KiB) Viewed 1820 times
ภาพประจำตัวสมาชิก
MBMoo
PHP VIP Members
PHP VIP Members
โพสต์: 25602
ลงทะเบียนเมื่อ: 04/06/2020 10:05 am

Re: ทำความรู้จักกับภาษา python (45) : การสืบทอด Class (คลาส)

โพสต์ที่ยังไม่ได้อ่าน โดย MBMoo »

โค้ด: เลือกทั้งหมด

class Parent1:
    parentAttr = 300
    def __init_(self):
        print('Calling parent1 constructor')
    def setAttr1(self, attr):
        Parent1.parentAttr = attr
    def getAttr1(self):
        print ('parent1 attribute :', Parent1.parentAttr)

class Parent2:
    def __init_(self):
        print('Calling parent2 constructor')
    def setAttr2(self, attr):
        Parent2.parentAttr = attr
    def getAttr2(self):
        print ('parent2 attribute :', Parent2.parentAttr)

class Child(Parent1,Parent2):
    def __init__(self):
        print ('calling child constructor')
    def childMethod(self):
        print ('calling child method')
    def childgetAttr(self):
        print ('Attr in parent calling form child', self.parentAttr)
c = Child()
c.childMethod()
c.setAttr1(400)
c.getAttr1()
c.setAttr2(200)
c.getAttr2()
c.childgetAttr()
print(isinstance(c, Parent2))
print(issubclass(Parent2,Parent1))
ผลลัพธ์
Python Knowledge-1.png
Python Knowledge-1.png (15.28 KiB) Viewed 1573 times
ตอบกลับโพส
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: athirach.offcial, facebook.com [Crawler], Google [Bot] และบุคลทั่วไป 93