1class Mammal:
2 def __init__(self, name):
3 self.name = name
4
5 def walk(self):
6 print(self.name + " is going for a walk")
7
8
9class Dog(Mammal):
10 def bark(self):
11 print("bark!")
12
13
14class Cat(Mammal):
15 def meow(self):
16 print("meow!")
17
18
19dog1 = Dog("Spot")
20dog1.walk()
21dog1.bark()
22cat1 = Cat("Juniper")
23cat1.walk()
24cat1.meow()
1class Person:
2 def __init__(self, _name, _age):
3 self.name = _name
4 self.age = _age
5
6 def sayHi(self):
7 print('Hello, my name is ' + self.name + ' and I am ' + self.age + ' years old!')
8
9p1 = Person('Bob', 25)
10p1.sayHi() # Prints: Hello, my name is Bob and I am 25 years old!
1# To create a simple class:
2class Shape:
3 def __init__():
4 print("A new shape has been created!")
5 pass
6
7 def get_area(self):
8 pass
9
10# To create a class that uses inheritance and polymorphism
11# from another class:
12class Rectangle(Shape):
13
14 def __init__(self, height, width): # The constructor
15 super.__init__()
16 self.height = height
17 self.width = width
18
19 def get_area(self):
20 return self.height * self.width
21
1class uneclasse():
2 def __init__(self):
3 pass
4 def something(self):
5 pass
6xx = uneclasse()
7xx.something()
1class Person:#set name of class to call it
2 def __init__(self, name, age):#func set ver
3 self.name = name#set name
4 self.age = age#set age
5
6
7 def myfunc(self):#func inside of class
8 print("Hello my name is " + self.name)# code that the func dose
9
10p1 = Person("barry", 50)# setting a ver fo rthe class
11p1.myfunc() #call the func and whitch ver you want it to be with
1class Fan:
2
3 def __init__(self, company, color, number_of_wings):
4 self.company = company
5 self.color = color
6 self.number_of_wings = number_of_wings
7
8 def PrintDetails(self):
9 print('This is the brand of',self.company,'its color is', self.color,' and it has',self.number_of_wings,'petals')
10
11
12 def switch_on(self):
13 print("fan started")
14
15 def switch_off(self):
16 print("fan stopped")
17
18 def speed_up(self):
19 print("speed increased by 1 unit")
20
21 def speed_down(self):
22 print("speed decreased by 1 unit")
23
24usha_fan = Fan('usha','skin',5)
25fan = Fan('bajaj','wite', 4)
26
27
28print('these are the details of this fan')
29usha_fan.PrintDetails()
30print()
31
32usha_fan.switch_on()