c#构造函数未被调用
本文关键字:调用 构造函数 | 更新日期: 2023-09-27 18:15:25
我的代码只是打印:
welcome to battleship
,但船舶坐标或船舶创建甚至最后一句"Press any key to exit"
我的怀疑去构造函数,看起来它没有调用,但为什么我的程序是计数和不崩溃?
我在Mac上用Xamarin Studio运行代码。
代码:using System;
using System.Collections.Generic;
namespace battleship
{
class MainClass
{
public static void Main(string[] args)
{
Console.WriteLine("welcome to battleship");
EnumShipClassification.ShipClassification cruiser = EnumShipClassification.ShipClassification.theCruiser;
Coordinate startCoordinate = new Coordinate(5, 5);
EnumDirection.Direction direction = EnumDirection.Direction.west;
Ship ship = new Ship(cruiser, startCoordinate, direction);
Console.WriteLine("ship created"); // @debug
Console.WriteLine("all coords: " + ship.coordinates);
//@end
Console.WriteLine("Press any key to exit.");
Console.ReadLine();
}
}
public class Ship
{
public int size; // values from wikipedia
public int amount; // values from wikipedia
public List<Coordinate> coordinates = new List<Coordinate>();
public EnumShipClassification.ShipClassification classification;
public Ship(EnumShipClassification.ShipClassification classification, Coordinate startCoordinate, EnumDirection.Direction toDirection)
{
Console.WriteLine("Ship Constructor called "); // @debug
this.classification = classification;
Console.WriteLine("classification added"); // @debug
switch (classification)
{
case EnumShipClassification.ShipClassification.theBattleship: size = 5; amount = 1; break;
case EnumShipClassification.ShipClassification.theCruiser: size = 4; amount = 2; break;
case EnumShipClassification.ShipClassification.theDestroyer: size = 3; amount = 3; break;
case EnumShipClassification.ShipClassification.theSubmarine: size = 2; amount = 4; break;
default: break;
}
Console.WriteLine("switch ended with size {0} and amout {1}", size, amount);
for (int i = 0; i < size; i++)
{
coordinates.Add(startCoordinate.advancedBy(i, toDirection));
Console.WriteLine("i is: " + i); // @debug
}
}
}
public class EnumDirection
{
public enum Direction
{
north, south, east, west // + all (Direction[] array with content [.n, .s, .e, .w]
}
public Direction[] all = { Direction.north, Direction.south, Direction.east, Direction.west };
}
public class EnumShipClassification
{
public enum ShipClassification // numbers: BoatSize
{
theBattleship = 5, theCruiser = 4, theDestroyer = 3, theSubmarine = 2
}
}
public class Coordinate
{
public int row;
public int column;
private int fieldSize = 10;
public void init()
{
row = 0;
column = 0;
}
public Coordinate invalid = new Coordinate(-1, -1);
public Coordinate(int row, int column)
{
this.row = row;
this.column = column;
}
public Coordinate getRandomCoordinate(int betweenX, int Y)
{
Random r = new Random(fieldSize);
return new Coordinate(r.Next(), r.Next());
}
public Coordinate neighbor(EnumDirection.Direction inDirection)
{
return advancedBy(1, inDirection: inDirection);
}
public Coordinate advancedBy(int displacement, EnumDirection.Direction inDirection)
{
switch (inDirection)
{
case EnumDirection.Direction.north: return new Coordinate(column: column, row: row + displacement);
case EnumDirection.Direction.south: return new Coordinate(column: column, row: row - displacement);
case EnumDirection.Direction.east: return new Coordinate(column: column + displacement, row: row);
case EnumDirection.Direction.west: return new Coordinate(column: column - displacement, row: row);
default: break; // never happens
}
return null; // never happens
}
public string description
{
get
{
return "column: " + column + ", row: " + row;
}
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
try
{
Coordinate _obj = (Coordinate)obj;
return _obj.row == this.row && _obj.column == this.column;
}
catch
{
return false;
}
}
public override int GetHashCode()
{
return 1; // TODO: return a real value
}
public static bool operator ==(Coordinate lhs, Coordinate rhs)
{
return lhs.row == rhs.row && lhs.column == rhs.column;
}
public static bool operator !=(Coordinate lhs, Coordinate rhs)
{
return lhs.row != rhs.row && lhs.column != rhs.column;
}
}
变化
public Coordinate invalid = new Coordinate(-1, -1);
public static Coordinate invalid = new Coordinate(-1, -1);
public Coordinate invalid = new Coordinate(-1, -1);
导致堆栈溢出。那是因为new Coordinate在Coordinate类中初始化所以每次都必须创建新坐标。将字段设置为静态,以便创建一次,并可用于所有实例。
如果不希望更改引用,也可以添加readyonly
关键字。
public readonly static Coordinate invalid = new Coordinate(-1, -1);