declare class python
# To create a simple class:
class Shape:
def __init__():
print("A new shape has been created!")
pass
def get_area(self):
pass
# To create a class that uses inheritance and polymorphism
# from another class:
class Rectangle(Shape):
def __init__(self, height, width): # The constructor
super.__init__()
self.height = height
self.width = width
def get_area(self):
return self.height * self.width
class ClassName(object): #"(object)" isn't mandatory unless this class inherit from another
def __init__(self, var1=0, var2):
#the name of the construct must be "__init__" or it won't work
#the arguments "self" is mandatory but you can add more if you want
self.age = var1
self.name = var2
#the construct will be execute when you declare an instance of this class
def otherFunction(self):
#the other one work like any basic fonction but in every methods,
#the first argument (here "self") return to the class in which you are
class car():
gas=100
def __init__(self, mpg, position, gast):
gas=gast
self.mpg=mpg
self.position=position
def move(self, miles):
if (self.mpg*gas>miles):
print("You don't have enough gas")
elif(self.mpg*gas<=miles):
self.position+=miles
gas-=(1/self.mpg)*miles
def get_gas(self):
if(self.position %50==0):
gas=100
else:
print("You can't do that")
class Truck(car):
def __init__(self, carry_capacity):
self.carry_capactiy=carrycapacity
def carry(self, load):
if(load<=carry_capacity):
print("Successfully carried")
else:
print("Failed to carry")
Car=car(5, 0, 100)
Car.move(50)
Car.get_gas()
truck=Truck(5)
truck.get_gas()
truck.carry()
class a_class:
#This initalizes the object, and is executed when you define
#a new object in the class
def __init__(self, input1):
self.__input1 = input1
#This is a function of the object that can be called
def return_input(self):
return self.__input1
a_class_object = a_class("input string")
print(a_class_object.return_input())