需要第二眼关注 C# 代码
Need Second Set of Eyes on C# Code
我已经查看了几个小时,但找不到没有任何输出的原因。没有指示错误,因此它在实际代码中。有人可以看看这个并可能发现我没有看到的东西吗?谢谢!
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();
}
}
}
如前所述,在调用基本构造函数的 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 fiddle,您可以在其中查看 运行 并使用它:
https://dotnetfiddle.net/YZUC4O
我已经查看了几个小时,但找不到没有任何输出的原因。没有指示错误,因此它在实际代码中。有人可以看看这个并可能发现我没有看到的东西吗?谢谢!
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();
}
}
}
如前所述,在调用基本构造函数的 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 fiddle,您可以在其中查看 运行 并使用它: https://dotnetfiddle.net/YZUC4O