inheritance គឺជាការបង្កើតថ្នាក់មួយបន្តភ្ជាប់ទៅនឹងថ្នាក់ផ្សេងៗទៀត។ ហើយដើម្បីបង្កើតថ្នាក់មួយបន្តភ្ជាប់ទៅនឹងថ្នាក់មួយទៀត យើងត្រូវធ្វើដូចខាងក្រោមនេះ៖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | class Area(): pi = 3.14 def __init__( self ): print ( 'The constructor has been called.' ) def surface( self ): print ( 'surface' ) class Rectangle(Area): def surface( self , width, height): area = width * height print ( 'The area of rectangle is' , area) |
នៅលើបន្ទាត់លេខ 11 ការសរសេរថា class Rectangle(Area): គឺជាការបង្កើតថ្នាក់ឈ្មោះ Rectangle មួយដោយបន្តភ្ជាប់ទៅនឹងថ្នាក់ឈ្មោះ Area ។ ក្នុងករណីនេះ ថ្នាក់ឈ្មោះ Area ត្រូវចាត់ទុកថាជា superclass ចំណែកឯថ្នាក់ឈ្មោះ Rectangle ត្រូវចាត់ទុកថាជា subclass ។
យ៉ាងណាម៉ិញ ផលប្រយោជន៍នៃ inheritance គឺថាតាមរយៈ subclass យើងអាចយក attribute នៅក្នុង superclass មកប្រើប្រាស់បានតាមសេចក្តីត្រូវការ។ ពិនិត្យកម្មវិធីខាងក្រោមនេះ៖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | class Area(): pi = 3.14 def __init__( self ): print ( 'The constructor has been called.' ) def surface( self ): print ( 'surface' ) class Rectangle(Area): def rec_surface( self , width = 0 , height = 0 ): area = width * height print ( 'The area of rectangle is' , area) rectangle = Rectangle() print ( 'The value of pi is' , rectangle.pi) rectangle.rec_surface() rectangle.surface() |
1 2 3 4 | The constructor has been called. The value of pi is 3.14 The area of rectangle is 0 surface |
អាស្រ័យហេតុនេះ នៅពេលដែល attribute ណាមួយ ត្រូវបានយកមកប្រើតាមរយៈ instance ណាមួយ ការស្វែងរកវត្ថុនោះ ត្រូវធ្វើឡើងនៅក្នុង instance នោះមុន រួចបានឡើងទៅថ្នាក់របស់វាផ្ទាល់ រួចបានឡើងទៅ superclass របស់វា បើសិនជាមាន។ តែបើ attribute ណាមួយត្រូវបានយកមកប្រើតាមរយៈថ្នាក់ណាមួយវិញ ការស្វែងរកវត្ថុនោះត្រូវធ្វើឡើងនៅក្នុងថ្នាក់នោះមុន រួចបានឡើងទៅ superclass បើសិនជាមាន។
នៅក្នុង inheritance យើងអាចយកថ្នាក់ចំនួនប៉ុន្មានក៏បានដែរមកបន្តគ្នា។ ពោលគឺយើងអាចបង្កើតថ្នាក់មួយបន្តភ្ជាប់ទៅនឹងថ្នាក់មួយទៀត ដែលត្រូវបានបន្តភ្ជាប់ទៅនឹងថ្នាក់មួយទៀត ដែលត្រូវបានបន្តភ្ជាប់ទៅនឹងថ្នាក់មួយទៀត...
ក្នុងករណីមានថ្នាក់ជាច្រើនបន្តភ្ជាប់គ្នាពីមួយទៅមួយ នៅពេលដែល attribute ណាមួយត្រូវបានយកមកប្រើ ការស្វែងរក attribute នោះ ត្រូវធ្វើឡើងជាដំបូងនៅក្នុង instance ឬថ្នាក់ដែលតាមរយៈវា attribute នោះត្រូវយកមកប្រើ រួចបានឡើងទៅថ្នាក់នៅខាងលើវា ជាបន្តបន្ទាប់។ ពិនិត្យកម្មវិធីខាងក្រោមនេះ៖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | class Area(): pi = 3.14 def __init__( self ): print ( 'The constructor has been called.' ) def surface( self ): print ( 'surface' ) class Rectangle(Area): def rec_surface( self , width = 0 , height = 0 ): area = width * height print ( 'The area of rectangle is' , area) class Triangle(Rectangle): def tri_surface( self , edge, height): surface = edge * height / 2 print ( 'The area of triangle is' , surface) triangle = Triangle() triangle.tri_surface( 25 , 5 ) triangle.rec_surface( 25 , 5 ) print ( 'The value of pi is' , triangle.pi) |
1 2 3 4 | The constructor has been called. The area of triangle is 62.5 The area of rectangle is 125 The value of pi is 3.14 |
សរុបមក យើងឃើញថា នៅពេលដែល attribute ណាមួយត្រូវយកមកប្រើតាមរយៈ instance ឬថ្នាក់ណាមួយ ការស្វែងរកវត្ថុពីរប្រភេទត្រូវធ្វើឡើងក្នុងពេលជាមួយគ្នា។ ជាដំបូង ការស្វែងរកវត្ថុដែលជា instance ឬថ្នាក់ដែលតាមរយៈវា attribute នោះត្រូវបានយកមកប្រើ។ ហើយការស្វែងរកវត្ថុដែលជា instance ឬថ្នាក់នោះ ត្រូវធ្វើឡើងដោយអនុលោមទៅតាមវិធាននៃការស្វែងរកវត្ថុនៅក្នុង scope ផ្សេងៗ។ បន្ទាប់មកទៀត គឺការស្វែងរកវត្ថុដែល attribute ត្រូវយកមកប្រើ។ ហើយការស្វែងរក attribute នោះ ត្រូវធ្វើឡើងដោយអនុលោមទៅតាមវិធាននៃការស្វែងរក attribute នៅក្នុង instance ឬនៅក្នុងថ្នាក់ផ្សេងៗ៕