简体   繁体   中英

Best way to emulate inheritance of static classes

Background : I have a standard code list of string const s (in static classes) in a library that may be shared across several projects, run both internally and externally. (This library has a primary function that is not solely for storing these strings). These codes are standardized and should not change, and a database to store these values is not a useful solution.

One of these lists is colors . A vehicle, for example, may use a basic list of colors that includes things like red, black, blue, grey, etc. A person's hair color can also be any of those standard colors, but has additional options - such as blonde - that do not apply to things that are not hair.

When using this library, I would want to use StandardCodes.Colors.Black for a vehicle and StandardCodes.Person.HairColors.Black for a hair color. Because the string for black is always "BLK - Black" , I don't want to duplicate that multiple times for each class that uses it. And when looking for the hair color black, I would expect to start with StandardCodes.Person.HairColors. and have Black come up as an option. If it did not, I (or another person) would have to know to look in StandardCodes.Colors for it, which is not ideal.

This would seem to be a simple case for the use of inheritance - HairColors , and similar lists like EyeColors would derive from Colors .

Example of this structure:

public static class StandardCodes
{
    public static class Colors
    {
        public const string Black = "BLK - Black";
        public const string Gray  = "GRY - Gray";
        public const string White = "WHI - White";
    }

    public static class Person
    {
        public static class HairColors : Colors
        {
            public const string Blonde = "BLN - Blond or Strawberry";
            public const string Sandy  = "SDY - Sandy";
        }
    }
}

The problem, of course, is that static classes cannot derive from other classes.

Is it best to make these classes not static and hide the constructors? Or is there another, better way of accomplishing this?

You could have a static class at the route of your hierarchy and then create an instance for the properties. Something like this:

public static class StandardCodes
{
    public static Colors Colors = new Colors();
    public static class Person
    {
        public static HairColors HairColors = new HairColors();
    }
}
public class Colors
{
    public readonly string Black = "BLK - Black";
    public readonly string Gray  = "GRY - Gray";
    public readonly string White = "WHI - White";
}

public class HairColors : Colors
{
    public readonly string Blonde = "BLN - Blond or Strawberry";
    public readonly string Sandy  = "SDY - Sandy";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM