Polymorphism polymorphic class

A polymorphic

Polymorphism refers to: various forms of a class of things ,, (an abstract class multiple subclasses, and thus depends on the concept of polymorphism inherited)

State wherein more than 1.1

  1. There are several types of forms sequence data: strings, lists, tuples

  2. There are a variety of animal forms: human, dog, pig

  3. Polymorphism may have binding (generally have a binding, binding strong in other languages)

    • Act 1
    class Animal:
        def speak(self):
            pass
    
    class Pig(Animal):
        def speak(self):
            print("哼哼哼")
    class Dog(Animal):
        def speak(self):
            print("汪汪汪")
    class People(Animal):
        def speak(self):
            print("say hello")
    
    • Act 2
    #用abc实现接口的统一化,约束代码(用的比较少)
    import abc
    #1 在父类括号内中写metaclass=abc.ABCMeta
    class Animal(metaclass=abc.ABCMeta):
        #2第二在要约束的方法上,写@abc.abstractmethod
    
        @abc.abstractmethod
        def speak(self):
            pass
    
    class Pig(Animal):
        # def aaa(self):
        #     print('哈哈哈')
        # 如果子类中没有speak方法,就会报错
        def speak(self):
            print('哼哼哼')
    pig = Pig()
    • Act III

      #用异常处理来实现
      class Animal():
          def speak(self):
              # 主动抛出异常
              raise Exception('必须要重写哦')
      class Pig(Animal):
          #如果没有会报错
          def speak(self):
              print('哼哼哼')
      
      pig = Pig()
    • Ducks categories: long walks like a duck (object has a binding method), then you are a duck

      class Pig:
          def speak(self):
              print('哼哼哼')
      class People:
          def speak(self):
              print('哈哈哈')
      pig = Pig()
      people = People()
      def animal_speak(obj):
          obj.speak()
      animal_speak(pig)
      animal_speak(people)

Second, polymorphism

Polymorphism is: function with different functions can use the same function name, so that you can call the function with a different content of the function name. In object-oriented approach is generally expressed polymorphism: send the same message to the different objects, the different objects will have when receiving different behavior (i.e., method). That is, each object can be on your way to respond to a common message. The so-called news is call the function, different behavior refers to different implementations that perform different functions.

  1. Polymorphisms: Different reflect a variety of forms
  2. Polymorphism depends on: Inheritance
  3. Polymorphism: the definition of a unified interface
class Animal:
    def speak(self):
        pass

class Pig(Animal):
    def speak(self):
        print("哼哼哼")
class Dog(Animal):
    def speak(self):
        print("汪汪汪")
class People(Animal):
    def speak(self):
        print("say hello")

pig = Pig()
dog = Dog()
people = People()


def animal_speak(obj):
    obj.speak()
animal_speak(pig)
animal_speak(dog)
animal_speak(people)
#

哼哼哼
汪汪汪
say hello

2.1 advantage polymorphism

  1. Increasing the flexibility of the program: maintaining the status quo, regardless of the ever-changing target user is the same kind of form to call, such as func (animal)
  2. Increase the amount of program Scalability: Animal class created by inheriting a new class, users do not need to change your code, or use func (animal) to call

Linux polymorphism in three polymorphic

  • The traditional wording
class File:
    def read(self):
        pass
    def write(self):
        pass
#内存类
class Memory(File):
    def read(self):
        print("Memory....read")
    def write(self):
        print('Memory.....write')
  • Ducks type of wording

    class Memory:
        def read(self):
            print("Memory....read")
        def write(self):
            print('Memory.....write')
    
    class Network:
        def read(self):
            print("Memory....read")
        def write(self):
            print('Memory.....write')

IV Summary

Multi-state: a thing of the same variety of forms, animals were divided into human, porcine (defined angle)
Polymorphism: one kind is called, the implementation of different effects (polymorphism)

Guess you like

Origin www.cnblogs.com/SkyOceanchen/p/11425933.html