13. python init

class Calculator:
    name = 'python test'
    age = 18
    def __init__(self, name, price, height, width, weight):
        self.name = name
        self.price= price
        self.h = height
        self.w = width
        self.weight = weight

        self.add(1,2) // 3 实例化类时,自动执行
        
    def add(self, x, y):
        return x + y
    
    def minus(self, x, y):
        return x - y

    def times(self, x, y):
        return x * y

    def divide(self, x, y):
        return x / y

1. 调用

c = Calculator('python init', 18, 25, 30, 50)

c.name
c.price
c.w
...
posted @ 2017-03-18 00:28  涵叔  阅读(118)  评论(0)    收藏  举报