Python Learning Object-oriented encapsulation

Source: Internet
Author: User


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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.