current position:Home>I met it during self-study and couldn't get it out.

I met it during self-study and couldn't get it out.

2022-01-27 04:33:18 CSDN Q & A

Color Class encapsulation uses RGB The color model represents the color and corresponding functions :
Define the band 3 Integer parameters r,g,b Constructor for , Used to initialize the corresponding red 、 green 、 Private instance object properties of the three color components of blue __r, __g, __b.
Through ornaments @property Definition 3 An instance object method that can be accessed as a read-only property r()、g()、c()
Define without parameters 、 Brightness calculation example method with return value luminance: g=0.299r+0.587g+0.114b, return g
Define without parameters 、 An example method of color to gray conversion with return value toGray: call luminance Method to obtain brightness g, Yes g Round to y, Return a tuple (y,y,y)
Instantiation Color class , Use the read-only attribute to output three channel values , Calling method toGray、luminance.


Defining abstract classes shape, Contains abstract methods area Calculation of area . Redefine the extension 2 A subclass Circle、Rectangle, The constructor passes in the basic parameters , rewrite area Method ( Return value without parameters , Return area ).
Define class cylinder Barrel, Constructor has two arguments ( Gao He Shape object ), Method volume( No parameters, no return value ) Calculate the volume of the cylinder and output various information of the cylinder ,


Refer to the answer 1:
class Color:    def __init__(self,r,g,b):        self.__r=r        self.__g=g        self.__b=b @property    def r(self):        return self.__r @property    def g(self):        return self.__g @property    def b(self):        return self.__b    def lum(self):        g=0.299*self.__r+0.587*self.__g+0.114*self.__b        return g    def toGray(self):        g=self.lum()        return (int(g),int(g),int(g))r=255g=152b=0c=Color(r,g,b)print(f'r={c.r},g={c.g},b={c.b}')print(c.lum())print(c.toGray())

Refer to the answer 2:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.

Random recommended