Object-oriented package of python

Package

Object-oriented three characteristics: inherited polymorphism Package

Hidden object's properties and implementation details, just outside the provision of public access methods

Encapsulated in a broad sense : the methods and variables are encapsulated in the class

Package in the narrow sense : in external class altogether can not call up

advantage

  1. The change isolation
  2. Easy to use
  3. Improve reusability
  4. Improve security

Package principles:

  • The content does not need to provide external hide
  • The properties are hidden to provide public access method for aligning

Private variables and private methods

In the beginning of the double-underlined embodiment python with hidden attribute (arranged private)

Code examples

# 其实这仅仅这是一种变形操作
# 类中所有双下划线开头的名称如__x都会自动变形成:_类名__x的形式:

class Student:
    def __init__(self,name):
        self.__name = name
    def get_name(self):
        return self.__name
    def set_name(self,value):
        if type(value) is str:
            self.__name =value
            
mhy = Student('mhy')
print(mhy.get_name())
mhy.set_name('alex')
print(mhy.get_name())
print(mhy.__name)


#A._Student__name是可以访问到的,即这种操作并不是严格意义上的限制外部访问,仅仅只是一种语法意义上的变形
# print(mhy.__dict__) 查看该类的属性,可以看到__name变成{'_Student__name': 'alex'}
# 属性封装
class Person:
    def __init__(self,username,password):
        self.username = username
        self.__password = password

p = Person('alex','alex3714')
print(p.username)

# 类变量封装
class A:
    __val = []
    def __init__(self):
        print(A.__val)   #只有在类内部才可以通过__val的形式访问到.

A()
print(A.__val)

# 类方法封装
class A:
    def func(self):
        self.__aaa()
    def __aaa(self):
        print('aaa')

a = A()
a.func()

This automatic deformation characteristics :

X 1. class defined for internal use only, such as self.x, is the result of deformation by reference.

** 2. This deformation fact, it is against external deformation is not accessible to the outside through __x name. **

** 3 subclass definition __x not covered in the parent class definition __x, as a subclass morphed into: _ subclass name __x, and morphed into the parent class: the parent class name _ _ _x, that is, double the decline at the beginning of the line of succession to property in the subclass, the subclass is not covered. **

This deformation problems that need attention are:

*** 1 This mechanism also does not limit us direct access from the outside property real sense, knowing the class name and property name can spell the name: class name _ __ property, then you can visit, such as a. _A__N ***

2. Modification of the process into effect only within the class, as defined in the assignment, is not deformed

3. In inheritance, the parent class if you do not want subclasses to cover their own methods, methods can be defined as private

#把fa定义成私有的,即__fa
>>> class A:
...     def __fa(self): #在定义时就变形为_A__fa
...         print('from A')
...     def test(self):
...         self.__fa() #只会与自己所在的类为准,即调用_A__fa
... 
>>> class B(A):
...     def __fa(self):
...         print('from B')
... 
>>> b=B()
>>> b.test()
from A

Packaging and scalability

Package comprising a clear distinction between inside and outside, so that the class implementor may modify things within the package without affecting the code outside caller; and knows only for external use with an interface (function), as long as the interface (function) name, change parameters, using who forever without changing code. This provides a good basis for cooperation - or, as long as the interface to the basic convention unchanged, the code changes cause for concern.

#类的设计者
class Room:
    def __init__(self,name,owner,width,length,high):
        self.name=name
        self.owner=owner
        self.__width=width
        self.__length=length
        self.__high=high
    def tell_area(self): #对外提供的接口,隐藏了内部的实现细节,此时我们想求的是面积
        return self.__width * self.__length


#使用者
>>> r1=Room('卧室','egon',20,20,20)
>>> r1.tell_area() #使用者调用接口tell_area


#类的设计者,轻松的扩展了功能,而类的使用者完全不需要改变自己的代码
class Room:
    def __init__(self,name,owner,width,length,high):
        self.name=name
        self.owner=owner
        self.__width=width
        self.__length=length
        self.__high=high
    def tell_area(self): #对外提供的接口,隐藏内部实现,此时我们想求的是体积,内部逻辑变了,只需求修该下列一行就可以很简答的实现,而且外部调用感知不到,仍然使用该方法,但是功能已经变了
        return self.__width * self.__length * self.__high


#对于仍然在使用tell_area接口的人来说,根本无需改动自己的代码,就可以用上新功能
>>> r1.tell_area()

Guess you like

Origin www.cnblogs.com/Hybb/p/11518953.html