This code defines a Python class called “Item”. It contains a class variable ‘counter’ that increments by 1 each time an instance of the class is created. The class has 4 methods:
setName
: which sets the name of the instancesetDescription
: which sets the description of the instancesetColor
: which sets the color of the instanceprint
: which prints the name, description, color, and counter of the instance.The code creates two instances of the class, i1
and i2
, and sets their name, description, and color properties using the setName
, setDescription
, and setColor
methods. Finally, it prints the details of the instances using the print method.
class Item():
counter = 1000
def __init__(self):
Item.counter += 1
def setName(self, n):
self.name = n
def setDescription(self, desc):
self.description = desc
def setColor(self, c):
self.color = c
def print(self):
print("Item name: " + self.name)
print("Item description: " + self.description)
print("Color: " + self.color)
print("ItemNr: " + str(self.counter) + "\n")
i1 = Item()
i1.setName("Shirt")
i1.setDescription("Adidas performance essentials big logo")
i1.setColor("Black")
i1.print()
i2 = Item()
i2.setName("Jeans")
i2.setDescription("Trendyol Collection")
i2.setColor("Blue")
i2.print()
Item name: Shirt
Item description: Adidas performance essentials big logo
Color: Black
ItemNr: 1001
Item name: Jeans
Item description: Trendyol Collection
Color: Blue
ItemNr: 1002