码迷,mamicode.com
首页 > 其他好文 > 详细

特殊现象——接口(interface)

时间:2017-01-21 19:33:19      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:补充   tar   dem   log   nbsp   write   允许   out   方法   

 接口——java开发工作者经常挂在嘴边的名词,对于使用python的我来说,往往就代表着url,原因有两:

一是python没有接口概念,另则工作缘故,只接触过web接口测试,坐井观天之势未深入了解,原来接口除了是连接前后端的,还可以是数据传递过程中的类方法或者函数。

Java 接口(interface)

接口使用 interface 关键字来声明,同现实生活常识一致,它规定了可以用来干什么,而不必了解它是怎么做到的,例如冰箱可以用来冷冻,不必了解它如何制冷。

主要特点:
a.接口中只能定义抽象方法,这些方法默认为是 public abstract 的
b.一个接口不实现另一个接口,但可以继承多个其他接口,接口的多继承特点弥补了类的单继承

实现接口的格式如下:
修饰符 class 类名 extends 父类 implements 多个接口 {
    实现方法
}

//串行硬盘接口
public interface SataHdd extends A,B{
    // 连接线的数量
    public static final int CONNECT_LINE = 4;
    // 写数据
    public void writeData(String data);
    // 读数据
    public String readData();
}
interface A{
    public void a();
}
interface B{
    public void b();
}

 

在java中,通常先定义了一个接口,然后在继承这个接口的类中再写接口的实现方法

1.定义接口(IAnimal.java)

package mypor.interfaces.demo;

public interface IAnimal {

    public String Behavior(); //行为方法,描述各种动物的特性
}

2.实现接口

package mypor.interfaces.demo;
import mypor.interfaces.demo.IAnimal;

//类: 狗
public class Dog implements IAnimal{
    
    public String Behavior()
    {
        String ActiveTime = "我晚上睡觉,白天活动";
        
        return ActiveTime;
    }
}



//类:猫
public class Cat implements IAnimal{

    public String Behavior()
    {
        String ActiveTime = "我白天睡觉,晚上捉老鼠。";
        
        return ActiveTime;
    }
}

3.测试继承接口的类

package mypor.interfaces.demo;
import mypor.interfaces.demo.Dog;
import mypor.interfaces.demo.Cat;


public class Test {
    
    public static void main(String[] args) {         
        //调用dog和cat的行为 
        Dog d = new Dog();
        Cat c = new Cat();
        System.out.println(d.Behavior());
        System.out.println(c.Behavior());
    }
}

注:java的“接口”是一个特例而非普通现象,是对类单继承的补充。

 

python自定义接口

python里无接口类型,定义接口只是人为的规范,致力于在编程过程自我约束
1.普通类定义接口

 1 >>> class Interface(object):
 2     def A(self):
 3         pass
 4     def B(self):
 5         pass
 6 
 7 >>> class realize_interface1(Interface):
 8     def __init__(self):
 9         pass
10     def A(self):        #方法名不必同接口类中一致
11         print 实现接口中的A方法
12     
13 >>> class realize_interface2(Interface):
14     def __init__(self):
15         pass
16     def B(self):
17         print 实现接口中的B方法
18         
19 >>> a = realize_interface1()
20 >>> a.A()
21 实现接口中的A方法
22 >>> b = realize_interface2()
23 >>> b.B()
24 实现接口中的B方法

2.抽象类,抽象方法定义接口

 1 >>> from abc import ABCMeta,abstractmethod  #python没有抽象类、接口的概念,所以要实现这种功能得abc.py 这个类库
 2 >>> class interface(object):  
 3     __metaclass__ = ABCMeta #指定这是一个抽象类 ,或者写成 class interface(metaclass = ABCMeta):
 4     @abstractmethod  #抽象方法  
 5     def A(self):  
 6         pass  
 7       
 8     def B(self):  
 9         pass
10 
11 >>> class realize_interface1(interface):  
12     def __init__(self):      
13         print 这是接口interface的实现  
14     def A(self):  
15         print 实现A功能          
16     def B(self):  
17         pass
18 
19 >>> class realize_interface2(interface):  
20     def __init__(self):      
21         print 这是接口interface的实现  
22     def A(self):  
23         pass        
24     def B(self):  
25         print "实现B功能"
26 
27 
28 >>> class realize_interface(interface): #必须实现interface中的所有函数,否则会编译错误  
29     def __init__(self):      
30         print 这是接口interface的实现        
31     def A(self):  
32         print "实现Marlon功能"
33 
34 >>> a = realize_interface1()
35 这是接口interface的实现
36 >>> a.A()
37 实现A功能
38 >>> b = realize_interface2()
39 这是接口interface的实现
40         
41 >>> c = realize_interface()
42 
43 Traceback (most recent call last):
44   File "<pyshell#148>", line 1, in <module>
45     c = realize_interface()
46 TypeError: Cant instantiate abstract class realize_interface with abstract methods B
47 
48 >>> f = interface()   #抽象类不能直接实例化
49 
50 Traceback (most recent call last):
51   File "<pyshell#152>", line 1, in <module>
52     f = interface()
53 TypeError: Cant instantiate abstract class interface with abstract methods A

从上面可以看出要继承抽象类的接口,需要把其中的每个方法全部实现,否则会报编译错误;另外python允许多继承,故而接口在python中并没有那么重要。

 

python第三方扩展库接口

Python本身并不提供提口的创建和使用,但是我们可以通过第三方扩展库来使用接口,那就是Zope.interface

 1 from zope.interface import Interface
 2 from zope.interface import implements
 3 
 4 
 5 # 定义接口
 6 class IHost(Interface):
 7     
 8     def goodmorning(self,guest):
 9         pass
10 
11 class Host(object):
12     implements(IHost)  # 实现接口 
13 
14     def goodmorning(self, guest):
15         return "Good morning, %s!" % guest
16 
17 if __name__ == __main__:
18     h = Host()
19     hi = h.goodmorning(ryana)
20     print(hi)

运行结果:

Good morning,ryana!

 

参考:http://www.weixueyuan.net/view/6009.html

        http://www.cnblogs.com/fnng/p/5259786.html

特殊现象——接口(interface)

标签:补充   tar   dem   log   nbsp   write   允许   out   方法   

原文地址:http://www.cnblogs.com/Ryana/p/6307560.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!