加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-16 09:28:08 所属栏目:asp.Net 来源:网络整理
导读:ASP.NET OOP-关联操作符重载的操作_RelationalOperatorOverloading_A 一、首先要在“App_Code / .cs”描述“类”所要的相关程序。 /// /// C# ?面向对象程序设计“关联操作符重载 (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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读