Advertisement
rezamalik15

contoh class pada python

Jun 5th, 2023
787
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.63 KB | None | 0 0
  1. class Animal:
  2.     name = None
  3.     color = None
  4.  
  5.     def __init__(self, name, color):
  6.         self.name = name
  7.         self.color = color
  8.  
  9.     def makeSound(self, sound):
  10.         return f"{self.name} is {sound}"
  11.  
  12.     def eat(self, food):
  13.         return f"{self.name} is eating {food}"
  14.  
  15.  
  16. dog = Animal("Dog", "Black")
  17. cat = Animal("Cat", "Yellow")
  18. bird = Animal("Bird", "White")
  19.  
  20. print(dog.name)
  21. print(dog.color)
  22. print(dog.makeSound("Barking"))
  23. print(dog.eat("meat"))
  24.  
  25. print(cat.name)
  26. print(cat.color)
  27. print(cat.makeSound("Meowing"))
  28. print(cat.eat("fish"))
  29.  
  30. print(bird.name)
  31. print(bird.color)
  32. print(bird.makeSound("Chirping"))
  33. print(bird.eat("fruit"))
  34.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement