Class attributes and object attributes


"" "

Achieve record number of objects to create functional

 

" ""

 

 

class Tool (Object):

    DEF __init __ (Self, new_name):

        self.name = new_name

 

 

# under this method stupid friends

NUM = 0

TOOL1 = Tool ( "shovel" )

NUM + =. 1

Print (NUM)

TOOL2 = Tool ( "shovel")

NUM + =. 1

Print (NUM)

tool3 = Tool ( "bucket")

NUM + =. 1

Print (NUM)

 

"" "

. 1

2

. 3

" " "

 

 

class TOOL2 (Object):

    " ""

    examples of attributes of a class of all, a total objects

 

    "" "

    # class attribute

    NUM = 0

 

    # methods

    def __init __ (self, new_name) :

        Examples # property

        self.name = new_name

        Class attributes # =. 1 +

        Tool2.num + =. 1

 

 

# This is better

tool1 = Tool2 ( "shovel")

TOOL2 TOOL2 = ( "shovel")

tool3 TOOL2 = ( "bucket")

 

Print (Tool2.num)

"" "

3

" ""

"" "

achieve record number of functions to create an object

 

." ""

 

 

class Tool (Object):

    DEF __init __ (Self, new_name):

        self.name = new_name

 

 

# under this method stupid friends

NUM = 0

TOOL1 = Tool ( "shovel")

NUM + =. 1

Print (NUM)

TOOL2 = Tool ( "shovel")

NUM + =. 1

Print (NUM)

tool3 = Tool ( "bucket")

NUM + =. 1

Print (NUM)

 

"" "

1

2

3

"""

 

 

class Tool2(object):

    "" "

    Examples of attributes of a class of all, a total objects

 

    " ""

    # class attribute

    NUM = 0

 

    # methods

    DEF the __init __ (Self, NEW_NAME):

        # instance attribute

        the self.name = NEW_NAME

        # Class attributes. 1 = +

        Tool2.num + = 1

 

 

# this is better

tool1 = Tool2 ( "shovel")

TOOL2 = TOOL2 ( "shovel")

tool3 = TOOL2 ( "bucket")

 

Print (Tool2.num)

"" "

3

" ""
Published 457 original articles · won praise 2 · Views 100,000 +

Guess you like

Origin blog.csdn.net/u010395024/article/details/103952792