c# - 不同类 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 类,然后调用枚举列表,但迫使我选择一个值。
解决方案
谢谢大家花时间回答我的问题。
问题现已解决!
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
推荐阅读
- r - 如何将制表符分隔的数据(不同格式)解析为 data.table/data.frame?
- python - 在python中使用正则表达式查找包含特定单词的部分文本
- css - 如何使用 CSS 突出显示非空表格单元格
- python - python中字典中多个变量的最有效方法是什么?
- sql - 新学习者的简单查询
- css - 为什么导航链接中的文本没有出现?
- html - 如何显示用户定义的表单字段数?
- node.js - 如何通过 Websocket 发送文件以及其他信息?
- java - 如何使我的 .jar 资源文件指向正确的目录
- node.js - 使用 AWS Cognito 和 AD FS 作为 Web 应用程序的身份验证