首页 > 新手java接口问题

新手java接口问题

刚学 java 对接口一知半解, 网站找来一个实例,感觉其中 class computer implements usb 好像根本不必要, 为什么不直接在 computer 里面直接写个 USB 方法? 还要弄个接口那么麻烦, 这个例子应该没有很好的提现接口用处, 但是我又不知道到底怎么理解接口的用处, 请大牛们指点.

下面为网上找的例子:

首先我们来实现一个简单的接口的定义。

interface usb {
    
    public void print();
    
}

在接口中不可以实现函数的方法,也就是不可以如下这样做:

interface usb {
    
    public void print(){
    
        System.out.println("hello world");
    
    };
    
}

接下来我们用一个类来实现这个接口。也就是一个具体化的过程,如下例:

class computer implements usb {
    
    @Override
    
    public void print() {
    
        // TODO Auto-generated method stub
    
        System.out.println("i am a computer");
    
    }

接下来就可以在主函数中实现这个类了。如下例:

public static void main(String[] args) {
    
   computer computer=new computer();
    
   computer.print();
    
} 

接下来进行继承多个接口。如下例:

interface usb {
    
    public void print();
    
}
    
interface call {
    
    public void callyou();
    
}

下面用类来进行实现。如下例:

class computer implements usb, call {
    
    @Override
    
    public void print() {
    
        // TODO Auto-generated method stub
    
        System.out.println("i am a computer");
    
    }
    
    @Override
    
    public void callyou() {
    
        // TODO Auto-generated method stub
    
        System.out.println("i will call");
    
    }
    
}

主方法中再实现一下就可以了。


可以看看我之前的这个回答。https://.com/q/1010000005140317/a-1020000005141026


看第5.9章:Why interfaces

原文如下:

An interface is a contract (or a protocol, or a common understanding) of what the classes can do. When a class implements a certain interface, it promises to provide implementation to all the abstract methods declared in the interface. Interface defines a set of common behaviors. The classes implement the interface agree to these behaviors and provide their own implementation to the behaviors. This allows you to program at the interface, instead of the actual implementation. One of the main usage of interface is provide a communication contract between two objects. If you know a class implements an interface, then you know that class contains concrete implementations of the methods declared in that interface, and you are guaranteed to be able to invoke these methods safely. In other words, two objects can communicate based on the contract defined in the interface, instead of their specific implementation.
Secondly, Java does not support multiple inheritance (whereas C++ does). Multiple inheritance permits you to derive a subclass from more than one direct superclass. This poses a problem if two direct superclasses have conflicting implementations. (Which one to follow in the subclass?). However, multiple inheritance does have its place. Java does this by permitting you to "implements" more than one interfaces (but you can only "extends" from a single superclass). Since interfaces contain only abstract methods without actual implementation, no conflict can arise among the multiple interfaces. (Interface can hold constants but is not recommended. If a subclass implements two interfaces with conflicting constants, the compiler will flag out a compilation error.)

我的理解是接口是一种规范,可以表示实现了某接口的类一定有某些函数。接口有一个很实用的特性是可以用接口变量引用实现了该接口的对象,也就是说AA类实现了A接口的话,可以这样写:A a=new AA()。试想一下,如果你写了如下的代码给别人用:
void show(A a) {
a.print();
}
你如果想用别人传过来的a对象的print()函数,你怎么确保它这个a对象一定有print()函数?但如果你的A是个接口的话, 则别人想调用你写的show(),一定要写一个实现了A接口,有print函数的类作为参数。也就是说,你写的接口部分地规范了要实现你这个接口的类的代码。


建议你选一本讲解设计模式的数看看,学习学习你就知道为什么要这么做了。

假设我有需要一个日志类,但是我并不确定日志要如何写入(日志也许会写入到文件里,也可能写入到数据库,并且我需要保证随时切换写入点),那这时候不同的写入方式代码一定是有有所区别的。这时候我们就需要定义多个日志类,DbLogFileLog……而在程序中,如果不使用接口,你如何去使用它,难道一旦切换日志就把所有的代码修改一遍吗?当然不是,你只要让DbLogFileLog……都实现一个接口Log,在程序中使用Log所定义的方法,则我们可以通过传入不同的实例轻松的切换日志写入方式。


接口在开发过程中可以快速分离工作内容。
比如调用者在写业务逻辑的时候需要一个功能,可能是数据库访问,或者复杂计算,但是他的工作专注于实现业务逻辑,不想分开精力去做底层实现,那么他只需要先实现一个接口,定义了规范,然后就可以继续他的业务逻辑代码了。
而实现者可以根据这个接口规范,做具体的实现。
这样通过使用接口就可以快速的分离工作内容,达到团队并行工作的目的。

此外,如果规范是通过接口定义的,那么当你这个功能有多个实现时,你只要实现了这个接口,那么可以快速的替换具体实现,做到代码层面的完全可以分离。

总结起来就一句话:接口或者规范可以在开发过程中做到分离。

作者:徐梦锦
链接:http://www.zhihu.com/question/20111251/answer/43235562
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

【热门文章】
【热门文章】