java 接口是一个类吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11720288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 06:08:15  来源:igfitidea点击:

Is an interface a class?

javainterface

提问by smallB

Is an interface a special kind of class or can you say that an interface isn't a class at all?

接口是一种特殊的类,还是可以说接口根本不是类?

回答by Mark Byers

An interface isn't a class, but you could say that both interfaces and classes are types.

接口不是类,但您可以说接口和类都是类型

From the Java specification:

Java 规范

In the Java programming language, every variable and every expression has a type that can be determined at compile-time. The type may be a primitive type or a reference type. Reference types include class types and interface types.

在 Java 编程语言中,每个变量和每个表达式都有一个可以在编译时确定的类型。类型可以是原始类型或引用类型。引用类型包括类类型和接口类型。

Notice though there is a special class called Class<T>that can represent both classes and interfaces:

请注意,虽然有一个特殊的类Class<T>可以表示类和接口:

Instances of the class Class represent classes and interfaces in a running Java application.

类 Class 的实例表示正在运行的 Java 应用程序中的类和接口。

The fact that an interface is represented by a Classinstance where isInterfaceis truecould give you the impression that an interface is just a special type of class. However this is not the case.

该接口由律师代表的事实Class实例,其中isInterface就是true可以给你的印象是一个界面仅仅是一种特殊类型的类。然而,这种情况并非如此。

回答by Joachim Sauer

No, an interface is not a class in Java.

不,接口不是 Java 中的类。

An interface is a type and all reference types (i.e. non-primitive types) handle quite similarly in Java. Often when people say "class" they are actuallyreferring to a "reference type".

接口是一种类型,所有引用类型(即非原始类型)在 Java 中的处理方式都非常相似。通常当人们说“类”时,他们实际上指的是“引用类型”。

What mightbe confusing you is that an interface definition is stored in a .classfile, but that's just a technical artifact of Java. In fact all reference type definitions (classes, interfaces, annotations, enums) are stored in .classfiles in Java.

什么可能会混淆大家的是,一个接口定义存储在一个.class文件中,但是这爪哇只是一个技术神器。事实上,所有引用类型定义(类、接口、注释、枚举)都存储.class在 Java 文件中。

回答by Bruce_Wayne

The concept of interfaces comes from Abstract Classes, where as abstract classes contains prototypes of method (or abstract methods) and can have few of its methods defined also, while interfaces contains onlythe prototypes(or signature) of method or abstract methods, whose definition is to be provided by the implementing class. so from the above statement it is clear that interfaces are like 100 percent abstract classes where - none of its method is defined. mentioning it again interfaces are like 100 percent abstract classes but not the classes.

接口的概念来自抽象类,其中抽象类包含方法(或抽象方法)的原型,并且可以定义很少的方法,而接口包含方法或抽象方法的原型(或签名),其定义由实现类提供。所以从上面的陈述中可以清楚地看出,接口就像 100% 的抽象类,其中 - 没有定义任何方法。再次提到它,接口就像 100% 的抽象类,但不是类。

"Interfaces are contracts for what a class can do"

“接口是一个类可以做什么的契约”

A reason for introducing interface is, we can extendonly single class but interface brought a new thingimplementin java so we can implement thousands of interface.So we can not say that it is a class.

引入接口的一个原因是,我们extend只能单个类,但是接口在java中带来了一个新东西implement,所以我们可以实现数千个接口。所以我们不能说它是一个类。

you can get more about this Here!

您可以在此处获得更多信息!

回答by Byter

Interface is just a contract which all implementing classes should follow. An interface is something like a template which cannot make an impact until a class implements it.

接口只是所有实现类都应该遵循的契约。接口就像一个模板,在类实现它之前不会产生影响。

回答by CurtainDog

Yes, an interface is an instance of java.lang.Class. If you have a Classyou can interrogate it to see if it is an interface: http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#isInterface()

是的,接口是java.lang.Class. 如果你有一个Class你可以询问它是否是一个接口:http: //docs.oracle.com/javase/7/docs/api/java/lang/Class.html#isInterface()

回答by MaVRoSCy

An interface(is a group of related methods with empty bodies.) is just an interface. Its not a class(A class is the blueprint from which individual objects are created).

接口(是一组带有空体的相关方法。)只是一个接口。它不是一个类(类是创建单个对象的蓝图)。

notice that you define an interface like this

请注意,您定义了这样的接口

interface Bicycle {....}

and a class is defined like this

一个类是这样定义的

class MyBMX implements Bicycle{...}

So an Interface is an Interface and NOT a class

所以接口是接口而不是类