• Day 19 OOP


    一 everything in python is an object

    In object-oriented programming languages like Python, an object is an entity that contains data along with associated metadata and/or functionality. In Python everything is an object, which means every entity has some metadata (called attributes) and associated functionality (called methods). These attributes and methods are accessed via the dot syntax.

    We'll find that the everything-is-object design choice of Python allows for some very convenient language constructs.

    二 Encapsulation/ɪnˌkæpsjuˈleɪʃn/ 封装

    Data hiding

    In Python, we use double underscore下划线 before the attributes name to make them inaccessible/private or to hide them

    #其实这仅仅这是一种变形操作且仅仅只在类定义阶段发生变形
    #类中所有双下划线开头的名称如__x都会在类定义时自动变形成:_类名__x的形式:

    class A:
      __N=0 #类的数据属性就应该是共享的,但是语法上是可以把类的数据属性设置成私有的如__N,会变形为_A__N
      def __init__(self):
          self.__X=10 #变形为self._A__X
      def __foo(self): #变形为_A__foo
          print('from A')
      def bar(self):
          self.__foo() #只有在类内部才可以通过__foo的形式访问到.

    #A._A__N是可以访问到的,
    #这种,在外部是无法通过__x这个名字访问到。

    property

    Python programming provides us with a built-in @property decorator which makes usage of getter and setters much easier in Object-Oriented Programming.

    Before going into details on what @property decorator is, let us first build an intuition on why it would be needed in the first place.

    Class Without Getters and Setters

    Let us assume that we decide to make a class that stores the temperature in degrees Celsius. It would also implement a method to convert the temperature into degrees Fahrenheit /ˈfærənhaɪt/ 华氏温度. One way of doing this is as follows

    class Celsius:
      def __init__(self, temperature = 0):
          self.temperature = temperature

      def to_fahrenheit(self):
          return (self.temperature * 1.8) + 32

    We can make objects out of this class and manipulate the temperature attribute as we wish:

    # Basic method of setting and getting attributes in Python
    class Celsius:
      def __init__(self, temperature=0):
          self.temperature = temperature

      def to_fahrenheit(self):
          return (self.temperature * 1.8) + 32


    # Create a new object
    human = Celsius()

    # Set the temperature
    human.temperature = 37

    # Get the temperature attribute
    print(human.temperature)

    # Get the to_fahrenheit method
    print(human.to_fahrenheit())

    Using Getters and Setters

    Suppose we want to extend the usability of the Celsius class defined above. We know that the temperature of any object cannot reach below -273.15 degrees Celsius (Absolute Zero in Thermodynamics)

    Let's update our code to implement this value constraint.

    An obvious solution to the above restriction will be to hide the attribute temperature (make it private) and define new getter and setter methods to manipulate it. This can be done as follows:

    # Making Getters and Setter methods
    class Celsius:
      def __init__(self, temperature=0):
          self.set_temperature(temperature)

      def to_fahrenheit(self):
          return (self.get_temperature() * 1.8) + 32

      # getter method
      def get_temperature(self):
          return self._temperature

      # setter method
      def set_temperature(self, value):
          if value < -273.15:
              raise ValueError("Temperature below -273.15 is not possible.")
          self._temperature = value


    # Create a new object, set_temperature() internally called by __init__
    human = Celsius(37)

    # Get the temperature attribute via a getter
    print(human.get_temperature())

    # Get the to_fahrenheit method, get_temperature() called by the method itself
    print(human.to_fahrenheit())

    # new constraint implementation
    human.set_temperature(-300)

    # Get the to_fahreheit method
    print(human.to_fahrenheit())

    The property Class

    A pythonic way to deal with the above problem is to use the property class. Here is how we can update our code:

    We added a print() function inside get_temperature() and set_temperature() to clearly observe that they are being executed.

    The last line of the code makes a property object temperature. Simply put, property attaches some code (get_temperature and set_temperature) to the member attribute accesses (temperature).

    # using property class
    class Celsius:
      def __init__(self, temperature=0):
          self.temperature = temperature

      def to_fahrenheit(self):
          return (self.temperature * 1.8) + 32

      # getter
      def get_temperature(self):
          print("Getting value...")
          return self._temperature

      # setter
      def set_temperature(self, value):
          print("Setting value...")
          if value < -273.15:
              raise ValueError("Temperature below -273.15 is not possible")
          self._temperature = value

      # creating a property object
      temperature = property(get_temperature, set_temperature)


    human = Celsius(37)

    print(human.temperature)

    print(human.to_fahrenheit())

    human.temperature = -300

    Python's @classmethod and @staticmethod

    @staticmethod function is nothing more than a function defined inside a class. It is callable without instantiating the class first. It's definition is immutable via inheritance. @classmethod function also callable without instantiating the class, but its definition follows Sub class, not Parent class, via inheritance.

    https://stackabuse.com/pythons-classmethod-and-staticmethod-explained/

    三 Inheritance 继承

    Inheritance allows us to define a class that inherits all the methods and properties from another class.

    Parent class is the class being inherited from, also called base class.

    Child class is the class that inherits from another class, also called derived class.

    Create a Parent Class

    Any class can be a parent class, so the syntax is the same as creating any other class:

    Create a Child Class

    To create a class that inherits the functionality from another class, send the parent class as a parameter when creating the child class:

    class ParentClass1: #定义父类
      pass

    class ParentClass2: #定义父类
      pass

    class SubClass1(ParentClass1): #单继承
      pass

    class SubClass2(ParentClass1,ParentClass2): #多继承
      pass

    bases

    Python provides a __bases__ attribute on each class that can be used to obtain a list of classes the given class inherits.

    >>> SubClass2.__bases__
    (<class '__main__.ParentClass1'>, <class '__main__.ParentClass2'>)

     day19思维导图

  • 相关阅读:
    CSS3选择器之:nth-child(n)
    【转】JS中,中国标准时间转化为yyyy-MM-dd
    【转】浅谈JavaScript中forEach与each
    Can't connect to MySQL server (10060)
    Linux/CentOS下安装MySql RPM Bundle
    查看CentOS/Linux的版本信息
    Java中IO流,输入输出流概述与总结
    阿里历年经典Java面试题汇总
    架构师眼中的高并发架构
    Java面试经
  • 原文地址:https://www.cnblogs.com/fengshili666/p/14262566.html
Copyright © 2020-2023  润新知