【发布时间】:2012-03-20 13:59:10
【问题描述】:
我正在学习访问器方法和枚举。我在命名空间“Vehicles”下编写了一个公共类“Car”,并设置了私有属性,例如_manufacturer、_model、_year 和_color。我想写一个方法来访问属性和另一个来设置/更新它们。这是我的课:
using System;
namespace Vehicles
{
public class Car
{
private string _manufacturer;
private string _model;
private string _year;
private string _color;
public void honkHorn()
{
// Add argument for a file name?
// Code here to play a WAV file?
MessageBox.Show("Honk!");
}
public string getCarInfo(string whichProperty)
{
switch (whichProperty)
{
case ("manufacturer"):
return _manufacturer;
case ("model"):
return _model;
case ("year"):
return _year;
case ("color"):
return _color;
default:
return null;
}
}
public void setCarInfo(string whichProperty, string newValue)
{
switch (whichProperty)
{
case ("manufacturer"):
_manufacturer = newValue;
break;
case ("model"):
_model = newValue;
break;
case ("year"):
_year = newValue;
break;
case ("color"):
_color = newValue;
break;
}
}
}
}
这是我的表格:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Vehicles;
namespace CS_Enumeration
{
public partial class Form1 : Form
{
public Car myCar = new Car();
public Form1()
{
InitializeComponent();
myCar.setCarInfo("manufacturer", "Ford");
labelManfValue.Text = myCar.getCarInfo("manufacturer");
myCar.setCarInfo("model", "Ranger");
labelModelValue.Text = myCar.getCarInfo("model");
myCar.setCarInfo("year", "2012");
labelYearValue.Text = myCar.getCarInfo("year");
myCar.setCarInfo("color", "Blue");
labelColorValue.Text = myCar.getCarInfo("color");
}
private void button1_Click(object sender, EventArgs e)
{
myCar.honkHorn();
}
}
}
这真的是编写可以获取/设置的单一方法的最佳方式吗?我首先尝试强制转换与对象属性名称匹配的字符串值并返回实际属性,但这不起作用(除非有人知道如何将字符串转换为对象属性?)。
感谢您的回复。这都是我正在阅读的书中的练习。它甚至说不是所有的东西都应该是公开的,但也不是所有的东西都应该是私有的。那么我怎么知道什么时候应该/不应该公开/私有呢?听起来这本书把我引向了错误的方向,什么是好的编码设计。任何人有任何关于学习良好的 Visual C# 编码设计实践的书籍建议吗?
【问题讨论】:
-
你为什么要这样做?海事组织这是一个可怕的设计 - 只是暴露属性。最重要的是,您在当前方法中失去了所有类型安全性,任何拼写错误都会导致运行时异常
-
查看我的原始帖子的编辑。这一切都基于我正在阅读的 C# 书籍,并且建议使用私有变量。我可以从我必须经历的获取/设置属性中看出这有点荒谬。谢谢。
-
这是什么书?我们想忽略它,并可能嘲笑它。
-
@JohnSaunders Head First C#
-
谢谢。显然,这是头脑优先,而不是大脑优先。
标签: c# enumeration accessor