微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ASP.NET OOP-关联操作符重载的操作_RelationalOperatorOverloading_A

ASP.NET OOP-关联操作符重载的操作_RelationalOperatorOverloading_A



一、首先要在“App_Code / .cs”描述“类”所要的相关程序。



///
/// C# ?面向对象程序设计“关联操作符重载 (RelationalOperatorOverloading) ”的用法 A。
///?
/// 关联的逻辑操作符可以经由自订的类型与运算之后,返回一个 true 或 false 的
/// 运算结果。
///?
/// 此范例将介绍“> <”这种关联操作符重载的操作。
///?
///


//声明自订一般类。
public class RelationalOperatorOverloading_A_Cs
{
? ? //声明公开整数字段。(学生人数)
? ? public int StudentCount_F;

? ? //声明公开成员静态方法。(关联操作符重载)
? ? public static bool operator >(RelationalOperatorOverloading_A_Cs objRelationalOperatorOverloading_A1_Val,RelationalOperatorOverloading_A_Cs objRelationalOperatorOverloading_A2_Val)
? ? {
? ? ? ? //当“学生A1班对象大于学生A2班对象”人数是成立时。
? ? ? ? if (objRelationalOperatorOverloading_A1_Val.StudentCount_F > objRelationalOperatorOverloading_A2_Val.StudentCount_F)
? ? ? ? {
? ? ? ? ? ? //返回结果。
? ? ? ? ? ? return true;
? ? ? ? }
? ? ? ? else //当“学生A1班对象大于学生A2班对象”人数不是成立时。
? ? ? ? {
? ? ? ? ? ? //返回结果。
? ? ? ? ? ? return false;
? ? ? ? }
? ? }

? ? //声明公开成员静态方法。(关联操作符重载)
? ? public static bool operator <(RelationalOperatorOverloading_A_Cs objRelationalOperatorOverloading_A1_Val,RelationalOperatorOverloading_A_Cs objRelationalOperatorOverloading_A2_Val)
? ? {
? ? ? ? //当“学生A1班对象小于学生A2班对象”人数是成立时。
? ? ? ? if (objRelationalOperatorOverloading_A1_Val.StudentCount_F < objRelationalOperatorOverloading_A2_Val.StudentCount_F)
? ? ? ? {
? ? ? ? ? ? //返回结果。
? ? ? ? ? ? return true;
? ? ? ? }
? ? ? ? else //当“学生A1班对象小于学生A2班对象”人数不是成立时。
? ? ? ? {
? ? ? ? ? ? //返回结果。
? ? ? ? ? ? return false;
? ? ? ? }
? ? }

}

二、接下来就可以在“aspx.cs”操作相关“类”所建立的“对象”等属性方法的操作。



using System;

public partial class _RelationalOperatorOverloading_A : System.Web.UI.Page
{
? ? //当 Button 控件按下时所要执行的动作。
? ? protected void Button1_Click(object sender,EventArgs e)
? ? {
? ? ? ? //声明建构对象操作实例。(学生A班)
? ? ? ? RelationalOperatorOverloading_A_Cs objStudent_A = new RelationalOperatorOverloading_A_Cs();
? ? ? ? //声明建构对象操作实例。(学生B班)
? ? ? ? RelationalOperatorOverloading_A_Cs objStudent_B = new RelationalOperatorOverloading_A_Cs();

? ? ? ? //设定学生A班人数。
? ? ? ? objStudent_A.StudentCount_F = 40;
? ? ? ? //设定学生B班人数。
? ? ? ? objStudent_B.StudentCount_F = 20;

? ? ? ? //设定 Label 控件的显示文字。(学生A班学生人数)
? ? ? ? this.Label_Output.Text += "Student_A 班级学生人数:" + objStudent_A.StudentCount_F + "

";

? ? ? ? //设定 Label 控件的显示文字。(学生B班学生人数)
? ? ? ? this.Label_Output.Text += "Student_B 班级学生人数:" + objStudent_B.StudentCount_F + "

";


? ? ? ? //当“学生A班大于学生B班”人数时。
? ? ? ? if (objStudent_A > objStudent_B)
? ? ? ? {
? ? ? ? ? ? //设定 Label 控件的显示文字
? ? ? ? ? ? this.Label_Output.Text += "Student_A 学生人数大于 Student_B 学生人数

";

? ? ? ? }
? ? ? ? else //当“学生A班小于学生B班”人数时。
? ? ? ? {
? ? ? ? ? ? //设定 Label 控件的显示文字
? ? ? ? ? ? this.Label_Output.Text += "Student_A 学生人数小于 Student_B 学生人数

";

? ? ? ? }
? ? ? ??
? ? }
}

三、详细 UI 部分请自行参阅源代码的.aspx 部分,源代码请见 YouTube 说明页的下载连结位置。

原文:大专栏  ASP.NET OOP-关联操作符重载的操作_RelationalOperatorOverloading_A

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐