我想要根据类的类型拥有具有不同值的相同静态变量。

所以我会

public class Entity
{
     public static Bitmap sprite;

     public void draw(Canvas canvas, int x, int y)
     {
          canvas.drawBitmap(sprite, x, y, null);
     }
}

public class Marine extends Entity
{

}

public class Genestealer extends Entity
{

}

然后在我的主程序中:

Marine.sprite = // Load sprite for all instances of Marine
Genestealer.sprite = // Load sprite for all instances of Genestealer

我不想在类的每个实例中存储相同的精灵。我想要每种类型的课程都有一个。我想继承静态精灵变量和绘制精灵的绘制函数。但我不希望 Genstealer 精灵覆盖 Marine 精灵。

这可能吗?

我该怎么做呢?

有帮助吗?

解决方案

使用抽象方法:

public class Entity
{
     public abstract Bitmap getSprite();

     public void draw(Canvas canvas, int x, int y)
     {
          canvas.drawBitmap(getSprite(), x, y, null);
     }
}

public class Marine extends Entity
{
    public Bitmap getSprite() {
        return /*the sprite*/;
    }
}

如果您愿意,getSprite 返回的精灵可以是静态的。这种方法的好处是:

  • 您不能(轻易)忘记在子类中包含精灵,因为如果您不实现抽象方法,编译器会抱怨。

  • 它很灵活。假设一名海军陆战队员“升级”后看起来应该有所不同。只需更改 Marine 的 getSprite 方法即可将级别考虑在内。

  • 这是此类事情的标准 OO 习惯用法,因此人们在查看代码时不会感到摸不着头脑。

其他提示

这是不可能为它是在超类的静态。在超类中的静态变量是通过所有的子类所共享(有效地一个singleton)。每当用户改变(比如说,通过实例化一个子类),即变化反映在所有其它的子类的实例。

相反,使子画面在子类中静态的,然后使用由劳伦斯所描述的方法的结构。

我有同样的问题,并来到溶液使用静态映射

类 - >对象

下面的代码示例使用整数作为期望的“类静态”变量的类型。

import java.util.Map;
import java.util.HashMap;

class C
{
    static Map<Class<?>,  Integer> class2IntegerMap = new HashMap<Class<?>, Integer>();


    public void setClassSpecificInteger(Integer _i)
    {
        class2IntegerMap.put(this.getClass(), _i);
    }

    public Integer getClassSpecificInteger()
    {
        return class2IntegerMap.get(this.getClass());    
    }           
}

class CA extends C
{
}

class CB extends C
{
}

class CAA extends CA
{
}

public class MainClass
{
    public static void main(String []args)
    {
        CA a1 = new CA();
        CA a2 = new CA();
        CB b1 = new CB();
        CB b2 = new CB();
        CAA aa1 = new CAA();

        a1.setClassSpecificInteger(Integer.valueOf(-1));
        b1.setClassSpecificInteger(Integer.valueOf(+33));

        System.out.println("The int-value for a1 is: "+a1.getClassSpecificInteger());
        System.out.println("The int-value for b1 is: "+b1.getClassSpecificInteger());

        System.out.println("The int-value for aa1 is: "+aa1.getClassSpecificInteger());

        System.out.println("The int-value for a2 is: "+a2.getClassSpecificInteger());
        System.out.println("The int-value for b2 is: "+b2.getClassSpecificInteger());

        CA a3 = new CA();
        CB b3 = new CB();

        System.out.println("The int-value for a3 is: "+a3.getClassSpecificInteger());
        System.out.println("The int-value for b3 is: "+b3.getClassSpecificInteger());

        CAA aa2 = new CAA();

        aa2.setClassSpecificInteger(Integer.valueOf(8));

        System.out.println("The int-value for aa1 now is: "+aa1.getClassSpecificInteger());
    } 
} 

的输出是:

The int-value for a1 is: -1
The int-value for b1 is: 33
The int-value for aa1 is: null
The int-value for a2 is: -1
The int-value for b2 is: 33
The int-value for a3 is: -1
The int-value for b3 is: 33
The int-value for aa1 now is: 8

我希望这可以帮助别人。请的那种。

因此,让一个雪碧给大家的实例变量。他们只是参考;几乎比指针更

一个快速测试会告诉你,是的,你可以在子类覆盖静态变量。

我已经把一个简单的继承结构来测试此。 StaticTest是StaticTestSub的超级。他们都声明静态整型TEST1TEST2TEST3伴有不同程度的访问。为了简化例子,我离开了private版本。

public class StaticTest {
    public static int TEST1 = 1;
    protected static int TEST2 = 1;
    static int TEST3 = 1;

    public static void main(String[] args) {
            System.out.println("StaticTest.TEST1: " + StaticTest.TEST1);
            System.out.println("StaticTest.TEST2: " + StaticTest.TEST2);
            System.out.println("StaticTest.TEST3: " + StaticTest.TEST3);
            System.out.println("StaticTestSub.TEST1: " + StaticTestSub.TEST1);
            System.out.println("StaticTestSub.TEST2: " + StaticTestSub.TEST2);
            System.out.println("StaticTestSub.TEST3: " + StaticTestSub.TEST3);
    }
}


public class StaticTestSub extends StaticTest {
    public static int TEST1 = 2;
    protected static int TEST2 = 2;
    static int TEST3 = 2;
}

您可以在家里尝试这个。所述出放是:

  

StaticTest.TEST1:1,点击   StaticTest.TEST2:1结果   StaticTest.TEST3:1结果   StaticTestSub.TEST1:2结果   StaticTestSub.TEST2:2结果   StaticTestSub.TEST3:2结果

有关您的特定需求,但是,我建议由劳伦斯·贡萨尔维斯采取的办法

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top