如何在模板类中实例化 T 类型 [英] how to instantiate T type inside a template class

查看:88
本文介绍了如何在模板类中实例化 T 类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个抽象类 abstract class MathFunc 用于在它们的特定类中实现数学函数 Rastrigin、Griewangk 和 Rosenbrock final class Rastrigin extends MathFuncfinal class Griewangk extends MathFuncfinal class Rosenbrock extends MatjFunc.

I have an abstract class abstract class MathFunc that is used to implement the mathematical functions Rastrigin, Griewangk and Rosenbrock in their specific classes final class Rastrigin extends MathFunc, final class Griewangk extends MathFunc and final class Rosenbrock extends MatjFunc.

到目前为止,这种类的层次结构以及它们的定义方式完美无缺,所以我认为没有必要在这方面寻求改进.

This hierarchy of classes and how they are defined so far works flawlessly so I think there is no need to look for improvements on this area.

现在我必须实现另一个类 Generation ,其中将包含一些 ArrayLists 但问题是我需要为上述每个数学函数都有一个 Generation 实现.所以我需要类似的东西:

Now i have to implement another class Generation that will have some ArrayLists in it but the thing is I need to have a Generation implementation for each mathematical function described above. So that i need something like :

ArrayList<Rastrigin> rast = new ArrayList<>();
ArrayList<Griewangk> grie = new ArrayList<>();
ArrayList<Rosenbrock> rose = new ArrayList<>();

在这些列表中,我有一些需要实例化的 Rastrigin/Griewangk/Rosenbrock 对象.

Inside these lists I have some Rastrigin/Griewangk/Rosenbrock objects that I need to instantiate.

我从过去的 C++ 项目中知道我可以使用模板来指定通用数据类型,我认为这是我的解决方案.到目前为止,我的实现如下所示:

I know from the past projects in C++ that I can use templates to specify a generic data type and I'm thinking that this is my solution. My implementation so far looks like this :

public class Generation <MathFunc> {
    private final ArrayList<MathFunc> pop = new ArrayList<>();
    private final ArrayList<MathFunc> nextpop = new ArrayList<>();
    private final ArrayList<MathFunc> Crossover = new ArrayList<>();

    Generation(MathFunc tp)
    {
        for(int i = 0; i < PopSize; i++)
        {
            pop.add(tp);
        }
    }
}

但问题是:我可以使用抽象类来实例化依赖它的对象吗?还有另一种方法吗?我对模板的用法很困惑.

But the problem is: can i use the abstract class to instantiate the objects that rely on it ? Is there another way to do it ? I'm quite confused about the usage of templates.

但是做 Generation(Mathfunc tp) 对我来说似乎有点奇怪,因为抽象类是不可实例化的.

But doing Generation(Mathfunc tp) seems a little weird to me since abstract classes are not instantiable.

附言 似乎是一个可能的解决方案,但我不太确定或者它实际上是如何工作的.

P.S. This seems like a possible solution but i'm very not sure about it or how it actually works.

推荐答案

如果您想重用 MathFunc 的同一个实例,我看不出您的代码有任何重大缺陷(除了Generation 的通用定义应该是 Generation).

If you want to reuse the same instance of a MathFunc I don't see any major flaws in your code (except that the generic definition of Generation should be Generation<T extends MathFunc).

所以首先像这样改变你的类:

So first change your class like this:

public class Generation <T extends MathFunc> {
  private final ArrayList<T> pop = new ArrayList<>();
  private final ArrayList<T> nextpop = new ArrayList<>();
  private final ArrayList<T> Crossover = new ArrayList<>();

  Generation(T tp)  {
    for(int i = 0; i < PopSize; i++) {
        pop.add(tp);
    }
  }
}

然后像这样实例化Generation:

Generation<Rastrigin> rastriginGen = new Generation<>(new Rastrigin());
Generation<Griewangk> griewangkGen = new Generation<>(new Griewangk());
Generation<Rosenbrock> rosenbrockGen = new Generation<>(new Rosenbrock());

如果您想在 Generation 中使用 MathFunc 的多个实例,您可能需要使用工厂模式.

If you want to use multiple instances of a MathFunc inside Generation you might want to use a factory pattern instead.

工厂界面:

interface MathFuncFuncFactory<T extends MathFunc> {
  T createFunc();
}

然后添加实现,如RastriginFactory实现MathFuncFuncFactory

内部世代:

Generation(MathFuncFactory<T> factory) {
  for(int i = 0; i < PopSize; i++) {
        pop.add(factory.createFunc());
    }
}

构造函数调用:

Generation<Rastrigin> rastriginGen = new Generation<>(new RastriginFactory());

这篇关于如何在模板类中实例化 T 类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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