コード例 #1
0
from shirt import Shirt

shirt_one = Shirt('red', 'L', 'long-sleeve', 25)
print(shirt_one.price)
shirt_one.price = 10
print(shirt_one.price)
shirt_one.price = 20
print(shirt_one.price)
shirt_one.color = 'yellow'
print(shirt_one.color)
shirt_one.size = 'M'
print(shirt_one.size)
shirt_one.style = 'short_sleeve'
print(shirt_one.style)
コード例 #2
0
print(shirt_one.color)
print(shirt_one.price)


shirt_two.change_price(45) # price change using method
print(shirt_two.price)


# How to access and change attribute values in python class

#  Shirt class has method to change shirt price
# Values can be changed either by assigning value or by  using method


shirt_one.color='Green'
print(shirt_one.color)


shirt_one.size='L'
print(shirt_one.size)


shirt_one.price=43
print(shirt_one.price)


# * There are some drawbacks to accessing attributes directly versus writing a method for accessing and displaying attributes.
# * Programming languages like C++/C  you can explicitely state wheather or not an object should be allowed to change or access attribute      values directly.
# * Changing value via method gives more flexibility in long term
from shirt import Shirt
from shirt import Jean

shirt_one = Shirt('orange', 'S', 'long sleeve', 45)
shirt_two = Shirt('red', 'XL', 'short sleeve', 30)

print(shirt_one.color)
print(shirt_two.price)

shirt_two.change_price(23)
print(shirt_two.price)

print(shirt_one.discount(.1))

shirt_one.color = 'purple'
shirt_one.size = 'L'
shirt_one.price = 38

jean_one = Jean('red', 'L', 70)
print(jean_one.color)
print(jean_one.size)
print(jean_one.price)