为私有对象属性创建访问器方法-枚举



我正在学习访问器方法和枚举。我在命名空间"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#的良好编码设计实践有什么建议吗?

不要这样做。

相反,使用公共属性可以获得类型安全性和类的更具表现力的用法。在您当前的方法中,属性名称字符串中的任何拼写错误都将导致运行时异常,而不是编译错误。

仅使用属性:

public class Car
{
   public string Manufacturer {get; set;}
   public string Model {get; set;}
   public string Year {get; set;}
   public string Color {get; set;}
  //..
}

现在您可以直接访问属性:

myCar.Manufacturer  = "Ford";
labelManfValue.Text = myCar.Manufacturer;

此外,您应该定义一个构造函数来完全初始化Car对象,否则您可能会设置一些属性,而其他属性则不会。

您可以使用反射来完成此操作:

void Main()
{
    var foo = new Foo();
    foo.Set("bar","test");
    Console.WriteLine(foo.Get("bar"));
}
class Foo
{
    string bar;
    string bop;
    public void Set(string name, string value)
    {
        GetType().GetField(name, BindingFlags.NonPublic|BindingFlags.Instance)
                 .SetValue(this, value);
    }
    public string Get(string name)
    {
        return (string)GetType().GetField(name, BindingFlags.NonPublic|BindingFlags.Instance)
                                .GetValue(this);
    }
}

但这是个非常糟糕的主意。

最新更新