Interface and Normalization design
1. What is an interface
Hi Boy, open a query interface for me ... The interface at this point refers to the way that you provide the user with the ability to invoke its own function \ Method \ portal, interface in Java is used as follows:
================= the first part: the interface in the Java language shows the meaning of the interface: ianimal.java/** Java's Interface interface features: * 1) is a set of functions, not a function * 2) interface functions for interactive , all functions are public, that is, other objects can operate * 3) interface only defines functions, but does not involve function implementation * 4) These functions are related, are animal-related functions, but photosynthesis is not suitable to put into the ianimal inside the */package Com.oo.demo; Public interface IAnimal {public void eat (); public void run (); public void sleep (); public void Speak ();} ================= Part II: Pig.java: Pig "class design, implementation of the Iannimal interface package Com.oo.demo;public class Pig implements ianimal{// Each of the following functions requires a detailed implementation of public void Eat () {System.out.println ("Pig like to eat grass"); } public void Run () {System.out.println ("Pig run:front legs, back legs"); } public void Sleep () {System.out.println ("Pig sleep-hours every day"); } public void Speak () {System.out.println ("Pig can not Speak");}} ================= the third part: person2.java/** realize the IAnimal "people", there are several points: * 1) also realize the IAnimal interface, but "people" and "pig" implementation is not the same, in order to avoid too much code to affect reading, The code here is simplified to one line, but the output is different, the same function point of the same interface in the actual project, the implementation of the same class is not the same. * 2) Here is the same class as "person", but it's completely different from the class "people" given in the previous class., this is because the same logic concept, in different application scenarios, has the properties and functions are totally different */package Com.oo.demo;public class Person2 implements IAnimal {public void Eat () {System.out.println ("person as to eat meat"); } public void Run () {System.out.println ("person run:left leg, right leg"); public void Sleep () {System.out.println ("person sleep 8 hours every Dat"); } public void Speak () {System.out.println ("Hellow World, I am a person"); }}================= Part IV: Tester03.javapackage Com.oo.demo;public class Tester03 {public static void main (string[] args {System.out.println ("===this is a person==="); IAnimal person = new Person2 (); Person.eat (); Person.run (); Person.sleep (); Person.speak (); System.out.println ("\n===this is a pig==="); IAnimal pig = new Pig (); Pig.eat (); Pig.run (); Pig.sleep (); Pig.speak (); }} interface in Java
2. Why to use the interface
The interface extracts a group of common functions that can be used as a collection of functions
Then let the subclass implement the functions in the interface.
The significance of this is normalization, what is called normalization: just the class that is implemented based on the same interface, all of the objects produced by these classes are used in the same way as the usages.
The benefits of normalization are:
- Normalization is what the user does not need to care about the object's class, just know that these objects have some functionality, which greatly reduces the user's difficulty in using
- Normalization allows high-level external users to handle the collection of objects that are compatible with all interfaces without distinction
- Imitating interface
In Python there is no keyword called interface, if you want to imitate the concept of an interface
Third-party modules available: Http://pypi.python.org/pypi/zope.interface
Inheritance can also be used, in fact, there are two uses of inheritance
One: Inheriting the method of the base class and making its own changes or extensions (code reuse): In practice, this use of inheritance is not very significant, and often harmful. Because it causes the subclass to be strongly coupled to the base class.
Two: Declare a subclass is compatible with a base class, define an interface class (imitate Java interface), the interface class defines some interface name (that is, the function name) and does not implement the functions of the interface, subclass inherits the interface class, and implements the function in the interface
Class Interface: #定义接口Interface类来模仿接口的概念, there is no Interface keyword in python to define an interface. def read (self): #定接口函数read pass def write: #定义接口函数write passclass Txt (Interface): #文本, Specific implementation of Read and write def read (self): print (' Read method of text data ') def write: print (' Read method of text data ') class Sata ( Interface): #磁盘, implementation of Read and write def read (self): print (' Read method of hard drive data ') def write ('): print (' Hard drive data Read method ') class Process (Interface): def read (self): print (' Read method of process data ') def write (self): Print (' Read method of process data ')
The above code just looks like interface, actually does not play the role of interface, subclass can not implement interface completely, this uses the abstract class
Abstract class
1. What is abstract class
Like Java, Python also has the concept of abstract classes, but also requires the use of a module implementation, the abstract class is a special class, it is unique in that can only be inherited, cannot be instantiated
2, why should have abstract class
If a class extracts the same content from a bunch of objects, the abstract class extracts the same content from a bunch of classes, including data properties and function properties.
For example, we have banana class, Apple class, Peach class, from these classes to extract the same content is the abstract fruit of the class, you eat fruit, either to eat a specific banana, or to eat a specific peach .... You can never eat something called a fruit.
From a design point of view, if a class is abstracted from a real object, then abstract classes are based on class abstraction.
From an implementation perspective, abstract classes differ from ordinary classes in that abstract classes can only have abstract methods (without implementing functionality), that classes cannot be instantiated, can only be inherited, and subclasses must implement abstract methods. This is a bit similar to the interface, but it's actually different.
3. Implement abstract classes in Python
#一切皆文件import ABC #利用abc模块实现抽象类class all_file (metaclass=abc. Abcmeta): all_type= ' file ' @abc. Abstractmethod #定义抽象方法, no function def read (self): ' subclass must define read function ' Pass @a Bc.abstractmethod #定义抽象方法, no function def write required: ' Subclass must define write function ' pass# class TXT (all_file): # pass## T1=t XT () #报错, subclass does not define abstract method class Txt (All_file): #子类继承抽象类, but must define the read and write Methods def read (self): print (' Read method of text data ') def WR ITE (self): print (' Read method of text data ') class Sata (All_file): #子类继承抽象类, but must define the read and write Methods def read (self): print (' Number of drives Read method ') def write: print (' Read method of hard disk data ') class Process (All_file): #子类继承抽象类, but the read and write Method def read (SEL) must be defined f): Print (' Read method of Process data ') def write (self): print (' Read method of process data ') Wenbenwenjian=txt () Yingpanwenjian=sata () Jinchen Gwenjian=process () #这样大家都是被归一化了, the thought of all Documents Wenbenwenjian.read () Yingpanwenjian.write () Jinchengwenjian.read () Print (wenbenwenjian.all_type) print (yingpanwenjian.all_type) print (Jinchengwenjian.all_type)
4. Abstract class and interface
The essence of an abstract class is a class, which refers to the similarity of a set of classes, including data attributes (such as All_type) and function properties (such as Read,write), while interfaces only emphasize the similarity of function properties
Abstract classes are a straightforward concept of classes and interfaces, with some features of classes and interfaces that can be used to implement a normalized design
Python Object-oriented: abstract class