-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.py
57 lines (44 loc) · 1.33 KB
/
class.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class Dog2:
# Class attribute
species = "Canis familiaris"
def __init__(self, name, age): # Instance attribute
self.name = name
self.age = age
def description(self):
return f"{self.name} is {self.age} years old"
# Another instance method
def speak(self, sound):
return f"{self.name} says {sound}"
def __str__(self):
return f"{self.name} is {self.age} years old"
class Dog2:
species = "Canis familiaris"
def __init__(self, name, age, breed):
self.name = name
self.age = age
self.breed = breed
miles = Dog2("Miles", 4, "Jack Russell Terrier")
buddy = Dog2("Buddy", 9, "Dachshund")
jack = Dog2("Jack", 3, "Bulldog")
jim = Dog2("Jim", 5, "Bulldog")
print(miles.name, buddy.name, jack.name, jim.name)
# print(miles.description())
class Dog:
species = "Canis familiaris"
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return f"{self.name} is {self.age} years old"
def speak(self, sound):
return f"{self.name} says {sound}"
class JackRussellTerrier(Dog):
def speak(self, sound="Arf"):
return f"{self.name} says {sound}"
class Dachshund(Dog):
pass
class Bulldog(Dog):
pass
miles1 = JackRussellTerrier("Miles1", 4)
print(miles1)
print(miles1.speak())