Composition is a concept that models a has a relationship. It enables creating complex types by combining objects of other types. This means that a class Composite can contain an object of another class Component. This relationship means that a Composite has a Component.
Example:
class Garage:
def __init__(self, *cars):
self.cars = cars
def __str__(self):
return (f"Total {len(self.cars)} cars in the Garage")
class Cars:
def __init__(self, name, year):
self.name = name
self.year = year
def __str__(self):
return ("{} is moving to Garage in {}".format(self.name, self.year))
car = Cars("Ferrari", "2019")
car2 = Cars("Rolls", "2020")
car3 = Cars("Benz", "2021")
garage = Garage(car, car2, car3)
print(car)
print(car2)
print(car3)
print(garage)
Output:
Ferrari is moving to Garage in 2019Rolls is moving to Garage in 2020
Benz is moving to Garage in 2021
Total 3 cars in the Garage
Awesome blog with lots of information. It is really helpful for all.
ReplyDeleteAngularJS Training in Chennai
AngularJS course in Chennai
Angular 6 Training in Chennai
Angular Training in Chennai
React JS Training in Chennai
PHP Training in Chennai
Web Designing course in Chennai
Tally course in Chennai
Salesforce Training in Chennai
AngularJS Training center in Chennai
Angular 7 Training in Chennai
AngularJS Interview Questions
excellent information about this page contin lot of information . i could help me more thanks you so much.
ReplyDeleteC and C++ Training Institute in chennai | C and C++ Training Institute in anna nagar | C and C++ Training Institute in omr | C and C++ Training Institute in porur | C and C++ Training Institute in tambaram | C and C++ Training Institute in velachery
Thank you for sharing this wonderful blog with us it is an awesome website to learn everything.
ReplyDeleteAngularJS training in chennai | AngularJS training in anna nagar | AngularJS training in omr | AngularJS training in porur | AngularJS training in tambaram | AngularJS training in velachery