简体   繁体   English

Java中传递接口引用和类引用之间的区别

[英]The difference between passing interface reference and class reference in java

Is there any difference between passing Interface reference or class reference as shown in example ; 如示例所示,传递接口引用或类引用之间有什么区别;

Interface MyInterface
{
     void foo();
}
public class MyClass implements MyInterface
{


 public void foo()
    {
        doJob();
    }
}

....
//in another class or etc..
public void case1(MyClass mc)
{
    mc.foo();
}

public void case2(MyInterface mi)
{
    mi.foo();
}
....
//In somewhere
MyClass mc = new MyClass();
case1(mc);
case2(mc);

What are the key differences between case1 and case2? case1和case2之间的主要区别是什么? Do they have any advantages in terms of performance , visibility , protecting the object from illegal usage? 它们在性能,可见性,保护对象免受非法使用方面是否具有任何优势? Are there any disadvantages in using it like this? 像这样使用它有什么缺点吗?

By passing the interface you are creating the opportunity to pass all the classes which implements the interface. 通过传递接口,您将创造机会传递实现该接口的所有类。 But in case1 you can only pass the MyClass and its subclasses. 但是在case1中,您只能传递MyClass及其子类。

For example think about the following case 例如考虑以下情况

public class YourClass implements MyInterface
{

 public void foo()
    {
        doJob();
    }
}

Now in case2 you can pass both the instance of MyClass and YourClass. 现在,在case2中,您可以传递MyClass和YourClass的实例。 but in case1 you can't. 但万一,你不能。

Now, what is the importance of it? 现在,它的重要性是什么?

In OOP it is recommended to program to the interface instead of class. 在OOP中,建议编程为接口而不是类。 So if you think about good design there will be no case1. 因此,如果您考虑好的设计,就不会有case1。 Only case2 will do the job for you. 只有case2会为您完成这项工作。

Program to an interface 编程到界面

The only advantage is that you are hiding the exact implementation by using interface MyInterface and you are free to change the implementation in future. 唯一的优点是您可以通过使用接口MyInterface 隐藏确切的实现 ,并且以后可以自由更改实现。

But when you use concrete class then you are bound to the behavior of that class. 但是,当您使用具体的类时,则必须遵守该类的行为。

Suppose you were passing List<Integer> in method of other class then it can be any implementation of list the user does not care. 假设您在其他类的方法中传递List<Integer> ,那么它可以是用户不关心的list的任何实现。 But if you pass ArrayList<Integer> then the user can use ArrayList specific methods and you will not be able to change it to LinkedList in future. 但是,如果您传递ArrayList<Integer>则用户可以使用ArrayList特定的方法,将来您将无法将其更改为LinkedList

So case2() is better than case1() 所以, case2()优于case1()

There is no other benefit. 没有其他好处。

Do they have any advantages in terms of performance , visibility , protecting the object from illegal usage? 它们在性能,可见性,保护对象免受非法使用方面是否具有任何优势?

Performance will be same when passing any kind of reference, it does not matter whether it is an interface or concrete class reference. 传递任何类型的引用时,性能将相同,无论是接口引用还是具体的类引用都无关紧要。

EDIT: As per the comment and further post of @maaartinus there seems to be some difference in performance when using interfaces. 编辑:根据@maaartinus的评论和进一步的帖子,使用接口时似乎在性能上存在一些差异。 But again I would not be worried unless it proven to be a bottleneck in my code, which is going to be never. 但是我再也不担心,除非它被证明是我的代码中的瓶颈,这永远不会。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM