what is an object in python
class Person:#set name of class to call it
def __init__(self, name, age):#func set ver
self.name = name#set name
self.age = age#set age
def myfunc(self):#func inside of class
print("Hello my name is " + self.name)# code that the func dose
p1 = Person("barry", 50)# setting a ver fo rthe class
p1.myfunc() #call the func and whitch ver you want it to be with
# 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 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 ClassName:
self.attribute_1 = variable_1 #Set attributes for all object instances
self.attrubute_2 = variable_2
def __init__(self, attribute_3, attribute_4): #Set attributes at object creation
self.attribute_3 = attribute_3
self.attribute_4 = attribute_4
def method(self): #All methods should include self
print("This is a method example.") #Define methods just like functions
object = Object(4, "string") #Set attribute_3 and attribute_4
object.method() #Methods are called like this.