对JAVA界面的困惑 [英] confused about JAVA interface

查看:32
本文介绍了对JAVA界面的困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
编程到接口"是什么意思?
接口与抽象类(通用面向对象)

我刚开始学习 JAVA,现在对界面很困惑.查了很多资料,还是不太明白.

I'm new to learn JAVA and now I'm confused about interface. I have searched and read many materials but still not clear.

当我尝试查找有关接口的一些信息时,我看到很多人谈论接口和抽象类之间的关系.但我什至不知道他们为什么要对比这两个.因为我认为抽象类是用来告诉其他人你不能创建这个抽象类的对象,如果你想,你必须修改抽象类.这是关于继承的东西,对吧?

When I try to find some information about interface, I see many people talked about the relationship between interface and abstract class. But I even don't know why they contrast these two. Because I think abstract class is used to tell other people you can not create an object of this abstract class and if you want, you must modify the abstract class. This is something about inheritance, right?

但是我不知道接口的含义.有一个接口a,如果一个class B要实现接口a,它必须使用保留字class B implements a,然后完成接口需要的所有方法.但我的问题是,如果B类必须自己完成所有方法,那么接口是什么意思?我认为我们不需要它.我不太明白.我读过很多句子,比如:界面可以体现面向对象语言的核心思想"、界面可以帮助简化程序"等等.但我真的无法理解其中的含义.

But I don't know the meaning of interface. There is a interface a, and if a class B is going to implement the interface a, it must use the reserved word class B implements a, and then complete all the methods that the interface requires. But my question is, if class B have to complete all the methods by itself, what's the meaning of interface? I think we don't need it. I don't understand it very much. I read many sentences like: "interface can reflect the core thought of object-oriented language", "interface can help make the program easier" and so on. But I can not really understand the meaning.

那么,有没有人可以给我看一些例子来让我理解界面?或者你可以告诉我一些有用的链接或清楚描述界面的书籍.我真的希望弄清楚.谢谢!

So, does anyone can show me some examples to let understand interface? Or you can tell me some useful links or the books that describe the interface clearly. I really hope to figure it out. THANK YOU!

推荐答案

假设你有一个 Car 类和 Vegetable 类,它们在现实生活中是无关的,并且有一个称为 wash() 的常见行为.因为我们可以洗车,也可以洗菜.但是洗车和洗菜是完全不同的过程/行为.

Suppose you have a Car class and Vegetable class which is unrelated in real life and there is a common behaviour called wash(). Because we can wash a car and wash a vegetable too. But washing a car and washing a vegetable is totally different process/behaviour.

例如:汽车应该用动力泵清洗,蔬菜放在厨房水槽下面.所以洗涤方式不同.因此,您将清洗过程作为接口中的 wash() 方法说 Washable 并在 CarVegetable 中实现它们 类.

For ex: Car should be washed with a power pump, Vegetables under your kitchen sink. So the way of washing is different. So you make the washing process as a method wash() in the interface say Washable and you implement them in both Car and Vegetable class.

interface Washable {

 public void wash();

} 

public class Car implements Washable {

 public void wash() {

   // wash the car with a power pump

 }


}

public class Vegetable implements Washable {

public void wash() {

   // wash the vegetable under a kitchen sink

 }


}

作为一个人,你会想洗车和洗菜.

As a person, you would want to wash a car as well as vegetable.

public class Person  {


Washable washableObject = new Car();

washableObject.wash();

washableObject = new Vegetable();

washableObject.wash();




}

  1. 所以接口是连接具有共同行为的不相关类的一种方式.但行为会以不同的方式实现或将来可以改变.

  1. So interface is a way to connect unrelated classes which has a common behavior.But the behavior will be differently implemented or can be changed in future.

有一天,您决定改变洗车的方式.假设您购买了一台洗车机".因此,Car 类中的 wash() 方法内部的实现发生了变化.

One day you decide to change the way you wash a Car.Suppose you have purchased a "car washing machine". So the implementation changes inside the method wash() in the Car class.

公共类 Car 工具可清洗 {

public class Car implements Washable {

  public void wash() {

   // wash the car with my new car washing machine !!

 }

}

但是作为 Person ,您仍然调用 wash() 方法.wash() 方法的实现方式发生了变化(用你的新洗车机洗车),这个实现变化不会影响你的 Person 类.

But as a Person , you still call the wash() method. The way the wash() method is being implemented changed ( washing the car with your new car washing machine ), this implementation change did not affect your Person class.

希望您现在清楚我们为什么使用 interfaces.

Hope you are clear why we use interfaces now.

这篇关于对JAVA界面的困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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