Class III Method

# Class can be defined in three ways 
# 1, examples of method of
the first method is a parameter # Self
# 2, class method
first method is a parameter # CLS (showing the current class), it is necessary hat @classmethod
#. 3, static method
# method has no parameters, with the hat @staticmethod


class the person (Object):
Country = 'China'
__skin_color = 'yellow'

DEF __init __ (Self, name, Age):
self.name name =
Self .__ Age = Age

# definition of instance method, may be modified and acquiring data private instance attributes

def set_age (self, new_age): # examples may replace the class methods, using Self .__ class__ is
IF 0 <Age <150:
Self .__ Age = new_age
the else:
Print ( 'Please enter the legal age!')

DEF get_age (Self):
return Self .__ Age


# define class methods can be modified and access to private class attributes, general and related operations use the current class
@classmethod
DEF set__skin_color (CLS, new_color is):
Print (CLS)
CLS .__ skin_color = new_color is

@classmethod
DEF get_skin_color (CLS):
return CLS skin_color .__

# define a static method, when the method does not require the current class (CLS) and the current object (Self)
@staticmethod
DEF SUM (num1, num2):
return num1 num2 +

# class methods may be rewritten as an example of such a method, the above method is modified to such class
DEF set__skin_color (Self, new_color is):
Self .__ class = __.__ skin_color new_color

DEF get_skin_color (Self):
return Self .__ class __.__ skin_color

Guess you like

Origin www.cnblogs.com/wjun0/p/11515392.html