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
1#Source: https://vegibit.com/python-class-examples/
2class Vehicle:
3 def __init__(self, brand, model, type):
4 self.brand = brand
5 self.model = model
6 self.type = type
7 self.gas_tank_size = 14
8 self.fuel_level = 0
9
10 def fuel_up(self):
11 self.fuel_level = self.gas_tank_size
12 print('Gas tank is now full.')
13
14 def drive(self):
15 print(f'The {self.model} is now driving.')
16
17obj = Vehicle("Toyota", "Carola", "Car")
18obj.drive()