1class Dog:
2
3 def bark(self):
4 print("Woof!")
5
6 def roll(self):
7 print("*rolling*")
8
9 def greet(self):
10 print("Greetings, master")
11
12 def speak(self):
13 print("I cannot!")
14
15# Creating the Dog class instance and saving it to the variable <clyde>
16clyde = Dog()
17clyde.bark() # --> Woof!
18clyde.roll() # --> *rolling*
19clyde.greet() # --> Greetings, master
20clyde.speak() # --> I cannot!
21
22# Creating another Dog instance
23jenkins = Dog()
24jenkins.bark() # --> Woof!
25jenkins.roll() # --> *rolling*
26# .. And other methods
27# .. Infinite objects can be created this way, all implementing the same methods defined in our class
28
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()
1#NOTES
2class PartyAnimal:
3 def Party():
4 #blahblah
5
6an=PartyAnimal()
7
8an.Party()# this is same as 'PartyAnimal.Party(an)'