1

インターフェイスOptionsSetと抽象クラスがありStringBasedOptionsSetます。

interface OptionsSet {

    string getString(string key);

    int getInt(string key);

}

abstract class StringBasedOptionsSet : OptionsSet {

    int getInt(string key) {
        string v = getString(key);
        try {
            return Convert.ToInt32(v);
        }
        catch (Exception exc) {
            if (exc is FormatException || exc is OverflowException) {
                return 0;
            }
            throw;
        }
    }

}

getString(string)からメソッドを呼び出せないのはなぜStringBasedOptionSet.getInt(string)ですか?

csmade/StringBasedOptionsSet.cs(21,36): error CS0103: The name `getString' does not exist in the current context
csmade/StringBasedOptionsSet.cs(32,25): error CS0103: The name `getString' does not exist in the current context

OptionsSet.getString(key)私も呼び出しを試みbase.getString(key)ましたが、非静的メソッド/オブジェクトに getStringエラーの定義が含まれていません。

編集:インターフェイスをStringBasedOptionsSet実装していないのはOptionsSet、例を削除する際の間違いでした。実際のコードにインターフェイスを実装しています。

4

3 に答える 3

1

StringBasedOptionsSet はインターフェイスを実装しないため、クラスは次のようになります。

interface OptionsSet {

    string getString(string key);

    int getInt(string key);

}

abstract class StringBasedOptionsSet : OptionsSet {

    int getInt(string key) {
        string v = getString(key);
        try {
            return Convert.ToInt32(v);
        }
        catch (Exception exc) {
            if (exc is FormatException || exc is OverflowException) {
                return 0;
            }
            throw;
        }
    }

}

また、抽象クラスで関数を再度定義する必要がある場合もあります。

于 2013-10-04T13:15:35.527 に答える