外部类问题 [英] External class problem

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

问题描述

阅读代码注释...........................



我尝试过:



 //外部课程
公共课程DCPS
{
public string pin1_Sign =+;
public string pin2_Sign = - ;
public double pin1_Volts = 3;
public double pin2_Volts = 0;

public void Pin1Contact(string pinXsign,double pinXvolts)
{
pinXsign = pin1_Sign; pinXvolts = pin1_Volts;
}
}







 public UserControl1()
{
InitializeComponent();
//这不起作用
//led.pin1_Sign不会更改为+而
//led.pin1_Volts不会更改为3
dcps.Pin1Contact(led .pin1_Sign,led.pin1_Volts); // ?????
led.pin1_Sign = dcps.pin1_Sign; //(测试)这工作正常
}
DCPowerSupply1.DCPS dcps = new DCPS();

解决方案

没有。它不会。

这是因为除非另有说明,所有在C#中传递的参数都是按值传递的,而不是通过引用传递的。



解释,想一想:

  public   void  TimesTwo( int  x)
{
x = x + x;
}
...
int value = 666 ;
TimesTwo( value );
Console.WriteLine( value );

如果值是通过引用传递的(即方法中的x与变量相同)在方法之外的值)然后控制台将打印1332。



但是......如果我这样打电话怎么办:

 TimesTwo( 666 ); 
Console.WriteLine( 666 );

您希望控制台显示什么? 1332或666?

很明显,我们希望控制台打印666,因为否则常量不再是常数,我们的其余代码将无法遵循!



所以C#按值传递,而不是参考:变量内容的副本被传递并传递给方法。因为TimesTwo正在改变副本,它不会影响外部世界,并且常量保持不变。



字符串也是如此:字符串的副本传递给方法,你的代码修改该副本,当方法退出时,副本被丢弃。

除了...带字符串,它更糟糕,因为字符串是不可变的:一旦你在C#中创建一个字符串,它根本无法更改 - 每次尝试时,都会创建一个新字符串,然后它们会并排存在。



如果你想传回一个值,你可以......但你必须在创建和使用方法时指定:

  public   void  Pin1Contact( out   string  pinXsign, out   double  pinXvolts)
{
pinXsign = pin1_Sign; pinXvolts = pin1_Volts;
}



 dcps.Pin1Contact( out  led.pin1_Sign , out  led.pin1_Volts); 



这告诉系统按引用返回参数,而不是按值返回。

你也可以使用 ref 关键字,系统也可以让你访问方法中的当前值:

  public   void  Pin1Contact( ref   string  pinXsign, ref   double  pinXvolts)
{
pinXsign + = pin1_Sign; pinXvolts + = pin1_Volts;
}



 dcps.Pin1Contact( ref  led.pin1_Sign , ref  led.pin1_Volts); 


您无法更改以这种方式领导。您只是更改本地参数变量的值。你的课程设计和用法是错误的。应该使用 Pin1Contact 方法来更改对象变量的值,而不是某些外部变量的值。它应该是这样的:

  public   class  DCPS 
{
public string pin1_Sign = +;
public string pin2_Sign = - ;
public double pin1_Volts = 3 ;
public double pin2_Volts = 0 ;

public void Pin1Contact( string pinXsign, double pinXvolts)
{
pin1_Sign = pinXsign;
pin1_Volts = pinXvolts
}
}

led.Pin1Contact( - 5 0 );


Read the code comments ...........................

What I have tried:

//external class
public class DCPS
{
    public string pin1_Sign = "+";
    public string pin2_Sign = "-";
    public double pin1_Volts = 3;
    public double pin2_Volts = 0;

    public void Pin1Contact(string pinXsign, double pinXvolts)
    {
        pinXsign = pin1_Sign; pinXvolts = pin1_Volts;
    }
}




public UserControl1()
{
    InitializeComponent();
    //this is NOT working
    //led.pin1_Sign is NOT changing to "+" and
    //led.pin1_Volts is NOT changing to 3
  dcps.Pin1Contact(led.pin1_Sign,led.pin1_Volts); //?????
    led.pin1_Sign = dcps.pin1_Sign; //(test) this is working fine
}
DCPowerSupply1.DCPS dcps = new DCPS();

解决方案

No. It won't.
That's because all parameters passed in C# unless otherwise specified are passed by value, not by reference.

To explain, think about this:

public void TimesTwo(int x)
   {
   x = x + x;
   }
...
   int value = 666;
   TimesTwo(value);
   Console.WriteLine(value);

If value was passed by reference (i.e. the x inside the method was the same variable as value outside the method) then the console would print "1332".

But ... what if I called it like this:

TimesTwo(666);
Console.WriteLine(666);

What would you expect the console to show? "1332" or "666"?
Fairly obviously, we want the console to print "666" because otherwise constants are no longer constant, and the rest of our code is going to become impossible to follow!

So C# passes by value, not reference: a copy of the variable content is made and is passed to the method. Because TimesTwo is changing the copy, it doesn't affect the outside world, and constants remain the same.

The same thing happens with strings: a copy of the string is passed to the method, and your code "modifies" that copy, and the copy is discarded when the method exits.
Except ... with strings it's even worse, because strings are immutable: once you create a string in C#, it can't be changed at all - every time you try, you create a new string, and they both then exist side by side.

If you want to pass a value back, you can .. but you have to specify that when you both create and use the method:

public void Pin1Contact(out string pinXsign, out double pinXvolts)
    {
        pinXsign = pin1_Sign; pinXvolts = pin1_Volts;
    }


dcps.Pin1Contact(out led.pin1_Sign, out led.pin1_Volts);


This tells the system to return parameters by reference, rather than by value.
YOu can also use the ref keyword instead, and the system will let you access the current value in the method as well:

public void Pin1Contact(ref string pinXsign, ref double pinXvolts)
    {
        pinXsign += pin1_Sign; pinXvolts += pin1_Volts;
    }


dcps.Pin1Contact(ref led.pin1_Sign, ref led.pin1_Volts);


You cannot changing the values of led in that way. You are merely changing the values of the local parameter variables. Your class design and usage is wrong. The Pin1Contact method should be used to change the values of the object's variables, not some external ones. It should be like:

public class DCPS
{
    public string pin1_Sign = "+";
    public string pin2_Sign = "-";
    public double pin1_Volts = 3;
    public double pin2_Volts = 0;

    public void Pin1Contact(string pinXsign, double pinXvolts)
    {
        pin1_Sign = pinXsign;
        pin1_Volts = pinXvolts
    }
}

led.Pin1Contact("-", 5.0);


这篇关于外部类问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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