Object-oriented package of Python, inheritance, polymorphism analysis operation example

This article describes the Python Examples of object oriented encapsulation, inheritance, polymorphism operation. Share to you for your reference, as follows:

Encapsulation, inheritance, polymorphism is a major feature of object-oriented 3

Why should package Here Insert Picture Description
Here Insert Picture Description
benefits

When using the process-oriented programming, when a need for data processing, need to consider which template which function is used to operate, but when using object-oriented programming, since it has been storing data into this independent space, this independent space through a special variable (class) (ie, objects) are able to get to the class (template), but the method in this class there is a certain amount of, nothing to do with such will not appear in this class, so the need for data processing, can very quickly locate the method needs who more convenient

Global variables are only parts of a 1, a number of multi-function requires multiple backups, often require the use of other variables to be stored; through the package for this variable will store the data object becomes a "global "variable, then this is not the same as long as the object variable can have another one part, so this is more convenient

Code division clearer

Process-oriented

全局变量1
全局变量2
全局变量3
...
def 函数1():
  pass
def 函数2():
  pass
def 函数3():
  pass
def 函数4():
  pass
def 函数5():
  pass

Object-Oriented

class 类(object):
  属性1
  属性2
  def 方法1(self):
    pass
  def 方法2(self):
    pass
class 类2(object):
  属性3
  def 方法3(self):
    pass
  def 方法4(self):
    pass
  def 方法5(self):
    pass

Why should inherit Here Insert Picture Description
Description

Can enhance code reuse, i.e. the development of a class, can be used directly in a plurality of sub-functions

Inheritance can effectively manage code, when a class has a problem on the line as long as the modifications of this class and its subclasses of this class often do not need to modify

How to understand the multi-state description

class MiniOS(object):
  """MiniOS 操作系统类 """
  def __init__(self, name):
    self.name = name
    self.apps = [] # 安装的应用程序名称列表
  def __str__(self):
    return "%s 安装的软件列表为 %s" % (self.name, str(self.apps))
  def install_app(self, app):
    # 判断是否已经安装了软件
    if app.name in self.apps:
      print("已经安装了 %s,无需再次安装" % app.name)
    else:
      app.install()
      self.apps.append(app.name)
class App(object):
  def __init__(self, name, version, desc):
    self.name = name
    self.version = version
    self.desc = desc
  def __str__(self):
    return "%s 的当前版本是 %s - %s" % (self.name, self.version, self.desc)
  def install(self):
    print("将 %s [%s] 的执行程序复制到程序目录..." % (self.name, self.version))
class PyCharm(App):
  pass
class Chrome(App):
  def install(self):
    print("正在解压缩安装程序...")
    super().install()
linux = MiniOS("Linux")
print(linux)
pycharm = PyCharm("PyCharm", "1.0", "python 开发的 IDE 环境")
chrome = Chrome("Chrome", "2.0", "谷歌浏览器")
linux.install_app(pycharm)
linux.install_app(chrome)
linux.install_app(chrome)
print(linux)

operation result

Linux 安装的软件列表为 []
将 PyCharm [1.0] 的执行程序复制到程序目录...
正在解压缩安装程序...
将 Chrome [2.0] 的执行程序复制到程序目录...
已经安装了 Chrome,无需再次安装
Linux 安装的软件列表为 ['PyCharm', 'Chrome']

We recommend the python learning sites to see how seniors are learning! From basic python script, reptiles, django, data mining, programming techniques, as well as to combat zero-based sorting data items, given to every love learning python small partner! Python veteran day have to explain the timing of technology, to share some of the ways to learn and need to pay attention to small details, click to join our gathering python learner

Published 49 original articles · won praise 63 · views 60000 +

Guess you like

Origin blog.csdn.net/haoxun05/article/details/104545827