“Dziedziczenie Pythona” Kod odpowiedzi

Dziedzictwo w Python 3 Przykład

class Robot:
    
    def __init__(self, name):
        self.name = name
        
    def say_hi(self):
        print("Hi, I am " + self.name)
        
class PhysicianRobot(Robot):

    def say_hi(self):
        print("Everything will be okay! ") 
        print(self.name + " takes care of you!")

y = PhysicianRobot("James")
y.say_hi()
Foolish Finch

kod Pythona w celu wykazania dziedziczenia

# Python code to demonstrate how parent constructors
# are called.
  
# parent class
class Person( object ):    
  
        # __init__ is known as the constructor         
        def __init__(self, name, idnumber):   
                self.name = name
                self.idnumber = idnumber
        def display(self):
                print(self.name)
                print(self.idnumber)
  
# child class
class Employee( Person ):           
        def __init__(self, name, idnumber, salary, post):
                self.salary = salary
                self.post = post
  
                # invoking the __init__ of the parent class 
                Person.__init__(self, name, idnumber) 
  
                  
# creation of an object variable or an instance
a = Employee('Rahul', 886012, 200000, "Intern")    
  
# calling a function of the class Person using its instance
a.display()
mathiasgodwin

Dziedzictwo w Python 3 Przykład

class Robot:
    
    def __init__(self, name):
        self.name = name
        
    def say_hi(self):
        print("Hi, I am " + self.name)
        
class PhysicianRobot(Robot):
    pass

x = Robot("Marvin")
y = PhysicianRobot("James")

print(x, type(x))
print(y, type(y))

y.say_hi()
Foolish Finch

Dziedziczenie Pythona

class Student(Person):
  def __init__(self, fname, lname, year):
    super().__init__(fname, lname)
    self.graduationyear = year
 

x = Student("Michael", "Smith", 2020)
Javasper

Dziedziczenie Pythona

class BaseClass:
  Body of base class
class DerivedClass(BaseClass):
  Body of derived class
SAMER SAEID

Dziedzictwo w Pythonie

from Chef import Chef

class ChineseChef(Chef):

	def make_salad():
		print("Chinese chef makes a salad")
Witty Wombat

Odpowiedzi podobne do “Dziedziczenie Pythona”

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu