在Java中针对接口进行编程是否与在C/C ++中使用头文件具有相同的概念? [英] Is programming against interfaces in Java the same concept as using header files in C/C++?

查看:85
本文介绍了在Java中针对接口进行编程是否与在C/C ++中使用头文件具有相同的概念?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用的Java代码通常具有类似的结构

The java code I'm working on at the moment has often a structure like

文件Controller.java:

file Controller.java:

interface Controller {...}

ControllerImpl.java文件:

file ControllerImpl.java:

class ControllerImpl implements Controller {...}

但是对于每个接口,只有一个实现.这与在C/C ++中使用头文件不同,在该文件中我将代码拆分为

But for every interface there is only one implementation. Isn't that the same as using header files in C/C++ where I have the code split into files like

Controller.hpp
Controller.cpp

据我所知,已经引入了C/C ++头文件来帮助编译器,而Java不再需要了.头文件也应该有助于提高代码的可读性,但是拥有一个具有折叠和轮廓视图的现代IDE也不再是必需的.

From what I know, header files in C/C++ have been introduced to help the compiler, which is not necessary anymore in Java. Also header files should help with the readability of the code, but having a modern IDE with folding and outline view this is also not a necessity anymore.

那么为什么人们又通过对接口进行编程来通过后门又用Java引入头文件?

So why do people again introduce header files in Java through the back door by programming against interfaces?

推荐答案

否.在C ++中,文件(标头)与类相同.

No. In C++, files (headers) are not the same as classes.

通过用进行编程,也可以在Java中使用Java进行接口编程.抽象基类.

但是,Java的接口"一词非常受限制.基本上,任何函数声明都是一个接口:

However, the Java term of "interface" is quite restricted. Basically, any function declaration is an interface:

void call_me(int times); 

当然还有类和其他类型.

As are, of course, classes and other types.

在C ++中,您将这些内容分组在标头中,因此接口可以由一个标头组成.但是,它也可能包含多个标头.

In C++, you group such things in headers, so an interface can consist of one header. However, it might just as well consist of multiple headers.

这篇关于在Java中针对接口进行编程是否与在C/C ++中使用头文件具有相同的概念?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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