的方式来处理的全局变量中的一类 [英] The way to handle global variables in a class

查看:72
本文介绍了的方式来处理的全局变量中的一类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个PHP这样的代码,将其转换为C#(PHP函数这里的一部分)。

have a php code like this,(part of the php function here)going to convert it in to C#.

function jaktDate2()
{
    Global $nameofselectbox,$startYear,$endYear,$year,
    $startDate,$endDate,$startMounth,$endMounth,$startDay,$endDay;
    $today = getdate();
    $year=$today['year'];
    $mounth=$today['mon'];
    $day=$today['mday'];



我的C#代码尝试是这样的,

my C# code try is this,

  public class HuntingDate
    {
        public string StartYear;
        public string EndYear;
        public string Year;
        public DateTime StartDate;
        public DateTime EndDate;
        public string StartMonth;
        public string EndMonth;
        public DateTime StartDay;
        public DateTime EndDay;


    }



我开始这样..是这样正确吗?我应该怎么做下一步怎么办?

I'm starting this way.. is this way correct? What should i do next ?

推荐答案

你定义的类似乎更像是价值,而不是像一个随机抓斗袋一个良好定义的类。 ?怎么所有这些值实际上代表

The class you're defining seems more like a random grab-bag of values and less like a well-defined class. What do all of these values actually represent?

public string StartYear;
public string EndYear;
public string Year;
public DateTime StartDate;
public DateTime EndDate;
public string StartMonth;
public string EndMonth;
public DateTime StartDay;
public DateTime EndDay;

例如,之间有什么区别开始日期朝九特派?什么是 StartYear StartMonth ?这是什么类实际上的确定的?它看起来像你想的的DateTime 值分解成组件。您不必这样做。一个简单的的DateTime 值足够存储必要的信息,你可以直接从该值获得的组件:

For example, what is the difference between StartDate and StartDay? What are StartYear and StartMonth? What is this class actually defining? It looks like you're trying to break up the DateTime values into components. You don't need to do that. A simple DateTime value will sufficiently store the necessary information, and you can get the components directly from that value:

public DateTime StartDate;
public DateTime EndDate;

如果你需要知道的月份,比如,你可以从价值得到它:

If you need to know the month, for example, you can get it from that value:

myObject.StartDate.Month;

要继续完善这个类,你要使用属性,而不是公共成员。在C#中,这应该是这样的:

To continue to improve the class, you'll want to use properties instead of public members. In C#, those would look like this:

public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }



这是特意叫自动实现的属性。他们是编译器速记全属性:

These are specifically called auto-implemented properties. They're compiler short-hand for full properties:

private DateTime _startDate;
public DateTime StartDate
{
    get { return _startDate; }
    set { _startDate = value; }
}
// repeat for EndDate



性能的好处是该类被暴露有关其内部结构的少一点。如果你需要任何逻辑添加到类(如检查日期的具体范围,如确保没有开始日期是在过去的),那么你可以把它添加到性能不破坏类的二进制兼容性。所以耗时代码永远不会需要知道其中的差别。例如:

The benefit of properties is that the class is exposing a little less about its internal structure. And if you need to add any logic to the class (such as checking specific bounds of the date, like making sure no StartDate is in the past) then you can add it to the properties without breaking the binary compatibility of the class. So consuming code never needs to know the difference. For example:

private DateTime _startDate;
public DateTime StartDate
{
    get { return _startDate; }
    set
    {
        if (value < DateTime.Now)
            throw new ArgumentException(string.Format("Start Date must not be in the past: {0}", value.ToString()));
        _startDate = value;
    }
}

您可以继续通过继续进一步采取这种定义这个类的行为。即使具有这些特性暴露仍使类比多了一个数据结构的对象。 (有关数据进一步读/反对反对称性,我建议清洁守则由罗伯特·马丁)一个适当的面向对象设计的目标是为隐藏其数据,并揭露了这些数据在内部执行状态的操作方法的对象

You can continue to take this even further by continuing to define the behavior of this class. Even having these properties exposed still makes the class more of a "data structure" than an "object." (For further reading on data/object anti-symmetry, I recommend Clean Code by Robert Martin.) The goal for a proper object-oriented design would be for the object to hide its data and expose methods that internally perform stateful actions on that data.

例如,如果您需要按每天延长结束日期,你可以这样做:

For example, if you needed to extend EndDate by a day, you could do this:

myObject.EndDate += new TimeSpan(1, 0, 0, 0);



但是,一个更加面向对象的方法(用粘出来,不问校长)会告诉对象本身延长时间,而不是直接告诉它的数据延长时间(从而要求,其在此过程中的数据,这无疑也违反法律德米特的对象):

But a more object-oriented approach (sticking with the "tell, don't ask" principal) would be to tell the object itself to extend the time, not directly tell its data to extend the time (thereby "asking" the object for its data in the process, which arguably also violates the Law Of Demeter):

myObject.ExtendEndDate(new TimeSpan(1, 0, 0, 0));



甚至是:

or even:

myObject.ExtendEndDateInDays(1);

您将只需要实现的目标上这样的方法,而该方法将的内部的延长结束日期的值。

You would just need to implement such a method on the object, and that method would internally extend the value of EndDate.

类本身应该封装所有的功能所必需的概念它代表。如果绝对必要,可以提供公共读取访问其内部成员,但是从设计的角度来看这是值得问自己真正需要的是对对象的数据,并为这些查询更有针对性的方法,而不是直接访问取得什么样的查询对类的数据成员。

The class itself should encapsulate all of the functionality necessary for the concept it represents. It can provide public read access to its internal members if absolutely necessary, but from a design perspective it's worth asking yourself what kind of queries really need to be made against the object's data and to provide more targeted methods for those queries, rather than direct access to the class' data members.

这篇关于的方式来处理的全局变量中的一类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆