Discuss / Python / 交作业了,有错误,请指点。

交作业了,有错误,请指点。

Topic source
# -*- coding: utf-8 -*-

#@property装饰器

class Screen(object):

    @property
    def width(self):
        return self.w

    @property
    def height(self):
        return self.h 

    @width.setter
    def width(self,w):
        if not isinstance(w,int):
            raise ValueError('width must be an integer!')
        self.w = w

    @height.setter
    def height(self,h):
        if not isinstance(h,int):
            raise ValueError('height must be an integer!')
        self.h = h

    @property
    def resolution(self):
        return self.w * self.h

sc = Screen()

print(sc)

sc.width = 1024

sc.height = 768

print('1024*768 = %d ' % sc.resolution)

  • 1

Reply