我对C#编程很陌生,并试图使用"Vihicle“接口属性编写以下程序,这些属性继承在'Car‘、’卡车‘类中。我面临的问题是这个错误:
出现了“System.StackOverflowException”类型的未处理异常“
我在将值赋值给汽车属性时得到了这个结果。这是我的代码:
namespace Inheritance_Assignment_2
{
interface Vihicle
{
string Make
{
get;
set;
}
String Model
{
get;
set;
}
int Year
{
get;
set;
}
void DisplayInfo();
float calculateMileage();
}
class Car : Vihicle
{
private string make;
public string Make // read-write instance property
{
get
{
return Make;
}
set
{
Make = value;
}
}
private string model;
public string Model // read-write instance property
{
get
{
return Model;
}
set
{
Model = value;
}
}
private int year;
public int Year // read-write instance property
{
get
{
return Year;
}
set
{
Year = value;
}
}
public void DisplayInfo()
{
Console.WriteLine(Make);
Console.WriteLine(Model);
Console.WriteLine(Year);
}
public float calculateMileage()
{
Random random = new Random();
float value = random.Next(10, 20);
return value;
}
}
class Truck : Vihicle
{
private string make;
public string Make // read-write instance property
{
get
{
return Make;
}
set
{
Make = value;
}
}
private string model;
public string Model // read-write instance property
{
get
{
return Model;
}
set
{
Model = value;
}
}
private int year;
public int Year // read-write instance property
{
get
{
return Year;
}
set
{
Year = value;
}
}
public void DisplayInfo()
{
Console.WriteLine(Make);
Console.WriteLine(Model);
Console.WriteLine(Year);
}
public float calculateMileage()
{
throw new NotImplementedException();
}
}
class TowingTruck : Truck
{
public string TowingCapacity // read-write instance property
{
get
{
return TowingCapacity;
}
set
{
TowingCapacity = value;
}
}
public void DisplayInfo() // Overrided function of class truck because this function doing some extra printing of
{ //TowingCapacity that is present in this TowingTruck Child of Truck Class
Console.WriteLine(Make);
Console.WriteLine(Model);
Console.WriteLine(Year);
Console.WriteLine(TowingCapacity);
}
public float calculateMileage()
{
throw new NotImplementedException();
}
}
class DeliveryTruck : Truck
{
public string Make // read-write instance property
{
get
{
return Make;
}
set
{
Make = value;
}
}
public string Model // read-write instance property
{
get
{
return Model;
}
set
{
Model = value;
}
}
public int Year // read-write instance property
{
get
{
return Year;
}
set
{
Year = value;
}
}
/*
public void DisplayInfo()
{
Console.WriteLine(Make);
Console.WriteLine(Model);
Console.WriteLine(Year);
}
public float calculateMileage()
{
// throw new NotImplementedException();
return 0;
}
*/
}
class Program
{
static void Main(string[] args)
{
//while (true) // Loop indefinitely
//{
// string name;
// int age;
// double height;
// Console.Write("Enter your name: ");
// name = Console.ReadLine();
// Console.Write("Enter your age: ");
// age = Convert.ToInt32(Console.ReadLine());
// Console.Write("Enter your height: ");
// height = Convert.ToDouble(Console.ReadLine());
// //Print a blank line
// Console.WriteLine();
// //Show the details you typed
// Console.WriteLine( name);
// Console.WriteLine( age);
// Console.WriteLine("Height is ", height);
// Console.WriteLine('\n');
//}
Car C = new Car();
float rnum = C.calculateMileage();
Console.WriteLine("Here is the Milage : " + rnum);
C.Make = System.Console.ReadLine();
System.Console.WriteLine("The employee information:");
System.Console.WriteLine("Employee name: {0}", C.Make);
//Console.Write("Enter your Model : ");
//C.Model = Console.ReadLine();
//Console.WriteLine(C.Model);
//Console.ReadLine();
}
}
}发布于 2014-09-15 12:26:21
看看你的财产:
private string make;
public string Make // read-write instance property
{
get
{
return Make;
}
set
{
Make = value;
}
}当您从Make读取一个值时,它在内部从Make读取一个值(与写入一个值相同),这将导致无限递归。您需要从保存值的变量中读取/写入:
private string make;
public string Make // read-write instance property
{
get
{
return make;
}
set
{
make = value;
}
}属性的内部逻辑不能引用自身。实际上有些东西必须储存价值。
编辑:,除非有任何特殊的理由使用这样的属性(比如在getter/setters中需要更多的逻辑),您可以使用自动生成的属性来简化代码。所以,不是这样的:
private string make;
public string Make // read-write instance property
{
get
{
return make;
}
set
{
make = value;
}
}你可以用这个:
public string Make { get; set; }编译器会自动将后者转换为与前者非常相似的东西(可能只是一个不同的支持变量名)。
https://stackoverflow.com/questions/25848018
复制相似问题