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

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

问题描述


可能重复:

编程到界面是什么意思?

接口与抽象类(一般OO)

我是学习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?

但是我不知道接口的含义。有一个接口,如果类B 要实现接口a,它必须使用保留word 类B实现,然后完成接口所需的所有方法。但我的问题是,如果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 类和蔬菜类在现实生活中无关,并且有一种称为 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.

对于ex: Car 应使用动力泵冲洗,蔬菜在你的厨房水槽下面。所以洗涤方式不同。所以你将洗涤过程作为一种方法 wash()在界面中说可洗并在两者中实现它们code>汽车和蔬菜类。

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.

公共类汽车实施可洗{

  public void wash() {

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

 }

}

但作为Person,您仍然可以调用 wash()方法。 wash()方法的实施方式发生了变化(用新洗车机清洗汽车),此实施变更并未影响您的人员 class。

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.

希望您清楚为什么我们现在使用接口

Hope you are clear why we use interfaces now.

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

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