“Warisan dalam 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

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 dalam Python

class Polygon:
    def __init__(self, no_of_sides):
        self.n = no_of_sides
        self.sides = [0 for i in range(no_of_sides)]

    def inputSides(self):
        self.sides = [float(input("Enter side "+str(i+1)+" : ")) for i in range(self.n)]

    def dispSides(self):
        for i in range(self.n):
            print("Side",i+1,"is",self.sides[i])
SAMER SAEID

Warisan dalam Python

from Chef import Chef

class ChineseChef(Chef):

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

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 dalam Contoh Python 3

<__main__.Robot object at 0x7fd0080b3ba8> <class '__main__.Robot'>
<__main__.PhysicianRobot object at 0x7fd0080b3b70> <class '__main__.PhysicianRobot'>
Hi, I am James
Foolish Finch

Jawaban yang mirip dengan “Warisan dalam Python”

Pertanyaan yang mirip dengan “Warisan dalam Python”

Lebih banyak jawaban terkait untuk “Warisan dalam Python” di Python

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya