public class student
{
public int score;
public student(int score)
{
this.score = score;
}
public int getScore()
{
return this.score;
}
public static student operator +(student s1, student s2)
{
return new student(s1.getScore() + s2.getScore());
}
public static student operator -(student s1, student s2)
{
return new student(s1.getScore() - s2.getScore());
}
}
重载方法一定要声明是static 的
调用代码
student s1=new student(50);
student s2=new student(90);
student s3=s1-s2;
student s4 = s1 + s2;
textBox1.Text = s3.getScore().ToString();
评论