From Collectoins import namedtuple
Point=namedtuple (' point ', [' X ', ' Y '])
T1=point (ON)
Print (t1.x)
Print (T1.Y)
Class properties without methods are not changed
Simple definition
Can't change
Object-Oriented encapsulation
Private Property!!!
Private Method!!!
Several methods described with adorners
@property!!!
@classmethod!!
@staticmethod!
Encapsulation:
class Teacher:
def __init__ (self,name,pwd):
Self.name=name
self.pwd=pwd
alex=teacher (' Alex ', ' 3714 ')
print (alex.pwd) This is not safe
defines a private property:
self.__ Pwd=pwd
The PWD becomes a private property
only in its own class using the method:
def func (self ):
print (self.__pwd)
This can be used in the class's own methods The
method can also be privatized
also
can invoke the method directly at the time of instantiation, inside self. Method ()
also has a private static property
If you want to invoke the _ Class name __ methods, static properties, properties
can only be internal private properties, private methods, Definition of private static property
Password encryption with private properties and private methods:
Class Teacher:
def __init__ (SELF,NAME,__PWD):
Self.name=name
Self.__pwd=__pwd
def __p (self):
return Hash (SELF.__PWD)
def oi (self,pwd):
return hash (PWD) ==self.__p ()
So=teacher (' Alex ', 2222)
Ret=so.oi (2221)
Print (ret)
When to use private methods:
1. There are some methods just used as intermediate results
2. Methods of the parent class do not want subclasses to inherit
Private attribute Usage Examples:
class Person:
def __init__ (self,height,weight):
Self.__height=height
Self.__weight=weight
def Jisuan (self):
return self.__weight/(self.__height*self.__height)
def Change (SELF,NEW,NEWW):
if NEWW >20:
Self.__weight=neww
self.__height=new
Else:
print (' Too light weight ')
Jing=person (1.70,52)
print (Jing.jisuan ())
Jing.change (1.65,42)
@property a method disguised as a property, directly on the method, the call is a property
class Fang:
def __init__ (Self,name,chang,kuan):
Self.name=name
Self.__chang=chang
Self.__kuan =kuan
@property
def get_1 (self):
return Self.name,self.__chang*self.__kuan
Jing=fang (' Jing ', 55,22)
print (jing.get_1)
@ Method name. Setter modifying the value of a property disguised as a function
def method Name (Self,zhi):
Self. Variable name =zhi
Private methods are methods that cannot be inherited by a quilt class
@staticmethod
The methods inside the class can be passed without arguments.
It is not possible to place functions independently of the class when fully using object-oriented programming
And this function completely does not need to depend on the properties of the object and the properties of the class
You can decorate this function with Staticmethod.
Static methods: There is no need to pass argument methods that do not require the properties of the object and the properties of the class
Common methods: You must wear an object to use the properties of the object and the properties of the class
Class method: You must pass a class that does not need to use the properties of the object, but you can use the properties of the class
@classmethod the CLS.
Python Learning Object-oriented encapsulation