day 26 binding method, a non-binding method, object-oriented chuanjiang

Binding method:

Bound methods A bound method object binding method / class of
binding methods: special, bind to who is who is going to fall, and will pass over their

binding methods of the class: binding to class of class to call the class itself will pass over the

binding class methods used in any place?
time without going through the object, only you need to be able to get something through the class, with the class of binding method
binding class method, may be adjusted by the object
class the Person:
DEF the __init __ (Self, name, Age):
Print (Self)
the self.name name =
self.age Age =
# @classmethod
# DEF Test (CLS):
# Print (CLS)
# ( 'class binding method') Print
# # generates an object class is instantiated, returns
# CLS return ( 'LQZ',. 19)
@classmethod
DEF get_obj_by_name (CLS, name):
#. 1 to find the file name of the pickle name file
# 2 deserialized into an object
# 3 return objects
pass
get_obj_by_name DEF (Self, name):
#. 1 to find the file name of the file named pickle
# 2 deserialized into objects
# 3 return objects
Pass


#
# = Person.test PER1 ()
#
# = Per2 the Person ( 'Nick', 18 is)


# Person.get_obj_by_name ADMIN = ( 'LQZ')
# admin.create ()
'' '
class the Admin:
DEF the __init __ (Self, name, Age):
Print (Self)
the self.name name =
self.age = Age
@ a classmethod
DEF get_obj_by_name (CLS, name):
# accessible to the class name
type_class = cls .__ name __ lower ( ).
lookup name to the file # 1 as the file name of the pickle
# 2 deserialized into objects
# 3 return objects
pass
# Object binding method
DEF get_obj_by_name1 (Self, name):
#. 1 to find the file name of the file named pickle
# 2 deserialized into objects
# 3 return objects
Pass
# LQZ = Admin.get_obj_by_name ( 'LQZ')


ADMIN = Admin.get_obj_by_name1 (None, 'LQZ')

DEF get_obj_by_name2 (type_class, name):
#. 1 to find the file name of the file named pickle
# 2 deserialized into objects
# 3 return objects
Pass


# ADMIN = the Admin ( '',. 19 )
# = admin1 admin.get_obj_by_name1 ( 'LQZ')


'' '

binding method # class, may be adjusted by the object
class the Person:
' ''
content annotation

'' '
DEF the __init __ (Self, name, Age):
# Print (Self)
Self.name=name
self.age=age
@classmethod
DEF Test (CLS):
Print (CLS)
Print ( 'class method bound')

# Person.test ()
P = the Person ( 'Nick', 18 is)
# binding object can call a method of the class, but also the the object's class passed
p.test ()


# summary:
-classmethod was a decorator, on the top class in the function, the function becomes a binding method of a class
- class of binding method is invoked by the class, automatic pass over the class (object can also be adjusted, generally do not)
- binding method in class with what?
- without going through the object, only certain things can be done through the class, when he defined as a class method the binding method




non-binding approach
staticmethod non-binding method defined within the class, ordinary methods, who do not bind 
objects / classes can call, but does not automatically pass the value of


class the Person:
DEF __init __ (Self, name, Age):
self.name name =
Age = self.age

DEF object_method (Self):
Print ( 'I am bound method objects, object to call me')

@classmethod
DEF class_method (CLS):
Print ( 'I'm a binding method of the class, I like to call ')

# as a normal function, but is written inside a class
@staticmethod
DEF static_method ():
Print (' I am a static method, who do not bind ')
# (unbound methods) static method
# class call
# Person.static_method ()
# object to call
# P = the Person ( 'Nick',. 19)
# p.static_method ()

'' '
# generates a unique id number
# Import UUID
# Print (uuid.uuid4 ())



# Static method (non-binding method) the role of
# with classes and objects does not matter, they can be defined as static methods, generally used within the class, outside of class you can use
# is an ordinary function, trying to get it class management, can be defined as static methods
class the Person:
DEF __init __ (Self, name, Age):
self.id = self.get_uuid ()
self.name = name
self.age Age =

# as a normal function, but It is written inside a class
@staticmethod
DEF static_method ():
Print ( 'I am a static method, who do not bind')
@staticmethod
DEF get_uuid ():
Import uuid
return uuid.uuid4 ()

# Import uuid
# DEF get_uuid ( ):
# return uuid.uuid4 ()
# uuid.uuid4 A = ()
# P = the Person (uuid.uuid4 (), 'Nick', 18 is)
# P = the Person (get_uuid (), 'Nick', 18 is)
P = Person ( 'nick', 19 )
Print # (p.id)
# Print (p.get_uuid ())
Print (Person.get_uuid ())
# object-oriented Senior: Person class is a special object


construe
Object-oriented most essential to solve is: to provide scalability 

class object
program must have the class, then there is the object
class has attributes, there are methods
class the Person:
# class attribute
School = 'Oldboy'
COUNT = 0
tie # object given process, the initialization method completes the initialization object
# special, when the class is instantiated automatically called
DEF the __init __ (Self, name = 'LQZ'):
Person.count + =. 1
the self.name name =
self.age. 19 =
P = the Person ( 'Nick')
# is not modify the object class attribute
# class attributes can be changed based
Print (the Person .__ dict__ magic)
p.school = 'PPP'
Print (P .__ dict__ magic)


binding methods
defined within a class, no decorator decoration methods are bound method object
needs to call the object, when the object is called, it will pass its own
class the Person:
DEF __init __ (self, name = 'LQZ'):
self.name = name
self.age 19 =
Change_Name DEF (Self, name):
the self.name name =

P = the Person ()
p.change_name ( 'XXX')
is essentially
Person.change_name (p, 'xxx')

object interaction
class the Person:
DEF the __init __ (Self, name = 'LQZ'):
the self.name name =
self.age =. 19
DEF Change_Name (Self, name):
the self.name = name.upper ()



Inherited
reduce code redundancy
elective system, each class has two methods should
method : Get the moniker
method two: own function storage target
Import the pickle
Import OS
class BaseClass:
@classmethod
DEF get_obj_by_name (CLS, name):
# string '
#admin
class_name = Lower CLS .__ name __ ().
# file path
path=os.path.join(class_name,name)
with open(path,'rb') as f:
obj=pickle.load(f)

return obj

def save(self):
#对象拿到类self.__class__
cls=self.__class__
class_name = cls.__name__.lower()
# 文件路径
path = os.path.join(class_name, self.name)
with open(path,'wb') as f:
pickle.dump(self,f)

return True


class Admin(BaseClass):
def register(self,name,password):
self.name=name
self.password=password
self.save()


class Student(BaseClass):
def __init__(self,):
self.name=''
= self.password ''
self.school = ''
self.course_list = []
DEF choose_course (Self, COURSE_NAME):
self.course_list.append (COURSE_NAME)
self.save ()


call to the interface layer registration method
def register_interface (name, pwd):
obj = Admin.get_obj_by_name (name)
IF Not obj:
ADMIN = the Admin ()
admin.register (name, pwd)


# # write functional layer in a user
name = INPUT ( 'name')
password = INPUT ( 'password' )

register_interface (name, password)

the interface layer
DEF choose_course_interface (student_name, COURSE_NAME):
# objects accessible to students
student = Student.get_obj_by_name (student_name)
student.choose_course (COURSE_NAME)

class selection function
Users functional layer
is assumed that all courses print out
COURSE_NAME = 'Linux'

choose_course_interface (student_name, COURSE_NAME)

DEF check_all_student_course (student_name):
Student = Student.get_obj_by_name (student_name)
return student.get_courses ()

query all courses students selected
check_all_student_course (student_name)




Find the order of succession (new-style class, classic)
breadth-first and depth-first
method of the parent class is called a subclass:
1. by name
. 2.super (class name, object) method name of the parent class () super strict accordance with the mro table lookup
derived
polymorphism and polymorphism
method for controlling subclasses must implement the parent class: ABC module, by Throws
duck type : parent not the obligation, artificial constraints

encapsulating
compositions also fall package
hiding properties and methods of
discharge with __ in front of the property or method: properties and methods can hide
the hidden attribute for safety
concealment methods in order to isolate the complexity of
propetory: the method of packaging data into attribute
modify, delete
@ method name .setter
@ Method name .deleter

binding methods of the class: classmethod
staticmethod: the static method (non-binding method)



Guess you like

Origin www.cnblogs.com/wwei4332/p/11429602.html