且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

[ASK]-关于变量

更新时间:2023-08-31 18:05:16

您的想法看起来不错,而且乍一看有些新鲜.

但是,根据OOP 的"Encapsulation"原理,您应该Encapsulate 您的Classes中的变量和方法.因此,每个类都应封装并拥有该类需要访问和处理的自己的属性(变量)和方法.

软件工程的另一个原则是减少"Coupling".您越减少了系统中组件之间的依赖关系和耦合,代码就越易于管理,可重用和可扩展.现在,如果您有一个专有类来封装变量(如示例中所示),并且如果您使用该类的对象来访问系统中不同区域中的那些变量,那么您将增加系统中的耦合.不同的代码将取决于相同的变量(全局变量的种类),这些全局变量的微小变化将对整个系统产生若干影响.

因此,***仅将相关变量保留在它们所对应的类中.这就是说,每个类都有自己的变量集,这些变量可能取决于上下文,因此外界可能无法访问.
Your idea looks fine and something new at first glance.

However, the "Encapsulation" principle of OOP says, you should Encapsulate your variables and methods within your Classes. So, each class should encapsulate and own their own properties (Variables) and methods that the class needs to access and process.

Another principle of Software Engineering is to Reduce "Coupling". The more you reduce dependency and coupling among the components in your system, the more manageable, reusable and extensible your code is. Now, if you have an exclusive class that encapsulates the variables (Like what you''ve shown in your example) and if you use object of that class to access those variables in different areas in your system, you are increasing coupling in your system. Different codes will be dependent on the same variables (Kind of Global variables) and a small change in these global variables would lead several impact in the entire system.

So, it is best to keep only related variables within the class where they correspond to. That means, each class will have their own set of variables, which may or may not be accessible by outside world, depending upon context.


您不想公开自己的课程级别变量作为公共变量.记住OOP规则,即封装.将所有变量设为私有并提供访问器(请参见此处 [ ^ ]以获得msdn帮助)

You don''t want to expose your class level variables as public. Remember the rule of OOP, encapsulation. Make all your variables private and provide accessors (see here[^] for msdn help)

//Class Variable
class Variables 
{
   private IDictionary<int,> accNum = new Dictionary<int,>();
  //-- other variables...

  public IDictionary<int,> AccNum
      {
        get ;
        set ;
      }
}


好..我会尝试从我认为您要问的问题中回答,

您想使一个方法的范围扩展到其成员变量之外,而扩展到它所包含的整个类..

好的..如果这就是您要问的,那么它很有可能,C#将ref关键字放置在成员参数列表之前,然后它将能够引用超出其范围的变量.所以我将其应用到您的代码中.

ok..I''ll try to answer it from what I think you''re asking about,

You want to make a method have its scope extending beyond its member variables but rather to the whole class it is contained in..

ok..if thats what you''re asking then its very possible, C# has the ref keyword which you''ll place before a members argument list and it will then be able to reference, the variable outside its scope..so I''ll apply it on your code..

class Variables 
{   
    public IDictionary<int, string> accNum = new Dictionary<int, string>();
  //-- other variables...
}

class Process{

Variables vs;  // Just create an instance of the Variable Class

// the ref keyword will open up the scope of the method to the instantiated variable above

public void Process1(ref vs)  
{       
   foreach(KeyValuePair<int, string> pair in vs.accNum)           {     
      //-- processing...    
}  

}  
<p></p>
public void Process2(ref vs)  
{   
 foreach(KeyValuePair<int, string> pair in vs.accNum) 
   {      
      //-- processing...    
   }  
}

}