Files
LearnPython/LearnTheBasics/10_ClassesAndObjects.py
2024-09-13 15:12:25 +02:00

65 lines
1.8 KiB
Python

#A very basic class would look something like this:
class MyClass:
variable = "blah"
def function(self):
print("This is a message inside the class.")
#We'll explain why you have to include that "self" as a parameter a little bit later. First, to assign the above class(template) to an object you would do the following:
class MyClass2:
variable = "blah"
def function(self):
print("This is a message inside the class.")
myobjectx = MyClass2()
#To access a function inside of an object you use notation similar to accessing a variable:
myobjectx.function()
#To access the variable inside of the newly created object "myobjectx" you would do the following:
x = myobjectx.variable
print(x)
#The __init__() function, is a special function that is called when the class is being initiated. It's used for assigning values in a class.
class NumberHolder:
def __init__(self, number):
self.number = number
def returnNumber(self):
return self.number
var = NumberHolder(7)
print(var.returnNumber()) #Prints '7'
# Exercise
# We have a class defined for vehicles. Create two new vehicles called car1 and car2. Set car1 to be a red convertible worth $60,000.00 with a name of Fer, and car2 to be a blue van named Jump worth $10,000.00.
# define the Vehicle class
class Vehicle:
name = ""
kind = "car"
color = ""
value = 100.00
def description(self):
desc_str = "%s is a %s %s worth $%.2f." % (self.name, self.color, self.kind, self.value)
return desc_str
# your code goes here
car1 = Vehicle()
car1.name = "Fer"
car1.value = 60000.00
car1.kind = "convertible"
car1.color = "red"
car2 = Vehicle()
car2.name = "Jump"
car2.kind = "van"
car2.color = "blue"
car2.value = 10000.00
# test code
print(car1.description())
print(car2.description())