13. python 类

代码

class Calculator:
    name = 'python test'
    age = 18
    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. 定义类,首字母大写

2. self 指向当前类

self.name // python test
self.age // 18

3. 调用类和类的方法

cal = Calculator() // 实例化类

cal.name // python test
cal.age // 18

cal.add(10,2) // 12

......

  

posted @ 2017-03-18 00:17  涵叔  阅读(116)  评论(0)    收藏  举报