python - Object Oriented Programming (properties, methods)

First, property

Property (attribute) also known as data members of the object (data member).

If you want to point to an object's properties, you can use the format:

  object.attribute

Property is divided into: private property and public property.

Private property is a two begins with an underscore (__), private members can not directly access outside the class.

Python provides a special way to access private members:

  __ ._ object name private property name class name (object name of the object class instance)

Public properties can be accessed either from within the class may be used in the external program.

There is a special class Python built-in property, such as: __ doc __, __ module __, __ base__.

 

Second, the method

A method is a function defined in the class.

Class methods and normal function is only one difference:

They must have an extra on the front of the parameters are usually named it self.

But self is not a Python keyword, if it can be replaced by other identifiers are also normally perform.

Built-in methods of the class: the beginning and the end are two underscores.

__init __ (self): the class constructor when creating an instance of a class will automatically call the method.

__del __ (self): class destructor, deleting objects, automatically.

__str __ (self): used to set how the objects displayed in occurs when the string type.

 

--------------------------- # 
# Properties and Methods
# ----------------- ----------
# define
class ProjectGroup:
"" "this is a class project team" ""
GROUP_NUMBER = 0 # attribute class (class variables)

constructor # class, will create an instance when It is called
DEF the __init __ (Self, project_name = None, Members = None, dev_time = None):
self.project_name = attribute (instance variables) project_name # object
self.members = members # public property, may be performed by an object outside the class access - project members
self .__ dev_time = dev_time # private property, the class is not directly accessible outside - project cycle
ProjectGroup.group_number +. 1 =

# destructor method, performed automatically deleted when the class object (the del statement to delete class instance)
__del __ DEF (Self):
ProjectGroup.group_number - = 1
Print ( "the Destroy!")

def __str __ (self): # built-in function, to set the object into the display character string type appears how the
return self.project_name

DEF print_members (Self):
Print (self.members)

DEF get_dev_time (Self):
return by Self .__ dev_time # public member ways to access private member variables

set_dev_time (Self, dev_time) DEF:
Self .__ dev_time = dev_time

DEF __mod_budget (Self, at budget): # private methods, accessible only within the class
self.budget = at budget
( "set the project budget print: ", self.budget)

DEF set_budget (Self, at Budget):
Self .__ mod_budget (at Budget) # call the private method

@classmethod # decorator, declare a class method
def class_get_number (cls): # class methods can not access instance variables
print ( "class method:", cls.group_number) # cls represent the class itself, does not need to pass value when external calls

@staticmethod # decorator, declare a static method
def static_get_number (): # static method
print ( "static method: ", ProjectGroup.group_number) # same can not access instance variables
# print (" static method method: ", ProjectGroup.project_name) # print error, the variable can not access member (instance variables class methods)


# instantiate an object generated
project_group1 = ProjectGroup ( "RFID project group")
# outside the class, property access public object
project_group1.members = [ 'INTEGRATED TRADITIONAL AND', 'Wenjing', 'Tao', 'Sun Xiang']
attribute # access class (the class name or Object name access)
Print ( "the current number of project team:" ProjectGroup.group_number)
Print ( "number when using Object access:" project_group1.group_number)

Print ( "the name of the current project group:"project_group1)

# private member is provided by a public member variable value method
project_group1.set_dev_time (90)
print ( "the project development cycle is: {} days" .format (project_group1._ProjectGroup__dev_time))

# call the private methods public methods, set up the project budget of 200,000
project_group1.set_budget (200000)

# by class name / object calls class method
ProjectGroup.class_get_number ()
# call through an object / class name static method
project_group1.static_get_number ()

# instantiating an object
project_group2 = ProjectGroup ( "data exchange project teams", [ 'Liu', 'Luqian', 'super Geng '], 120)
Print ( "number group_number2:", ProjectGroup.group_number)

# delete an object, automate destructor
project_group3 = ProjectGroup ()
Print ( "GROUP_NUMBER:", ProjectGroup.group_number)
project_group4 ProjectGroup = () # Create per an instantiated object, group_number will add. 1
Print ( "number GROUP_NUMBER:", ProjectGroup.group_number)
del project_group3
Print ( "group_number:", ProjectGroup.group_number)

# Class built property
# returns the string class documentation
Print ( "ProjectGroup .__ doc__:", ProjectGroup .__ doc__)

# Returns the name of this class to create objects using
Print (project_group1 .__ class__)
Print (project_group2 .__ class__)

# to dictionary It returns the object in the form of attributes and attribute values (not including the attributes of the class)
Print (project_group1 .__ dict__ magic)
Print (project_group2 .__ dict__ magic)

# returns the class name of the module containing
Print (ProjectGroup .__ module__)

# returns the parent class of all name (only return to the previous parent), also known as the base class
Print (ProjectGroup .__ bases__)

# returns the name of the current module
print ( "current module name:", __name__)

Guess you like

Origin www.cnblogs.com/Teachertao/p/11221316.html