python初学class类、属性和方法后的小练习

1.classl知识点汇总练习

class Person:
    eye = 2
    finger = 10

    def __init__(self, name):
        self.name = name

    def eat(self, food):
        print('%s---正在吃%s---'%(self.name, food))


xiaoMing = Person('小明')
xiaoMing.age = 23
print(xiaoMing.age)
print(xiaoMing.eye)
xiaoMing.eat('水果')

2.定义一个矩形类,并且有计算面积方法。

class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width


rec1 = Rectangle(10, 5)
ar1 = rec1.area()
print(ar1);

你可能感兴趣的:(python)