C# OOP 任务问题
C# OOP Task problems
好的。我设法将所有内容压缩为 2 个问题:
1/ public 枚举应该在 class 中还是之外?我认为它们都有效,但是有什么好的做法吗?
2/ 我不明白如何设置一个包含来自另一个 class 的对象的构造函数。见底部评论 "Main" class.
GSM CLASS:
using System.Text;
class GSM
{
public string model;
public string manufacturer;
public decimal price;
public string owner;
Battery battery = new Battery("Nokia", 7, 5);
Display display = new Display(12.5, 3);
// CONSTRUCTORS:
public GSM(string model, string manufacturer, decimal price, string owner)
{
this.model = model;
this.manufacturer = manufacturer;
this.price = price;
this.owner = owner;
}
public GSM(string model, string manufacturer, decimal price, string owner, Battery battery, Display display)
: this(model, manufacturer, price, owner)
{
this.battery = battery;
this.display = display;
}
}
电池
public enum BatteryType // Is this suppose to be here or inside the class?
{
LiIon, LiPo, NiMH, NiCd
}
class Battery
{
//battery characteristics
private string model;
private int hoursIdle;
private int hoursTalk;
private BatteryType batteryType = new BatteryType();
}
======
显示
class Display
{
//display characteristics
private double size;
private int numberOfColors;
// CONSTRUCTORS:
public Display(double size, int numberOfColors)
{
this.size = size;
this.numberOfColors = numberOfColors;
}
}
====主要内容:
class GSMTest
{
public static void Main()
{
GSM myGSM = new GSM("Sony ERcs", "Sony ERRR", 124.56m, "Pesho", BatteryType.LiPo, 12.3);
// I can't create this object. Argument5: cannont convert from GSM.BatteryType to GSM.Battery. What gives!?
// Display has 2 fields. I have an instance of it in GSM. Yet I don't know how to set it here so I can create myGSM
}
}
P.S。它们都来自同一个命名空间; class Battery 也有一个构造函数。忘记加了,不知道有没有必要
关于在 class
中嵌入 enum
,最好将 enum
视为一种特殊的 class
(在幕后,这是)。通过这种方式,仅在创建 nested class
.
的类似情况下嵌入 enum
执行此操作时出现的语法问题很简单——如果不显式引用外部类型,就无法从外部类型外部引用嵌入类型。由于 using
只能应用于名称空间(别名除外),如果不进行额外的工作,它不会缩短语法,因此请考虑是否让外部调用者在代码中引用 BatteryType
更有意义
var t = Battery.BatteryType.LiIon
或
var t = BatteryType.NiCd
至于在构造函数中引用类型,当构造函数采用特定类型的对象时,您必须传递该类型的对象或从该类型派生的对象。因此,创建其他变量可能会有用
Battery bat = new Battery();
Display disp = new Display(12.3, 256);
GSM myGSM = new GSM("Sony ERcs", "Sony ERRR", 124.56m, "Pesho", bat, disp);
此外,您可以嵌套对 new
的调用,就像任何其他调用一样
GSM myGSM = new GSM(
"Sony ERcs",
"Sony ERRR",
124.56m,
"Pesho",
new Battery(),
new Display(12.3, 256));
好的。我设法将所有内容压缩为 2 个问题:
1/ public 枚举应该在 class 中还是之外?我认为它们都有效,但是有什么好的做法吗?
2/ 我不明白如何设置一个包含来自另一个 class 的对象的构造函数。见底部评论 "Main" class.
GSM CLASS:
using System.Text;
class GSM
{
public string model;
public string manufacturer;
public decimal price;
public string owner;
Battery battery = new Battery("Nokia", 7, 5);
Display display = new Display(12.5, 3);
// CONSTRUCTORS:
public GSM(string model, string manufacturer, decimal price, string owner)
{
this.model = model;
this.manufacturer = manufacturer;
this.price = price;
this.owner = owner;
}
public GSM(string model, string manufacturer, decimal price, string owner, Battery battery, Display display)
: this(model, manufacturer, price, owner)
{
this.battery = battery;
this.display = display;
}
}
电池
public enum BatteryType // Is this suppose to be here or inside the class?
{
LiIon, LiPo, NiMH, NiCd
}
class Battery
{
//battery characteristics
private string model;
private int hoursIdle;
private int hoursTalk;
private BatteryType batteryType = new BatteryType();
}
====== 显示
class Display
{
//display characteristics
private double size;
private int numberOfColors;
// CONSTRUCTORS:
public Display(double size, int numberOfColors)
{
this.size = size;
this.numberOfColors = numberOfColors;
}
}
====主要内容:
class GSMTest
{
public static void Main()
{
GSM myGSM = new GSM("Sony ERcs", "Sony ERRR", 124.56m, "Pesho", BatteryType.LiPo, 12.3);
// I can't create this object. Argument5: cannont convert from GSM.BatteryType to GSM.Battery. What gives!?
// Display has 2 fields. I have an instance of it in GSM. Yet I don't know how to set it here so I can create myGSM
}
}
P.S。它们都来自同一个命名空间; class Battery 也有一个构造函数。忘记加了,不知道有没有必要
关于在 class
中嵌入 enum
,最好将 enum
视为一种特殊的 class
(在幕后,这是)。通过这种方式,仅在创建 nested class
.
enum
执行此操作时出现的语法问题很简单——如果不显式引用外部类型,就无法从外部类型外部引用嵌入类型。由于 using
只能应用于名称空间(别名除外),如果不进行额外的工作,它不会缩短语法,因此请考虑是否让外部调用者在代码中引用 BatteryType
更有意义
var t = Battery.BatteryType.LiIon
或
var t = BatteryType.NiCd
至于在构造函数中引用类型,当构造函数采用特定类型的对象时,您必须传递该类型的对象或从该类型派生的对象。因此,创建其他变量可能会有用
Battery bat = new Battery();
Display disp = new Display(12.3, 256);
GSM myGSM = new GSM("Sony ERcs", "Sony ERRR", 124.56m, "Pesho", bat, disp);
此外,您可以嵌套对 new
的调用,就像任何其他调用一样
GSM myGSM = new GSM(
"Sony ERcs",
"Sony ERRR",
124.56m,
"Pesho",
new Battery(),
new Display(12.3, 256));