本文重点介绍了 Java 中类和接口之间的区别。它们在语法上看起来很相似,都包含方法和变量,但它们在很多方面都有所不同。
类和接口之间的区别
下表列出了 Java 语言中接口和类之间的所有主要区别:
Class |
Interface |
---|---|
用于创建类的关键字是“class” | 用于创建接口的关键字是“interface” |
类可以被实例化,即可以创建类的对象。 | 接口无法实例化,即无法创建对象。 |
类不支持多重继承。 | 该接口支持多个inheritance. |
它可以从另一个类继承。 | 它不能继承类。 |
它可以使用关键字‘extends’被另一个类继承。 | 它可以通过使用关键字 ‘implements’ 被类继承,也可以通过使用关键字 ‘extends’ 被接口继承。 |
它可以包含构造函数。 | 它不能包含构造函数。 |
它不能包含抽象方法。 | 它仅包含抽象方法。 |
类中的变量和方法可以使用任何访问说明符(public、private、default、protected)进行声明。 | 接口中的所有变量和方法都声明为公共的。 |
类中的变量可以是 static、final 或两者都不是。 | 所有变量都是静态的和最终的。 |
Java 中的类
class 是用户定义的蓝图或原型,用于创建对象。它表示一种类型的所有对象所共有的一组属性或方法。一般来说,类声明可以包含这些组件,按顺序:
- 修饰符:类可以是公共的或具有默认访问权限(请参阅这个详情)。
- 类名称:名称应以首字母开头(按照惯例大写)。
- 超类(如果有):类的父类(超类)的名称(如果有),前面带有关键字 extends。一个类只能扩展(子类)一个父类。
- 接口(如果有):由类实现的接口的逗号分隔列表(如果有),前面带有关键字implements。一个类可以实现多个接口。
- Body:类主体用大括号 { } 括起来。
构造函数用于初始化新对象。字段是提供类及其对象的状态的变量,方法用于实现类及其对象的行为。
例子:
Java
// Java program to demonstrate Class
// Class Declaration
public class Dog {
// Instance Variables
String name;
String breed;
int age;
String color;
// Constructor Declaration of Class
public Dog(String name, String breed, int age,
String color)
{
this.name = name;
this.breed = breed;
this.age = age;
this.color = color;
}
// method 1
public String getName() { return name; }
// method 2
public String getBreed() { return breed; }
// method 3
public int getAge() { return age; }
// method 4
public String getColor() { return color; }
@Override public String toString()
{
return ("Hi my name is " + this.getName()
+ ".\nMy breed, age and color are "
+ this.getBreed() + ", " + this.getAge()
+ ", " + this.getColor());
}
public static void main(String[] args)
{
Dog tuffy
= new Dog("tuffy", "papillon", 5, "white");
System.out.println(tuffy.toString());
}
}
输出
Hi my name is tuffy. My breed, age and color are papillon, 5, white
接口 Java 语
与类一样,interface 可以具有方法和变量,但接口中声明的方法默认是抽象的(只有方法签名,没有人)。
- 接口指定类必须做什么而不是如何做。这是类的蓝图。
- 接口是关于函数的,就像播放器可以是接口一样,任何实现播放器的类都必须能够(或必须实现)move()。因此它指定了该类必须实现的一组方法。
- 如果一个类实现了一个接口并且没有为接口中指定的所有函数提供方法体,则该类必须声明为抽象的。
- Java 库示例是 Comparator Interface 。如果一个类实现了这个接口,那么它就可以用来对集合进行排序。
用法:
interface <interface_name> { // declare constant fields // declare methods that abstract // by default. }
例子:
Java
// Java program to demonstrate
// working of interface.
import java.io.*;
// A simple interface
interface in1 {
// public, static and final
final int a = 10;
// public and abstract
void display();
}
// A class that implements the interface.
class testClass implements in1 {
// Implementing the capabilities of
// interface.
public void display() { System.out.println("Geek"); }
// Driver Code
public static void main(String[] args)
{
testClass t = new testClass();
t.display();
System.out.println(a);
}
}
输出
Geek 10
相关用法
- Java Integer divideUnsigned()用法及代码示例
- Java Integer equals()用法及代码示例
- Java Integer getInteger()用法及代码示例
- Java Integer longValue()用法及代码示例
- Java Integer max()用法及代码示例
- Java Integer min()用法及代码示例
- Java Integer numberOfLeadingZeros()用法及代码示例
- Java Integer numberOfTrailingZeros()用法及代码示例
- Java Integer parseInt()用法及代码示例
- Java Integer parseUnsignedInt()用法及代码示例
- Java Integer remainderUnsigned()用法及代码示例
- Java Integer reverseBytes(int i)用法及代码示例
- Java Integer toBinaryString()用法及代码示例
- Java Integer toHexString()用法及代码示例
- Java Integer toUnsignedLong()用法及代码示例
- Java Integer toUnsignedString()用法及代码示例
- Java Integer decode()用法及代码示例
- Java Integer doubleValue()用法及代码示例
- Java Integer floatValue()用法及代码示例
- Java Integer hashCode()用法及代码示例
- Java Integer highestOneBit()用法及代码示例
- Java Integer intValue()用法及代码示例
- Java Integer lowestOneBit()用法及代码示例
- Java Integer.numberOfTrailingZeros()用法及代码示例
- Java Integer reverse()用法及代码示例
注:本文由纯净天空筛选整理自deepthisenthil大神的英文原创作品 Differences between Interface and Class in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。