范例一:
运行结果:
长等于20,宽等于10的长方形的面积等于:-->200
运用重载关系运算符++将长方形面积加10后的长方形面积等于-->210
运用重载关系运算符--将长方形面积减10后的长方形面积等于-->200
Press any key to continue
using System;
namespace ConsoleApplication1
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
Rectangle myrectangle=new Rectangle();
myrectangle.getArea(10,20);
Console.WriteLine("长等于20,宽等于10的长方形的面积等于:-->"+myrectangle.Area);
Console.Write("运用重载关系运算符++将长方形面积加10后的长方形面积等于-->");
myrectangle++;
Console.WriteLine(myrectangle.Area);
Console.Write("运用重载关系运算符--将长方形面积减10后的长方形面积等于-->");
myrectangle--;
Console.WriteLine(myrectangle.Area);
Console.ReadLine();
}
}
class Rectangle
{
int width;
int length;
public int Area;
public void getArea(int w,int l)
{
width=w;
length=l;
Area=width*length;
}
public void InArea()
{
Area+=10;
}
public void Darea(){
Area-=10;
}
public static Rectangle operator ++(Rectangle rectangle)
{
Rectangle myrectangle =new Rectangle();
myrectangle=rectangle;
myrectangle.InArea();
return myrectangle;
}
public static Rectangle operator --(Rectangle rectangle){
Rectangle rect=new Rectangle();
rect=rectangle;
rect.Darea();
return rect;
}
}
}
范例二:
+和-运算符的重载
代码如下
运行结果
长等于20,宽等于10的长方形面积等于-->200
长等于10,宽等于5的长方形面积等于-->50
_______________________________________________
运用重载运算符+将两个长方形面积相加的结果为-->250
运用重载运算符-将两个长方形面积相减的结果为-->150
Press any key to continue
using System;
namespace ConsoleApplication1
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
Rectangle rect1=new Rectangle();
Rectangle rect2=new Rectangle();
rect1.getArea(10,20);
rect2.getArea(5,10);
Console.WriteLine("长等于20,宽等于10的长方形面积等于-->"+rect1.area);
Console.WriteLine("长等于10,宽等于5的长方形面积等于-->"+rect2.area);
Console.WriteLine("_______________________________________________");
Rectangle sum=new Rectangle();
sum.area=rect1.area+rect2.area;
Console.WriteLine("运用重载运算符+将两个长方形面积相加的结果为-->"+sum.area);
Rectangle sub=new Rectangle();
sub.area=rect1.area-rect2.area;
Console.WriteLine("运用重载运算符-将两个长方形面积相减的结果为-->"+sub.area);
Console.ReadLine();
}
}
class Rectangle{
int width;
int length;
public int area;
public void getArea(int w,int l){
width=w;
length=l;
area=width*length;
}
public static Rectangle operator +(Rectangle rect1,Rectangle rect2){
Rectangle sum=new Rectangle();
sum.area=rect1.area+rect2.area;
return sum;
}
public static Rectangle operator-(Rectangle rect1,Rectangle rect2){
Rectangle sub=new Rectangle();
sub.area=rect1.area-rect2.area;
return sub;
}
}
}
范例四
处理不同类型运算
代码如下
运行结果
班级的人数为:--> 20
加上10个学生班级的人数为-->30
减去10个学生班级的人数为-->10
Press any key to continue
using System;
namespace ConsoleApplication1
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
int students=10;
Student stu1=new Student();
Student sum=new Student();
Student sub=new Student();
Console.WriteLine("班级的人数为:--> {0}",stu1.students);
sum=sum+students;
sub=sub-students;
Console.WriteLine("加上10个学生班级的人数为-->{0}",sum.students);
Console.WriteLine("减去10个学生班级的人数为-->{0}",sub.students);
Console.ReadLine();
}
}
class Student{
public int students;
public Student(){
students=20;
}
public static Student operator +(Student s,int num){
int student=s.students+num;
Student stu1=new Student();
stu1.students=student;
return stu1;
}
public static Student operator -(Student s,int num){
int student=s.students-num;
Student stu1=new Student();
stu1.students=student;
return stu1;
}
}
}