Created
August 17, 2016 18:53
-
-
Save asduser/53712dd4c3615f3198c673422f92c372 to your computer and use it in GitHub Desktop.
A simple code, which explains the process of entity status changing by using of private and public methods.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
class Fruit: | |
def __init__(self): | |
self.__state__ = False | |
def isEaten(self): | |
return self.__state__ | |
def consume(self): | |
self.__state__ = True | |
class Person: | |
counter = 0 | |
def eat(self, fruit): | |
if not fruit.isEaten(): | |
fruit.consume() | |
self.counter += 1 | |
f1 = Fruit() | |
f2 = Fruit() | |
p1 = Person() | |
p1.eat(f1) | |
p1.eat(f1) | |
p1.eat(f2) | |
print(p1.counter) # should be 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment