Членские переопределения переменной C# используются методом базового класса

Вот, возможно, наивный C# implementation:-

public class ProperCaseHelper {
  public string ToProperCase(string input) {
    string ret = string.Empty;

    var words = input.Split(' ');

    for (int i = 0; i < words.Length; ++i) {
      ret += wordToProperCase(words[i]);
      if (i < words.Length - 1) ret += " ";
    }

    return ret;
  }

  private string wordToProperCase(string word) {
    if (string.IsNullOrEmpty(word)) return word;

    // Standard case
    string ret = capitaliseFirstLetter(word);

    // Special cases:
    ret = properSuffix(ret, "'");
    ret = properSuffix(ret, ".");
    ret = properSuffix(ret, "Mc");
    ret = properSuffix(ret, "Mac");

    return ret;
  }

  private string properSuffix(string word, string prefix) {
    if(string.IsNullOrEmpty(word)) return word;

    string lowerWord = word.ToLower(), lowerPrefix = prefix.ToLower();
    if (!lowerWord.Contains(lowerPrefix)) return word;

    int index = lowerWord.IndexOf(lowerPrefix);

    // If the search string is at the end of the word ignore.
    if (index + prefix.Length == word.Length) return word;

    return word.Substring(0, index) + prefix +
      capitaliseFirstLetter(word.Substring(index + prefix.Length));
  }

  private string capitaliseFirstLetter(string word) {
    return char.ToUpper(word[0]) + word.Substring(1).ToLower();
  }
}
5
задан 10 July 2009 в 23:44
поделиться

4 ответа

Is there any reason you can't use a virtual property? That would provide exactly the functionality you are looking for. It just wouldn't be a field.

protected abstract string[] array { get; }

...

protected override string[] array { get { return new string[]{"...","..."}; }}
8
ответ дан 13 December 2019 в 05:40
поделиться

Why do you need to override the variable? Looking from your code, just setting the values would be enough, no?

Plus, static variables are tied to the class (not the instance), therefore it's not overridable on any situation.

1
ответ дан 13 December 2019 в 05:40
поделиться
class BaseClass
{
    public virtual string Method()
    {
        return string.Empty;
    }
}

abstract class BaseClass<T> : BaseClass where T : BaseClass<T>
{
    protected static string[] strings;

    public override string Method()
    {
        return string.Join("  ", strings);
    }
}

class Subclass1 : BaseClass<Subclass1>
{
    static Subclass1()
    {
        strings = new[] { "class1value1", "class1value2", "class1value3" };
    }
}

class Subclass2 : BaseClass<Subclass2>
{
    static Subclass2()
    {
        strings = new[] { "class2value1", "class2value2", "class2value3" };
    }
}

Важной частью является общий параметр T , который в основном функционирует как индекс для строковых массивов.

1
ответ дан 13 December 2019 в 05:40
поделиться

Simply don't use new. Set the array in your subclass' constructor.

EDIT: with code:

class subclass1 : baseclass
{
    public subclass1()
    {
        array = new string[]
        {
            "class1value1",
            "class1value2",
            "class1value3",
            "class1value4"
        };
    }
}

class subclass2 : baseclass
{
    public subclass2()
    {
        array = new string[]
        {
            "class2value1",
            "class2value2",
            "class2value3",
            "class2value4"
        };
    }
}
1
ответ дан 13 December 2019 в 05:40
поделиться
Другие вопросы по тегам:

Похожие вопросы: