Java通用容器类 [英] Java Generic Container Class

查看:97
本文介绍了Java通用容器类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究用Java实现的演化仿真模型,并且遇到了一个我似乎无法解决的关键的面向对象设计问题。问题可以总结如下:

我有一个基本的抽象类Player和两个具体的子类,Signaller和Receiver:

  abstract class Player 
{
Strategy [] strategies;
双重健身;
...
}

类Signaller扩展Player
{
double quality;
....
}

class Receiver extends Player
{
double [] weights;
int selectedChannel;
....
}

现在我需要代表信号器和接收器,我受限于使用数组来存储它们。对于两种群体类型,都有相同的方法,但对于信号传递者群体或接收者群体也有相应的方法。

从概念上来说,我需要像这样:

  abstract class Population 
{
Player [] members;

void mixUpPopulation(){...}
策略[] getMeanStrategies(){...}
double getMeanFitness(){...}
。 ..
}

类SignallerPopulation扩展人口
{
Signaller []成员;
...
}

类ReceiverPopulation扩展人口
{
Receiver []成员;

double [] getChannelPreferences(){...}
...
}

我想到了实现此目的的两种基本方法:


  1. 有如上所述的类层次结构。

    问题:超类中的 Player [] 如何以及<在子类中,code> Signaller [] 或 Receiver [] 是指同一个对象集合吗?


  2. 使基类具有通用性:




  3.   class Population< T扩展播放器> 
    {
    ...
    T [] members =(T [])new Object [popSize];
    }

    问题:如何实现特定方法到每个人口类型?

    我希望你对这些问题有所了解,或者提出解决问题的其他方法。


    <你可以在你的问题中使用设计1,而不是将数组存储在抽象基类中,而是添加一个抽象的受保护方法(例如getMembers())这将在子类中实现,以将实际数组作为玩家数组返回。

    或者,您可以使抽象基类成为泛型,并派生子类适当的类型:

     抽象类Population< T扩展播放器> 
    {
    T []成员;

    void mixUpPopulation(){...}
    策略[] getMeanStrategies(){...}
    double getMeanFitness(){...}
    。 ..
    }

    class SignallerPopulation扩展Population< Signaller>
    {
    public SignallerPopulation(int popSize){members = new Signaller [popSize]; }
    ...
    }

    类ReceiverPopulation扩展Population< Receiver>
    {
    public ReceiverPopulation(int popSize){members = new Receiver [popSize]; }
    double [] getChannelPreferences(){...}
    ...
    }


    I'm working on a evolutionary simulation model implemented in Java and ran into a key object-orientation design issue which I can't seem to work out. The problem can be summarized as follows:

    I have a base abstract class Player and two concrete subclasses, Signaller and Receiver:

    abstract class Player
    {
        Strategy[] strategies;
        double fitness;
        ...
    }
    
    class Signaller extends Player
    {
        double quality;
        ....
    }
    
    class Receiver extends Player
    {
        double[] weights;
        int chosenChannel;
        ....
    }
    

    Now I need classes which represent collections of Signallers and Receivers and I am constrained to using arrays to store them. There are methods common to both population types, but also specific methods for a signaller populations or for a receiver population.

    Conceptually, I would need something like this:

    abstract class Population
    {
        Player[] members;
    
        void mixUpPopulation() {...}
        Strategy[] getMeanStrategies() {...}
        double getMeanFitness() {...}
        ...
    }
    
    class SignallerPopulation extends Population
    {
        Signaller[] members;
        ...
    }
    
    class ReceiverPopulation extends Population
    {
        Receiver[] members;
    
        double[] getChannelPreferences() {...}
        ...
    }
    
    

    I have thought of two basic ways of achieving this:

    1. Have the class hierarchy as described above.
      Problem: How can the Player[] in the superclass and also the Signaller[] or Receiver[] in the subclasses refer to the same collection of objects?

    2. Make the base class generic:

    class Population <T extends Player>
    {
        ...    
        T[] members = (T[])new Object[popSize];
    }
    

    Problem: How do I implement the methods specific to each of the population types?

    I would appreciate your insights into those problems or maybe suggestions of other ways of tackling the problem.

    解决方案

    You can use the design 1 as in your question, but instead of storing an array in the abstract base class you add an abstract protected method (e.g. getMembers()) that will be implemented in the subclasses to return the actual array as an array of Players.

    Alternatively, you can make the abstract base class generic, and derive the subclasses with the appropriate types:

    abstract class Population<T extends Player>
    {
        T[] members;
    
        void mixUpPopulation() {...}
        Strategy[] getMeanStrategies() {...}
        double getMeanFitness() {...}
        ...
    }
    
    class SignallerPopulation extends Population<Signaller>
    {
        public SignallerPopulation(int popSize) { members = new Signaller[popSize]; }
        ...
    }
    
    class ReceiverPopulation extends Population<Receiver>
    {
        public ReceiverPopulation(int popSize) { members = new Receiver[popSize]; }
        double[] getChannelPreferences() {...}
        ...
    }
    

    这篇关于Java通用容器类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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