Showing posts with label Class. Show all posts
Showing posts with label Class. Show all posts

Car Class Program

# define the Vehicle class
class Vehicle:
    name = ""              # initial value setting inisde the clas iteself
    kind = "car"           # initial value setting inisde the clas iteself
    color = ""             # initial value setting inisde the clas iteself
    value = 100.00         # initial value setting inisde the clas iteself
    def description(self): # creating function inside the class amd return as string
        desc_str = "%s is a %s %s worth $%.2f." % (self.name, self.color, self.kind, self.value)
        return desc_str
     
     
car= Vehicle()             # creates a object [car1] for class vehicle
print car.description()    # using created object calling the function inside class


car1 = Vehicle()           # creates a object car for class vehicle
car1.name = "Fer"          # using object[car1] variables on the class vehicle is assigned
car1.color = "red"         # using object[car1] variables on the class vehicle is assigned
car1.kind = "convertible"  # using object[car1] variables on the class vehicle is assigned
car1.value = 60000.00      # using object[car1] variables on the class vehicle is assigned

car2 = Vehicle()
car2.name = "Jump"
car2.color = "blue"
car2.kind = "van"
car2.value = 10000.00

print car.description()    # Using object [car] created calling the functions[description()]inside the class
print car1.description()   # Using object [car1] created calling the functions[description()]inside the class
print car2.description()   # Using object [car2] created calling the functions[description()]inside the class

Simple Class Program in Python

class MyClass:          # class is created
    variable = "blah"   # variable is created and assigned the value

    def function(self): # funtion is created iniside the class as self
        print "This is a message inside the class."
       
       
       
myobjectx = MyClass()     # object[myobjectx] is created for class
myobjectx.variable        # using created object variable in the class is accessed

myobjecty = MyClass()          # object[myobjecty] is created for class
myobjecty.variable = "yackity" # using created object variable in the class new value is assigned

print myobjectx.variable       # using corresponding object and variable the value is displayed
print myobjecty.variable       # using corresponding object and variable the value is displayed
print MyClass.variable         # using corresponding object and variable the value is displayed

myobjectx.function()           # using corresponding object and function inside the class is accessed