繁体   English   中英

使用SuperClass作为参考而不是SubClass?

[英]Using SuperClass as reference instead of SubClass?

要实现队列,我们​​为什么要使用?

Queue<Integer> q = new LinkedList<Integer>();

我的意思是超类引用,为什么我们不使用LinkedList引用?

我们有一个名为Code的设计原则,从不与具体实现接口

这里Queue是一个可以保存实现类的接口

优于上述原则的优点

1.将来很容易在多个实现之间切换

 So it is always recommonded to code to interface instead of concrete implementations

如果我们不使用上述原则,我们就会失去关键的面向对象的概念多态性。

避免不必要的限制

你应该总是尝试用最少的不必要的限制来声明你的变量; 通常这意味着像Queue或Collection这样的界面。 这使您可以更轻松地更改您的实现。 我将使用集合框架中的两个类作为我的示例,但原则是通用的。

想象一下以下目标:

我需要2个可以添加对象的集合。 而且我还需要能够找到两个集合中的哪些对象。 换句话说,我需要以下方法

Collection#add
Collection#retainAll

这些都在Collection内,所以任何集合都可以。 Collection是一个接口,所以我需要选择一个具体的实现。 我一时兴起选择ArrayList。 我的代码如下:

public static void main(String[] args){
    Collection<Integer> a=new ArrayList<>();
    Collection<Integer> b=new ArrayList<>();

    initialise(a);
    initialise(b);
    testRetain(a,b);
}

public static void initialise(Collection<Integer> collection){
    Random rnd=new Random();


    for(int i=0;i<1000;i++){
        collection.add(rnd.nextInt());
    }
}

public static void testRetain(Collection<Integer> collection1, Collection<Integer> collection2){
    collection1.removeAll(collection2);
}

这段代码工作正常,完全符合我的要求。 然而; 在剖析中我发现它是一个瓶颈。 因此,我测试了Collection类的不同实现并分析了结果。

在此输入图像描述

正如您所看到的,对于retainAll操作,HashSet变得更好。 所以我可以通过改变new ArrayList<>();来改变我的实现new ArrayList<>(); new HashSet<>(); 在一个地方。 不需要改变我用过HashSet所有其他方法,因为他们不关心,只要他们得到某种Collection他们很高兴

public static void main(String[] args){
    Collection<Integer> a=new HashSet<>();
    Collection<Integer> b=new HashSet<>();

    initialise(a); //<--no need to alter this method
    initialise(b);
    testRetain(a,b); //<--no need to alter this method
}

那很容易。 现在用几十种方法想象一个更大的应用程序; 所有这些都可以硬编码以使用ArrayList即使它们不需要,所有这些都需要手动更改。

To implement a Queue,why do we use?

没有人能够断言这是每个人都必须遵循的模式。 你可以很好地使用

LinkedListq=new LinkedList();

经常使用上述模式的原因,因为它提供了多态性的力量。 简而言之,您现在需要一个LinkedList但稍后您可能需要PriorityQueue 要对多个具体类使用相同的引用,我们使用上面的模式。

暂无
暂无

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

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