“Warisan Python” Kode Jawaban

Warisan dalam Contoh Python 3

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

Kode Python untuk menunjukkan warisan

# 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

Warisan dalam Contoh Python 3

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

Warisan Python

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

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

Warisan Python

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

Warisan dalam Python

from Chef import Chef

class ChineseChef(Chef):

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

Jawaban yang mirip dengan “Warisan Python”

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya