lin*_*ida 2 c# string tostring
我有一节课:
class Rect{
int x;
int y;
public Rect(int x, int y){
this.x = x;
this.y = y;
}
}
Run Code Online (Sandbox Code Playgroud)
我希望这种情况发生:
Console.WriteLine(new Rect(12,12));
>>> <Rect with x=12, y=12>
Run Code Online (Sandbox Code Playgroud)
我怎么能这样做?
你可以覆盖ToString()方法:
class Rect{
int x;
int y;
public Rect(int x, int y){
this.x = x;
this.y = y;
}
public override string ToString()
{
return "("+x.ToString()+","+y.ToString()+")";
}
}
Run Code Online (Sandbox Code Playgroud)