首页 > 解决方案 > 不同类 C# 中的枚举使用问题

问题描述

class GSM 
{

    private string name;
    private string manufacturer;
    private string owner;
    private double price;

    public string Name  
    {
        get { return this.name; }
    }

    public string Manufacturer
    {
        get { return this.manufacturer; }
    }

    public string Owner
    {
        get { return this.owner; }
        set { this.owner = value; }
    }

    public double Price
    {
        get { return price; }
        set { price = value; }
    }

    public GSM(string name, string manu) 
    {
        this.name = name;
        this.manufacturer = manu;
    }

    public GSM(string name, string manu, string owner, double price, Battery battery, Display display)
    {
        this.name = name;
        this.manufacturer = manu;
        this.owner = owner;
        this.price = price;
    }

    public override string ToString()
    {
        return "Owner: " + Owner + "\nModel: " + Name + "\nManufacturer: " + Manufacturer + "\nPrice: " + Price + "\n";
    }     
}

两种类型的类

class Battery
{
    private ushort hoursIdle;
    private ushort hoursTalk;
    private _BatteryType batteryType;
    
    public _BatteryType BatteryType
    {
        get { return this.batteryType; }
    }

    public enum _BatteryType { LiLon, NiMH, NiCd } 
    
    public ushort HoursIdle 
    {
        get { return hoursIdle; }
    }

    public ushort HoursTalk 
    {
        get { return hoursTalk; }
    }

    public Battery(ushort hoursIdle, ushort hoursTalk, _BatteryType batteryType)
    {
        this.hoursIdle = hoursIdle;
        this.hoursTalk = hoursTalk;
        this.batteryType = batteryType;
    }              
}

我的主要

static void TestDataArray()
{
        GSM test = new GSM("IPhone", "Apple");
        GSM test2 = new GSM("IPhone XI", "Apple", "x", 13000,new Battery(10,5,Battery._BatteryType.LiLon),new Display(20,1000,Display._Display.LCD));
        GSM test3 = new GSM("Samsung Galaxy S20", "Samsung", "y", 11000, new Battery(10, 5, Battery._BatteryType.LiLon), new Display(20, 2000,Display._Display.AMOLED));
        GSM[] MobileArray = new GSM[3] { test,test2,test3 };

        foreach (object o in MobileArray)
        {
            Console.WriteLine(o);
        }
}

问题:我想在 iphone 中获取枚举数据“LCD”,在三星中获取“AMOLED”,无法将其覆盖为字符串方法。试图在 GSM 类中创建一个新实例,但获得了默认值。尝试调用 Battery 类,然后调用枚举列表,但迫使我选择一个值。

标签: c#enums

解决方案


谢谢大家花时间回答我的问题。

问题现已解决!

 class GSM
    {
        private string name;
        private string manufactor;
        private string owner;
        private double price;
        private Battery battery;
        private Display display;

        public Battery Battery
        { 
            get { return this.battery; }
            set { this.battery = value; }
        }
        public Display Display
        {
            get { return this.display; }
            set { this.display = value; }
        }
        public string Name  
        {
            get { return this.name; }
        }
        public string Manufactor
        {
            get { return this.manufactor; }
        }
        public string Owner
        {
            get { return this.owner; }
            set { this.owner = value; }
        }
        public double Price
        {
            get { return price; }
            set { price = value; }
        }
        public GSM(string name, string manu) 
        {
            this.name = name;
            this.manufactor = manu;
            
        }
        public GSM(string name, string manu, string owner, double price,Battery battery,Display display)
        {
            this.name = name;
            this.manufactor = manu;
            this.owner = owner;
            this.price = price;
            this.battery = battery;
            this.display = display;
        }
        public override string ToString()
        {
            return "Owner: " + Owner + "\nModel: " + Name + "\nManufactor: " + Manufactor + "\nPrice: " + Price + "\nSpecification: \nBatteryType: " + battery.BatteryType + "\nDisplayType: " + display.Displays;
        }     

我创建了新字段,因此我的构造函数能够获取这些值。我现在还查看了我的变量名称以澄清问题。非常感谢 akaBase 和 Klaus Gutter


推荐阅读