自定类型转换
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class CustomConversion
{
static void Main()
{
Rectangle r = new Rectangle(50, 50);
Console.WriteLine(r.ToString());
r.Draw();
Square s = new Square(6);
Console.WriteLine(s.ToString());
s.Draw();
//将矩形显示强制转换为正方形
s =(Square)r;
Console.WriteLine(s.ToString());
s.Draw();
Console.ReadLine();
}
}
//矩形
public struct Rectangle
{
public Rectangle(int Widht, int Heiht)
{
this.widht = Widht;
this.height = Heiht;
}
private int widht;
public int Widht
{
get { return widht; }
set { widht = value; }
}
private int height;
public int Height
{
get { return height; }
set { height = value; }
}
public override string ToString()
{
return string.Format("[{0},{1}]", this.widht, this.height);
}
public void Draw()
{
for (int i = 0; i < this.height; i++)
{
for (int j = 0; j < this.widht; j++)
{
if (i == 0 || i == this.height - 1)
{
Console.Write("-");
}
else
{
if (j == 0 || j == this.widht - 1)
{
Console.Write("|");
}
else
{
Console.Write(" ");
}
}
}
Console.WriteLine();
}
}
}
//正方形
public struct Square
{
private float lenght;
public float Lenght
{
get { return lenght; }
set { lenght = value; }
}
public Square(float length)
{
this.lenght = length;
}
public void Draw()
{
for (int i = 0; i < this.lenght; i++)
{
for (int j = 0; j < this.lenght; j++)
{
if (i == 0 || i == this.lenght - 1)
{
Console.Write("-");
}
else
{
if (j == 0 || j == this.lenght - 1)
{
Console.Write("|");
}
else
{
Console.Write(" ");
}
}
}
Console.WriteLine();
}//end of for
}//end of for
//实现矩形可显示强制转换为正方形
public static explicit operator Square(Rectangle r)
{
Square s;
s.lenght = r.Height;
return s;
}
public override string ToString()
{
return string.Format("Length:{0}",this.lenght);
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class CustomConversion
{
static void Main()
{
Rectangle r = new Rectangle(50, 50);
Console.WriteLine(r.ToString());
r.Draw();
Square s = new Square(6);
Console.WriteLine(s.ToString());
s.Draw();
//将矩形显示强制转换为正方形
s =(Square)r;
Console.WriteLine(s.ToString());
s.Draw();
Console.ReadLine();
}
}
//矩形
public struct Rectangle
{
public Rectangle(int Widht, int Heiht)
{
this.widht = Widht;
this.height = Heiht;
}
private int widht;
public int Widht
{
get { return widht; }
set { widht = value; }
}
private int height;
public int Height
{
get { return height; }
set { height = value; }
}
public override string ToString()
{
return string.Format("[{0},{1}]", this.widht, this.height);
}
public void Draw()
{
for (int i = 0; i < this.height; i++)
{
for (int j = 0; j < this.widht; j++)
{
if (i == 0 || i == this.height - 1)
{
Console.Write("-");
}
else
{
if (j == 0 || j == this.widht - 1)
{
Console.Write("|");
}
else
{
Console.Write(" ");
}
}
}
Console.WriteLine();
}
}
}
//正方形
public struct Square
{
private float lenght;
public float Lenght
{
get { return lenght; }
set { lenght = value; }
}
public Square(float length)
{
this.lenght = length;
}
public void Draw()
{
for (int i = 0; i < this.lenght; i++)
{
for (int j = 0; j < this.lenght; j++)
{
if (i == 0 || i == this.lenght - 1)
{
Console.Write("-");
}
else
{
if (j == 0 || j == this.lenght - 1)
{
Console.Write("|");
}
else
{
Console.Write(" ");
}
}
}
Console.WriteLine();
}//end of for
}//end of for
//实现矩形可显示强制转换为正方形
public static explicit operator Square(Rectangle r)
{
Square s;
s.lenght = r.Height;
return s;
}
public override string ToString()
{
return string.Format("Length:{0}",this.lenght);
}
}
}