2017年8月12日 星期六

[python] class example

Example 1

class fruit():
    color = 'red'
    def taste(self):
        return 'delicious'

apple = fruit()      
apple.color
apple.taste()   

Example 2

class staff():
    def salary(self):
        return '10000元'

yamamoto = staff()
yamamoto.salary()

Example 3

class staff():
    bonus = 30000
    def salary(self):
        salary = 10000 + self.bonus
        return salary

yamamoto = staff()
yamamoto.salary()

Example 4

class staff():
    def __init__(self, bonus):
        self.bonus = bonus
    def salary(self):
        salary =  10000 + self.bonus
        return salary

yamamoto = staff()
yamamoto.salary()