简体   繁体   English

如何实例化泛型抽象类

[英]how to instance generic abstract class

I am trying to profile a ann algorithm written in Java that is implemented as a generic abstract class and I cant figure out how to instance it. 我试图剖析用Java编写的an算法,该算法作为通用抽象类实现,但我不知道如何实例化它。

Eclipse gives me error "Cannot instantiate the type KdTree" which is not very helpful. Eclipse给我的错误“无法实例化类型KdTree”不是很有帮助。 Any ideas on how to instance this class so I can test it? 关于如何实例化此类的任何想法,以便我可以对其进行测试?

Class defination and constructor: 类定义和构造函数:

public abstract class KdTree<T> {    
    private KdTree(int dimensions, Integer sizeLimit) {
        this.dimensions = dimensions;
    }
}

My attempt to instance it: 我尝试实例化它:

public class test_robo {
    public void run_test() 
    {
        KdTree<Integer> tree = new KdTree<Integer>(1,1);
    }
}

link to the full code for KdTree http://robowiki.net/wiki/User:Rednaxela/kD-Tree 链接到KdTree的完整代码http://robowiki.net/wiki/User:Rednaxela/kD-Tree

First of all, you cannot instantiate an abstract class. 首先,您不能实例化一个抽象类。

I saw the code in the link you provided; 我在您提供的链接中看到了代码; there are few implementations of the base class KdTree<T> already in there. 基类KdTree<T>已经很少。

  1. WeightedSqrEuclid 加权平方欧氏
  2. WeightedManhattan 曼哈顿加权
    ... ...

If that's not what you're looking for, extend the base class and implement all those abstract methods as you wish. 如果这不是您想要的,请扩展基类并根据需要实现所有这些抽象方法。

You cannot instantiate an abstract class directly. 您不能直接实例化一个抽象类。 The reason it is declared abstract is that it is not meant to be used by itself - you have to provide an implementation of its abstract methods first. 声明为抽象的原因是它本身并不意味着要使用-您必须首先提供其抽象方法的实现。

You need to inherit your own class from the abstract base, implement its abstract methods, and then instantiate your class. 您需要从抽象库继承您自己的类,实现其抽象方法,然后实例化您的类。 An instance of your class is automatically an instance of its abstract base. 类的实例自动是其抽象库的实例。

public class ProfilerTree extends KdTree<Integer> {
    public ProfilerTree(int dimensions, Integer sizeLimit) {
        super(dimensions, sizeLimit);
    }
    ...
    // Implement abstract methods of KdTree<Integer> here
}
...
KdTree<Integer> tree = new ProfilerTree(1,1);

you can't instantiate an abstract class. 您不能实例化一个抽象类。 Abstract actually means it doesn't make sense on its own so it always has to be extended and its methods implemented. 实际上,抽象意味着它本身没有意义,因此必须对其进行扩展并实现其方法。

Unlike interfaces, abstract classes can contain fields that are not static and final, and they can contain implemented methods. 与接口不同,抽象类可以包含非静态和最终字段,并且可以包含已实现的方法。 Such abstract classes are similar to interfaces, except that they provide a partial implementation, leaving it to subclasses to complete the implementation. 此类抽象类与接口相似,不同之处在于它们提供了部分实现,将其留给子类来完成实现。 If an abstract class contains only abstract method declarations, it should be declared as an interface instead. 如果抽象类仅包含抽象方法声明,则应将其声明为接口。 Multiple interfaces can be implemented by classes anywhere in the class hierarchy, whether or not they are related to one another in any way. 可以通过类层次结构中任何位置的类来实现多个接口,无论它们是否以任何方式相互关联。 Think of Comparable or Cloneable, for example. 例如,考虑可比或可克隆。 By comparison, abstract classes are most commonly subclassed to share pieces of implementation. 相比之下,抽象类通常被子类化以共享实现。 A single abstract class is subclassed by similar classes that have a lot in common (the implemented parts of the abstract class), but also have some differences (the abstract methods). 单个抽象类由相似类的子类继承,这些相似类具有很多共同点(抽象类的实现部分),但也有一些区别(抽象方法)。

see http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html 参见http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html

You can instantiate it by constructing an anonymous subclass, like so: 您可以通过构造一个匿名子类来实例化它,如下所示:

KdTree<Integer> tree = new KdTree<Integer>(1,1)
{
    @Override
    public void myAbstractMethodName()
    {
        //do something!
    }
};

Otherwise, you can generate your own implementation: 否则,您可以生成自己的实现:

private class KdTreeSub extends KdTree<Integer>
{
    public KdTreeSub()
    {
        super(1, 1);
    }
}

And later call it 然后叫它

public void myMethod()
{
   ...
   KdTree<Integer> kdtree = new KdTreeSub();
   ...
}

The reason for this is that abstract classes are not complete classes. 这样做的原因是抽象类不是完整的类。 They are missing parts of them, usually a method. 他们缺少它们的一部分,通常是一种方法。 This method is marked with the "abstract" identifier: 此方法标记有“抽象”标识符:

public abstract int read();

The idea behind this is that you can construct a class that handles other parts: 这背后的想法是,您可以构造一个处理其他部分的类:

public byte[] read(int len)
{
    byte[] b = new byte[len];
    for(int i = 0; i < b.length; i++) b[i] = read();
    return b;
}

And simplify creating new classes. 并简化创建新类的过程。

The class, as it stands, was not meant to be instantiated. 就目前而言,该类并不是要实例化的。 It's meant to store boilerplate code for concrete implementations. 它旨在存储用于具体实现的样板代码。 There are 4 of them in your link, starting with WeightedSqrEuclid . 您的链接中有4个,从WeightedSqrEuclid开始。

You can either instantiate those, simply by eg new WeightedSqrEuclid<Integer>(1,1) , or, if you want to profile the general code, write your own class extending KdTree . 您可以通过new WeightedSqrEuclid<Integer>(1,1)实例化这些实例,或者,如果您想分析通用代码,则编写自己的扩展KdTree的类。

However, in the latter case you should either create your subclass in the same file, or change a constructor of KdTree to at least protected . 但是,在后一种情况下,您应该在同一文件中创建子类,或者将KdTree的构造函数更改为至少protected This is because, to create a subclass of this type, you need to call one of the constructors of KdTree in your implementation. 这是因为要创建此类型的子类,您需要在实现中调用KdTree的构造函数之一。

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

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