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
2class Box(object): #(object) ending not required
3 def __init__(self, color, width, height): # Constructor: These parameters will be used upon class calling(Except self)
4 self.color = color # self refers to global variables that can only be used throughout the class
5 self.width = width
6 self.height = height
7 self.area = width * height
8 def writeAboutBox(self): # self is almost always required for a function in a class, unless you don't want to use any of the global class variables
9 print(f"I'm a box with the area of {self.area}, and a color of: {self.color}!")
10
11greenSquare = Box("green", 10, 10) #Creates new square
12greenSquare.writeAboutBox() # Calls writeAboutBox function of greenSquare object
1# If questions ask SK3#3160 he can help you i think
2def ok(index):
3 print(index) > Hi!
4ok("Hi!")
5class Lib:
6 def ok(self,Token):
7 print(Token) > Hello World!
8Library = Lib()
9Library.ok("Hello World!")