?? rectangle.cs
字號:
using System;
class Rectangle
{
private int iHeight;
private int iWidth;
// 缺省構造函數
public Rectangle()
{
Height=0;
Width=0;
}
// 構造函數重載
public Rectangle(int w,int h)
{
Width=w;
Height=h;
}
// 屬性:寬 的get和set訪問器
public int Width
{
get
{
return iWidth;
}
set
{
iWidth=value;
}
}
// 屬性:高 的get和set訪問器
public int Height
{
get
{
return iHeight;
}
set
{
iHeight=value;
}
}
// 屬性:面積 的get訪問器
public int Area
{
get
{
return Height*Width;
}
}
// 重載操作符 ==
public static bool operator==(Rectangle a,Rectangle b)
{
return ((a.Height==b.Height)&&(a.Width==b.Width));
}
// 重載操作符 !=
public static bool operator!=(Rectangle a,Rectangle b)
{
return !(a==b);
}
// 重載操作符 >
public static bool operator>(Rectangle a,Rectangle b)
{
return a.Area>b.Area;
}
// 重載操作符 <
public static bool operator<(Rectangle a,Rectangle b)
{
return !(a>b);
}
// 重載操作符 >=
public static bool operator>=(Rectangle a,Rectangle b)
{
return (a>b)||(a==b);
}
// 重載操作符 <=
public static bool operator<=(Rectangle a,Rectangle b)
{
return (a<b)||(a==b);
}
// 重載ToString
public override String ToString()
{
return "高=" + Height + ",寬=" + Width;
}
public static void Main()
{
// 實例化三個矩形并設置各自屬性
Rectangle objRect1 =new Rectangle();
Rectangle objRect2 =new Rectangle();
Rectangle objRect3 =new Rectangle(10,15);
objRect1.Height=15;
objRect1.Width=10;
objRect2.Height=25;
objRect2.Width=10;
// 打印三個矩形的信息
// 其中調用了ToString方法
Console.WriteLine("矩形#1 " + objRect1);
Console.WriteLine("矩形#2 " + objRect2);
Console.WriteLine("矩形#3 " + objRect3);
// 使用重載后的操作符比較矩形并打印結果
if(objRect1==objRect2)
{
Console.WriteLine("矩形#1和矩形#2高和寬都相等");
}
else
{
if(objRect1>objRect2)
{
Console.WriteLine("矩形1大于矩形2");
}
else
{
Console.WriteLine("矩形1小于矩形2");
}
}
if(objRect1==objRect3)
{
Console.WriteLine("矩形1和矩形3高和寬都相等");
}
else
{
Console.WriteLine("矩形1和矩形3不相等");
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -