我对此并不陌生,我会逐步按照说明进行操作,但是由于某种原因,请继续获取stackoverflowexception。我究竟做错了什么?
using System;
using System.Collections.Generic;
using System.Linq;
namespace assignment3
{
class changeValue
{
//Create a class called changeValue that declares 2 integer class variables: value1 and
// value2. These should be declared as public and you should not use automatic properties
// to declare them.
public int value1
{
get
{
return value1;
}//end get
set
{
value1 = value;
}//end set
}
public int value2
{
get
{
return value2;
}//end get
set
{
value2 = value;
}//end set
}
public changeValue(int val1, int val2)
{
//here is the constructor where you code the if statements
int value1 = val1;
int value2 = val2;
if (value1 > 5)
{
value1 = val1;
}
if (val1 <= 5)
{
value1 = (val1+val2);
}
if (val2 < 10)
{
value2 = (val2 * val2 + 5);
}
if (val2 >= 10)
{
value2 = val2;
}
}
public void printit()
{
//here is the printit method used to print the results
Console.WriteLine("The calculated value is:" + (value1 * value2));
}
}
class assignment3
{
public static void Main(string[] args)
{
//declare the local val1 and val2 integer variables
int val1;
int val2;
//prompt the user for input of two integers
//don’t forget to convert from the string input to integer
Console.Write("Enter an integer value: "); //obtain user input
val1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter a second integer value: "); //obtain user input
val2 = Convert.ToInt32(Console.ReadLine());
//instantiate a changeValue object here
changeValue myValue = new changeValue(val1,val2);
myValue.printit();//call the object method printit here
}
}
}
您创建的内容本质上是循环引用。您的获取器和设置器需要支持者字段。
private int _value1;
private int _value2;
public int Value1
{
get
{
return _value1;
}//end get
set
{
_value1= value;
}//end set
}
public int Value2
{
get
{
return _value2;
}//end get
set
{
_value2 = value;
}//end set
}
通过设置value1 = value的方式,您已经创建了一个无限循环。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句