1

インスタンス クラスがクラス内の変数と等しいかどうかを確認するにはどうすればよいですか?

public class ChainType
{
    public ChainType none;
    public ChainType unknown;
    public ChainType horizontal;
    public ChainType vertical;
    public ChainType centerCross;
    public ChainType leftTopCornerCross;
    public ChainType rightTopCornerCross;
    public ChainType leftBottomCornerCross;
    public ChainType rightBottomCornerCross;
}

public class aClass{
    ChainType chainType = new ChainType();

    someMethod(){
        chainType = getChainType(); // sets to chainType.horizontal

        if(chainType == ChainType.horizontal){..} // getting Error here about object reference to non-static class
    }
}
4

6 に答える 6

1

The tip actually addresses enums, so let's forget about that.

There is a subtile difference between your code and the example. The class name is Bullets, plural. You should change your class name to ChainTypes and you should be fine.

public class ChainTypes
{
    public ChainType none;
    public ChainType unknown;
    public ChainType horizontal;
    public ChainType vertical;
    public ChainType centerCross;
    public ChainType leftTopCornerCross;
    public ChainType rightTopCornerCross;
    public ChainType leftBottomCornerCross;
    public ChainType rightBottomCornerCross;
}

ChainTypes types = new ChainTypes();
ChainType chaintype = getChainType();   
if (chainType == types.horizontal) {
}

I understand the reasoning, but this can be quite tricky in comparisons (the tip doesn't address the usages of the Bullets. Comparison is done on references, so you will have to make ChainTypes static and make sure you always return the correct instance.

于 2013-07-18T07:39:54.063 に答える
1

そのようにしたい場合は、staticのフィールドに修飾子を追加する必要がありChainTypeます。

public class ChainType
{
    public static ChainType none;
    public static ChainType unknown;
    ...
    public static ChainType rightBottomCornerCross;
}

しかし、それがあなたの問題を解決するとは思いません。コンパイルされますが、動作しません。

chainTypeはポインタであり、none、uknown、....も同様です。 chainType に none、unknown、... のいずれかを割り当てた場合にのみ、独自の方法で比較できます。

列挙型の方が優れている必要があります。

public enum ChainType
{
    none,
    unknown,
    ...
}

列挙型に切り替えた後にコードを変更する必要はありません。

編集(「ここでヒント36番に従おうとしています。devmag.org.za/2012/07/12/」)

あなたはこの間違った方法を取得します。ヒント 36 は、配列を使用する代わりに、箇条書きに個別の変数を使用する必要があることを示しています。

Bullet bulletTypeA
Bullet bulletTypeB
...

それ以外の

Bullet[] bullets;
bullets[BulletType.A]
于 2013-07-18T07:33:06.000 に答える
0

I'm not sure if this is the best approach. If you are using these classes to define what something is then I would recommend an enum.

If they are required in some kind of inheritance structure then you should be comparing GetType()'s or typeof(object).

Lastly if you need to test that two objects are the same (as in the same actual thing in your program) then you are missing an initialisation of each of those instance variables and your getChainType should return one of those instances.

于 2013-07-18T07:38:36.557 に答える
0

最初: それらを静的に定義する必要があります:

public class ChainType
{
    public static ChainType none; //STATIC !!
    ... 
}

のようにアクセスできますChainType.horizontal

2番目:次のようなコードを書くという事実に注意してください

if(chainType == ChainType.horizontal){..}

内容ではなく、この 2 つのインスタンスの参照を比較ます。これが期待どおりの場合は、最初の修正に進みます。そうでない場合は、型のカスタム等値比較子も定義する必要があります。

于 2013-07-18T07:34:02.620 に答える
0

この目的のために列挙を使用する必要があります。

public enum ChainType
{
    none,
    unknown,
    horizontal,
    vertical,
    centerCross,
    leftTopCornerCross,
    rightTopCornerCross,
    leftBottomCornerCross,
    rightBottomCornerCross
}

public class aClass{
    ChainType chainType = ChainType.horizontal;

    someMethod(){

        if(chainType == ChainType.horizontal){..} 
    }
}
于 2013-07-18T07:35:11.443 に答える
0

列挙型を使用するように叫んでいるというすべての答えは正しいですが(ベストプラクティスですが)、エラーを見てみましょう。エラーは自明です。クラスの非静的プロパティに直接アクセスしています(クラスはそのオブジェクトではありません)代わりに、chainType.horizo​​ntalを試すことができます(ここでchainTypeはChainTypeクラスのオブジェクトです)。

于 2013-07-18T07:44:36.113 に答える