用纯属性取代get和set方法


class VoltageResistance(Resistor):

    def __init__(self, ohms):

        super().__init__(ohms)

        self._voltage = 0

    @property

    def voltage(self):

        return self._voltage

    @voltage.setter

    def voltage(self, voltage):

        if voltage <= 0:

            raise ValueError('%f voltage must be >0' % voltage)

        self._voltage = voltage

你可能感兴趣的:(用纯属性取代get和set方法)