我一直在研究这个问题几个小时,但却找不到输出任何内容的原因。没有指出错误,所以它在实际代码中。有人可以看看这个,可能会抓到我没看到的东西吗?谢谢!
namespace ConsoleApplication8
{
class Tape
{
public Tape(int length, int width)
{
len = length;
wid = width;
}
public int len { get; set; }
public int wid { get; set; }
public override string ToString()
{
return string.Format("{0}\nLength: {1}\nWidth: {2}", GetType(), len, wid);
}
}
class VideoTape : Tape
{
public int PlayTime { get; set; }
public VideoTape(int length, int width, int playTime)
: base(len, wid)
{
PlayTime = playTime;
}
public override string ToString()
{
return string.Format("{0}\nPlay Time: {1}", base.ToString(), PlayTime);
}
}
class AdhesiveTape : Tape
{
private int _stickiness;
public AdhesiveTape(int length, int width, int stickiness)
: base(length, width)
{
Stickiness = stickiness;
}
public int Stickiness
{
get { return _stickiness; }
set
{
if (value >= 1 && value <= 10)
_stickiness = value;
else
_stickiness = 0;
}
}
public override string ToString()
{
return string.Format("{0}\nStickiness: {1}",
base.ToString(), (Stickiness == 0) ? "Invalid Input" : Stickiness.ToString());
}
}
class Test
{
static void Main(string[] args)
{
var tape = new Tape(100, 10);
var videoTape = new VideoTape(50, 5, 200);
var adhesiveTape = new AdhesiveTape(500, 8, 8);
Console.WriteLine(tape);
Console.WriteLine(videoTape);
Console.WriteLine(adhesiveTape);
Console.ReadLine();
}
}
}
答案 0 :(得分:1)
如前所述,在VideoTape类型的构造函数中调用基础构造函数时存在参数传递问题,以下代码对我来说很好:
using System;
namespace ConsoleApplication8
{
class Tape
{
public Tape(int length, int width)
{
len = length;
wid = width;
}
public int len { get; set; }
public int wid { get; set; }
public override string ToString()
{
return string.Format("{0}\nLength: {1}\nWidth: {2}", GetType(), len, wid);
}
}
class VideoTape : Tape
{
public int PlayTime { get; set; }
public VideoTape(int length, int width, int playTime)
: base(length, width)
{
PlayTime = playTime;
}
public override string ToString()
{
return string.Format("{0}\nPlay Time: {1}", base.ToString(), PlayTime);
}
}
class AdhesiveTape : Tape
{
private int _stickiness;
public AdhesiveTape(int length, int width, int stickiness)
: base(length, width)
{
Stickiness = stickiness;
}
public int Stickiness
{
get { return _stickiness; }
set
{
if (value >= 1 && value <= 10)
_stickiness = value;
else
_stickiness = 0;
}
}
public override string ToString()
{
return string.Format("{0}\nStickiness: {1}",
base.ToString(), (Stickiness == 0) ? "Invalid Input" : Stickiness.ToString());
}
}
class Test
{
static void Main(string[] args)
{
var tape = new Tape(100, 10);
var videoTape = new VideoTape(50, 5, 200);
var adhesiveTape = new AdhesiveTape(500, 8, 8);
Console.WriteLine(tape);
Console.WriteLine(videoTape);
Console.WriteLine(adhesiveTape);
Console.ReadLine();
}
}
}
已更改:
public VideoTape(int length, int width, int playTime)
: base(len, wid)
要:
public VideoTape(int length, int width, int playTime)
: base(length, width)
len和wid不是VideoTape构造函数的参数,因此无法传递给基础构造函数
这是一个.net小提琴,你可以看到它在运行并玩弄它: https://dotnetfiddle.net/YZUC4O