新手java接口问题

查看:112
本文介绍了新手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");
    
    }
    
}

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

解决方案

看第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.)

这篇关于新手java接口问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆