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

ตอบกระทู้

รูปแสดงอารมณ์
:icon_plusone: :like: :plusone: :gfb: :-D :) :( :-o 8O :? 8) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: :angry: :baa: :biggrin:
รูปแสดงอารมณ์อื่นๆ

BBCode เปิด
[img] เปิด
[url] เปิด
[Smile icon] เปิด

กระทู้แนะนำ
   

มุมมองที่ขยายได้ กระทู้แนะนำ: ทำความรู้จักกับภาษา python (45) : การสืบทอด Class (คลาส)

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

โดย MBMoo » 09/06/2020 11:18 am

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

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 1575 times

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

โดย jirawoot » 24/06/2019 11:11 am

สืบทอด 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 1822 times

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

โดย rangsan » 05/05/2018 4:52 pm

การสืบทอด 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 2353 times
ศึกษาจาก : https://www.youtube.com/watch?v=qDvQ-7q ... lzdKrpxsMM

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

โดย Vlogsize » 29/01/2018 1:55 pm

เป็น code ที่เข้าใจง่ายมากเลยครับ

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

โดย Jom07 » 25/01/2018 5:08 pm

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

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

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

โดย Four » 25/01/2018 4:45 pm

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

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))
ผลรัน

รูปภาพ

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

โดย dawthana » 19/01/2017 2:07 pm

สามารถศึกษาเพิ่มเติมได้ที่ https://www.mindphp.com/vdo-tutorial-python/ ... ython.html

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

โดย dawthana » 19/01/2017 12:29 pm

การสืบทอด 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 2526 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 2526 times
ตัวอย่างการเรียกใช้ Class Inheritance แบบ Multiple
class_Inheritance_Multiple_class.png
class_Inheritance_Multiple_class.png (33.45 KiB) Viewed 2526 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 2526 times
ตัวอย่างการใช้งานฟังก์ชัน instanceเหมือนเป็นการถามว่า c เป็น object ของคลาส child หรือป่าว ถ้าเป็นก็จะคืนค่ามาว่าจริงหรือเท็จ ดังภาพ แต่ในภาพ c เป็น object ของทั้งคลาส child และ คลาส parent เพราะว่าคลาส child สืบทอดมาจากคลาส parent
instance.png
instance.png (98.69 KiB) Viewed 2526 times
ศึกษาข้อมูลมาจาก https://www.youtube.com/watch?v=qDvQ-7qJuoE&t=624s

ข้างบน