Discuss / Python / python是第一语言~小白想学习Java了~

python是第一语言~小白想学习Java了~

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

class Screen:
    def __init__(self):
        self.__width = None
        self.__height = None

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

    @width.setter
    def width(self,value):
        if not isinstance(value,int):
            raise ValueError("NO!!!! This is Int!")
        self.__width = value

    @width.deleter
    def width(self):
        del self.__width

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

    @height.setter
    def height(self,value):
        if not isinstance(value,int):
            raise ValueError("NO!!!! This is Int!")
        self.__height = value

    @height.deleter
    def height(self):
        del self.__height

    @property
    def resolution(self):
        return "assert s.resolution == 786432,'{} * {} = {}'".format(self.__width,self.__height,self.__width * self.__height)
>>> s = Screen()
>>> s.width = 1024
>>> s.height = 768
>>> s.resolution
"assert s.resolution == 786432,'1024 * 768 = 786432'"
>>>

得先了解property是干啥的~


  • 1

Reply