与类一样,Java中的接口可以有方法和变量,但在接口中声明的方法在默认情况下是抽象的(只有方法签名,没有正文)。
null
- 接口指定类必须做什么,而不是如何做。这是班级的蓝图。
- 接口是关于功能的,就像一个播放器可能是一个接口,任何实现播放器的类都必须能够(或必须实现)move()。因此它指定了类必须实现的一组方法。
- 如果一个类实现了一个接口,并且没有为接口中指定的所有函数提供方法体,那么这个类必须声明为抽象类。
- Java库的一个例子是, 比较器接口 。如果类实现了此接口,则可以使用它对集合进行排序。
语法:
interface <interface_name>{ // declare constant fields // declare methods that abstract // by default. }
要声明接口,请使用 界面 关键词。它用于提供总体抽象。这意味着接口中的所有方法都是用空体声明的,并且是公共的,默认情况下,所有字段都是公共的、静态的和最终的。实现接口的类必须实现接口中声明的所有方法。实现接口使用 工具 关键词。
我们为什么使用界面?
- 它用于实现总体抽象。
- 由于java在类的情况下不支持多重继承,但通过使用接口可以实现多重继承。
- 它还用于实现松耦合。
- 接口用于实现抽象。所以问题出现了,当我们有抽象类时,为什么要使用接口?
原因是,抽象类可能包含非final变量,而接口中的变量是final、public和static。
// A simple interface
interface
Player
{
final
int
id =
10
;
int
move();
}
为了实现一个接口,我们使用关键字:implement
// 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 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
一个真实的例子: 让我们考虑一下自行车、汽车、自行车等汽车的例子,它们有共同的功能。所以我们制作了一个界面,把所有这些共同的功能放在一起。让Bicylce,自行车,汽车…。etc在自己的课堂上以自己的方式实现所有这些功能。
import java.io.*; interface Vehicle { // all are the abstract methods. void changeGear( int a); void speedUp( int a); void applyBrakes( int a); } class Bicycle implements Vehicle{ int speed; int gear; // to change gear @Override public void changeGear( int newGear){ gear = newGear; } // to increase speed @Override public void speedUp( int increment){ speed = speed + increment; } // to decrease speed @Override public void applyBrakes( int decrement){ speed = speed - decrement; } public void printStates() { System.out.println( "speed: " + speed + " gear: " + gear); } } class Bike implements Vehicle { int speed; int gear; // to change gear @Override public void changeGear( int newGear){ gear = newGear; } // to increase speed @Override public void speedUp( int increment){ speed = speed + increment; } // to decrease speed @Override public void applyBrakes( int decrement){ speed = speed - decrement; } public void printStates() { System.out.println( "speed: " + speed + " gear: " + gear); } } class GFG { public static void main (String[] args) { // creating an inatance of Bicycle // doing some operations Bicycle bicycle = new Bicycle(); bicycle.changeGear( 2 ); bicycle.speedUp( 3 ); bicycle.applyBrakes( 1 ); System.out.println( "Bicycle present state :" ); bicycle.printStates(); // creating instance of bike. Bike bike = new Bike(); bike.changeGear( 1 ); bike.speedUp( 4 ); bike.applyBrakes( 3 ); System.out.println( "Bike present state :" ); bike.printStates(); } } |
输出
Bicycle present state : speed: 2 gear: 2 Bike present state : speed: 1 gear: 1
JDK 8接口中添加的新功能
- 在JDK 8之前,接口无法定义实现。我们现在可以为接口方法添加默认实现。此默认实现具有特殊用途,不会影响接口背后的意图。
假设我们需要在现有接口中添加一个新函数。显然,旧代码将无法工作,因为类尚未实现这些新函数。因此,在默认实现的帮助下,我们将为新添加的函数提供一个默认体。那么旧的代码仍然有效。
// An example to show that interfaces can
// have methods from JDK 1.8 onwards
interface
in1
{
final
int
a =
10
;
default
void
display()
{
System.out.println(
"hello"
);
}
}
// A class that implements interface.
class
testClass
implements
in1
{
// Driver Code
public
static
void
main (String[] args)
{
testClass t =
new
testClass();
t.display();
}
}
输出:
hello
- JDK 8中添加的另一个特性是,我们现在可以在接口中定义静态方法,这些接口可以在没有对象的情况下独立调用。注意:这些方法不是继承的。
// An example to show that interfaces can
// have methods from JDK 1.8 onwards
interface
in1
{
final
int
a =
10
;
static
void
display()
{
System.out.println(
"hello"
);
}
}
// A class that implements interface.
class
testClass
implements
in1
{
// Driver Code
public
static
void
main (String[] args)
{
in1.display();
}
}
输出:
hello
关于接口或文章摘要的要点:
- 我们不能创建接口的实例(接口不能被实例化),但我们可以引用它,引用它的实现类的对象。
- 一个类可以实现多个接口。
- 一个接口可以扩展另一个接口(但只能扩展一个接口)。
- 实现接口的类必须实现接口中的所有方法。
- 所有的方法都是公开和抽象的。所有字段都是公共的、静态的和最终的。
- 它用于实现多重继承。
- 它用于实现松耦合。
相关文章:
本文由 梅哈克·库马尔。 和 尼茨海伦德拉 。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请发表评论
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END