“Python -Erbschaft” Code-Antworten

Vererbung in Python 3 Beispiel

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

Python -Code, um Vererbung zu demonstrieren

# 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

Vererbung in Python 3 Beispiel

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

Python -Erbschaft

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

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

Python -Erbschaft

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

Vererbung in Python

from Chef import Chef

class ChineseChef(Chef):

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

Ähnliche Antworten wie “Python -Erbschaft”

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen