妙用Object

  当你在写C#程序时,经常会用到“ToString()”这个方法,而且如果你细心你点就会发现所有的引用类型都含有“ToString()”这个方法,那么你知道为什么会这样吗?很简单,因为所有的引用类型都继承自Object这个类,而“ToString”这个方法便是Object的一个成员,所以所有的引用类型都拥有“ToString()”这个成员。接下来,我们将通过几段代码来深入理解Object的作用。

1.Object的成员:

方法 返回类型 虚拟 静态 说明
Object() N/A System.Object类型的构造函数,由派生类的构造函数自动调用
~Object()(也称为Finalize()) N/A System.Object类型的析构函数,由派生类的析构函数调用,不能手动调用
Equals(object) bool 把调用该方法的对象与另一对象相比,如果它们相等,就返回true。默认的实现代码会查看对象参数是否引用了同一个对象。如果想以不同的方式来比较对象,则可以重写该方法,例如,比较两个对象的状态。
Equals(object,object) bool 这个方法比较传送给它的两个对象,看看它们是否相等。检查时使用了 Equals(object) 方法。注意,如果两个对象都是空引用,这个方法就返回true。
ReferenceEquals(objcet,object) bool 这个方法比较传送给它的两个对象,看看它们是不是同一个实例的引用。
ToString() String 返回一个对应于对象实例的字符串。默认情况下,这是一个类类型限定名称,但可以重写它,给类类型提供合适的实现方式。
MemberwiseClone() object 通过创建一个新对象实例并复制成员,以复制该对象。成员不会得到这些成员的新实例。新对象的任何引用类型成员都将引用与源类型形同的对象,这个方法是受保护的,所以只能在类或派生的类中使用。
GetType() System.Type 以System.Type对对象的形式返回对象的额类型
GetHashCode() int 在需要次参数的地方,用作对象的散列函数,它返回一个以压缩形似标识的对象的值

2.object与自定义类型:

  所有的类都继承自object类,我们自己写的类同样如此(如果你没有显示的指明基类,编译器会默认为object)。所以我们编写一些类时,可以利用object已经提供的方法。比如我们常用的“ToString”方法,通过重写,可以现实不同的字符串的版本等。object也可用于向上转型与向下转型

3.Object在事件处理时的妙用:

  在写事件处理程序时,我们通常会这样写: public void Btn_Click(object sender,EventArgs e){}  ,第一个参数总是 object类型,为什么要这样定义?要弄懂问题,首先要知道这个“sender”所表示的含义:sender指的是调用这个事件处理程序的对象。而这样定义的好处可以通过下面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gRhEjOkWGaTkKODBM/5MgIDTkyQkOOjNCQIyM05MgIzXOODCMy/wOZcppxwfOc1gAAAABJRU5ErkJggg==" alt="" />

建立一个winForm窗体应用程序,项目名为Calculate,下面为完整源代码(已测试):

//项目文件 Calculate.Designer.cs
namespace Sample_Windows
{
partial class Calculate
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region Windows 窗体设计器生成的代码 /// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btn9 = new System.Windows.Forms.Button();
this.btn8 = new System.Windows.Forms.Button();
this.btn7 = new System.Windows.Forms.Button();
this.btn6 = new System.Windows.Forms.Button();
this.btn5 = new System.Windows.Forms.Button();
this.btn4 = new System.Windows.Forms.Button();
this.btn3 = new System.Windows.Forms.Button();
this.btn2 = new System.Windows.Forms.Button();
this.btn1 = new System.Windows.Forms.Button();
this.btn0 = new System.Windows.Forms.Button();
this.btnAdd = new System.Windows.Forms.Button();
this.btnDiv = new System.Windows.Forms.Button();
this.btnMul = new System.Windows.Forms.Button();
this.btnSub = new System.Windows.Forms.Button();
this.btnClear = new System.Windows.Forms.Button();
this.resultBox = new System.Windows.Forms.TextBox();
this.btnResult = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// btn9
//
this.btn9.Location = new System.Drawing.Point(, );
this.btn9.Name = "btn9";
this.btn9.Size = new System.Drawing.Size(, );
this.btn9.TabIndex = ;
this.btn9.Text = "";
this.btn9.UseVisualStyleBackColor = true;
this.btn9.Click += new System.EventHandler(this.btn9_Click);
//
// btn8
//
this.btn8.Location = new System.Drawing.Point(, );
this.btn8.Name = "btn8";
this.btn8.Size = new System.Drawing.Size(, );
this.btn8.TabIndex = ;
this.btn8.Text = "";
this.btn8.UseVisualStyleBackColor = true;
this.btn8.Click += new System.EventHandler(this.btn8_Click);
//
// btn7
//
this.btn7.Location = new System.Drawing.Point(, );
this.btn7.Name = "btn7";
this.btn7.Size = new System.Drawing.Size(, );
this.btn7.TabIndex = ;
this.btn7.Text = "";
this.btn7.UseVisualStyleBackColor = true;
this.btn7.Click += new System.EventHandler(this.btn7_Click);
//
// btn6
//
this.btn6.Location = new System.Drawing.Point(, );
this.btn6.Name = "btn6";
this.btn6.Size = new System.Drawing.Size(, );
this.btn6.TabIndex = ;
this.btn6.Text = "";
this.btn6.UseVisualStyleBackColor = true;
this.btn6.Click += new System.EventHandler(this.btn6_Click);
//
// btn5
//
this.btn5.Location = new System.Drawing.Point(, );
this.btn5.Name = "btn5";
this.btn5.Size = new System.Drawing.Size(, );
this.btn5.TabIndex = ;
this.btn5.Text = "";
this.btn5.UseVisualStyleBackColor = true;
this.btn5.Click += new System.EventHandler(this.btn5_Click);
//
// btn4
//
this.btn4.Location = new System.Drawing.Point(, );
this.btn4.Name = "btn4";
this.btn4.Size = new System.Drawing.Size(, );
this.btn4.TabIndex = ;
this.btn4.Text = "";
this.btn4.UseVisualStyleBackColor = true;
this.btn4.Click += new System.EventHandler(this.btn4_Click);
//
// btn3
//
this.btn3.Location = new System.Drawing.Point(, );
this.btn3.Name = "btn3";
this.btn3.Size = new System.Drawing.Size(, );
this.btn3.TabIndex = ;
this.btn3.Text = "";
this.btn3.UseVisualStyleBackColor = true;
this.btn3.Click += new System.EventHandler(this.btn3_Click);
//
// btn2
//
this.btn2.Location = new System.Drawing.Point(, );
this.btn2.Name = "btn2";
this.btn2.Size = new System.Drawing.Size(, );
this.btn2.TabIndex = ;
this.btn2.Text = "";
this.btn2.UseVisualStyleBackColor = true;
this.btn2.Click += new System.EventHandler(this.btn2_Click);
//
// btn1
//
this.btn1.Location = new System.Drawing.Point(, );
this.btn1.Name = "btn1";
this.btn1.Size = new System.Drawing.Size(, );
this.btn1.TabIndex = ;
this.btn1.Text = "";
this.btn1.UseVisualStyleBackColor = true;
this.btn1.Click += new System.EventHandler(this.btn1_Click);
//
// btn0
//
this.btn0.Location = new System.Drawing.Point(, );
this.btn0.Name = "btn0";
this.btn0.Size = new System.Drawing.Size(, );
this.btn0.TabIndex = ;
this.btn0.Text = "";
this.btn0.UseVisualStyleBackColor = true;
this.btn0.Click += new System.EventHandler(this.btn0_Click);
//
// btnAdd
//
this.btnAdd.Location = new System.Drawing.Point(, );
this.btnAdd.Name = "btnAdd";
this.btnAdd.Size = new System.Drawing.Size(, );
this.btnAdd.TabIndex = ;
this.btnAdd.Text = "+";
this.btnAdd.UseVisualStyleBackColor = true;
this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
//
// btnDiv
//
this.btnDiv.Location = new System.Drawing.Point(, );
this.btnDiv.Name = "btnDiv";
this.btnDiv.Size = new System.Drawing.Size(, );
this.btnDiv.TabIndex = ;
this.btnDiv.Text = "-";
this.btnDiv.UseVisualStyleBackColor = true;
this.btnDiv.Click += new System.EventHandler(this.btnDiv_Click);
//
// btnMul
//
this.btnMul.Location = new System.Drawing.Point(, );
this.btnMul.Name = "btnMul";
this.btnMul.Size = new System.Drawing.Size(, );
this.btnMul.TabIndex = ;
this.btnMul.Text = "*";
this.btnMul.UseVisualStyleBackColor = true;
this.btnMul.Click += new System.EventHandler(this.btnMul_Click);
//
// btnSub
//
this.btnSub.Location = new System.Drawing.Point(, );
this.btnSub.Name = "btnSub";
this.btnSub.Size = new System.Drawing.Size(, );
this.btnSub.TabIndex = ;
this.btnSub.Text = "/";
this.btnSub.UseVisualStyleBackColor = true;
this.btnSub.Click += new System.EventHandler(this.btnSub_Click);
//
// btnClear
//
this.btnClear.Location = new System.Drawing.Point(, );
this.btnClear.Name = "btnClear";
this.btnClear.Size = new System.Drawing.Size(, );
this.btnClear.TabIndex = ;
this.btnClear.Text = "C";
this.btnClear.UseVisualStyleBackColor = true;
this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
//
// resultBox
//
this.resultBox.Location = new System.Drawing.Point(, );
this.resultBox.Name = "resultBox";
this.resultBox.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
this.resultBox.Size = new System.Drawing.Size(, );
this.resultBox.TabIndex = ;
this.resultBox.Text = "";
//
// btnResult
//
this.btnResult.Location = new System.Drawing.Point(, );
this.btnResult.Name = "btnResult";
this.btnResult.Size = new System.Drawing.Size(, );
this.btnResult.TabIndex = ;
this.btnResult.Text = "=";
this.btnResult.UseVisualStyleBackColor = true;
this.btnResult.Click += new System.EventHandler(this.btnResult_Click);
//
// Calculate
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(, );
this.Controls.Add(this.btnResult);
this.Controls.Add(this.resultBox);
this.Controls.Add(this.btnClear);
this.Controls.Add(this.btnSub);
this.Controls.Add(this.btnMul);
this.Controls.Add(this.btnDiv);
this.Controls.Add(this.btnAdd);
this.Controls.Add(this.btn0);
this.Controls.Add(this.btn1);
this.Controls.Add(this.btn2);
this.Controls.Add(this.btn3);
this.Controls.Add(this.btn4);
this.Controls.Add(this.btn5);
this.Controls.Add(this.btn6);
this.Controls.Add(this.btn7);
this.Controls.Add(this.btn8);
this.Controls.Add(this.btn9);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
this.Name = "Calculate";
this.Text = "Calculate";
this.ResumeLayout(false);
this.PerformLayout(); } #endregion private System.Windows.Forms.Button btn9;
private System.Windows.Forms.Button btn8;
private System.Windows.Forms.Button btn7;
private System.Windows.Forms.Button btn6;
private System.Windows.Forms.Button btn5;
private System.Windows.Forms.Button btn4;
private System.Windows.Forms.Button btn3;
private System.Windows.Forms.Button btn2;
private System.Windows.Forms.Button btn1;
private System.Windows.Forms.Button btn0;
private System.Windows.Forms.Button btnAdd;
private System.Windows.Forms.Button btnDiv;
private System.Windows.Forms.Button btnMul;
private System.Windows.Forms.Button btnSub;
private System.Windows.Forms.Button btnClear;
private System.Windows.Forms.TextBox resultBox;
private System.Windows.Forms.Button btnResult;
}
}
//项目文件 Calculate.cs
using System;
using System.Windows.Forms; namespace Sample_Windows
{
public partial class Calculate : Form
{
public Calculate()
{
InitializeComponent();
} private string _firstNumber = String.Empty;
private string _secondNumber = String.Empty;
private string _operation = "+"; private bool _flag = false; private void btn9_Click(object sender, EventArgs e)
{
if (!_flag)
{
_firstNumber += btn9.Text;
          resultBox.Text=_firstNumber;
}
else
{
_secondNumber += btn9.Text;
         resultBox.Text = secondNumber;
}
}
......
......//此处沈略部分代码(btn8_Click,btn7_Click,btn6_Click...)
......
     private void btn0_Click(object sender, EventArgs e)
{
if (!_flag)
{
_firstNumber += btn0.Text;
         resultBox.Text=_firstNumber;
}
else
{
_secondNumber += btn0.Text;
         resultBox.Text=_secondNumber;
}
} private void btnResult_Click(object sender, EventArgs e)
{
if (_firstNumber != String.Empty && _secondNumber != String.Empty)
{
double a = Convert.ToDouble(_firstNumber);
double b = Convert.ToDouble(_secondNumber); switch (_operation)
{
case "+":
resultBox.Text = Convert.ToString(a + b);
break;
case "-":
resultBox.Text = Convert.ToString(a - b);
break;
case "*":
resultBox.Text = Convert.ToString(a*b);
break;
case "/":
resultBox.Text = Convert.ToString(a/b);
break;
}
}
else
{
MessageBox.Show("Error!!!","Warning",MessageBoxButtons.OK,MessageBoxIcon.Warning);
}
} private void btnClear_Click(object sender, EventArgs e)
{
resultBox.Text = "";
_flag = false;
} private void btnAdd_Click(object sender, EventArgs e)
{
_operation = "+";
resultBox.Text = _operation;
_flag = !_flag;
} private void btnDiv_Click(object sender, EventArgs e)
{
_operation = "-";
resultBox.Text = _operation;
_flag = !_flag;
} private void btnMul_Click(object sender, EventArgs e)
{
_operation = "*";
resultBox.Text = _operation;
_flag = !_flag;
} private void btnSub_Click(object sender, EventArgs e)
{
_operation = "/";
resultBox.Text = _operation;
_flag = !_flag;
}
}
}

这是一个简单的计算器程序,在上面的代码中,共有16个事件处理程序,而其中10个数字处理程序与4个操作符处理程。看以看出代码很长,修改修改起来也很麻烦。接下来看一下下面的代码:

//项目文件 Calculate.Designer.cs
...
this.btn0.Click += new System.EventHandler(this.btn_Click);this.btn2.Click += new System.EventHandler(this.btn_Click);
this.btn3.Click += new System.EventHandler(this.btn_Click);
this.btn4.Click += new System.EventHandler(this.btn_Click);
this.btn5.Click += new System.EventHandler(this.btn_Click);
...
this.btnSub.Click += new System.EventHandler(this.btnOperation_Click); ... //项目文件 Calculate.cs
using System;
using System.Windows.Forms; namespace Sample_Windows
{
public partial class Calculate : Form
{
public Calculate()
{
InitializeComponent();
} private string _firstNumber = String.Empty;
private string _secondNumber = String.Empty;
private string _operation = "+"; private bool _flag = false; private void btn_Click(object sender, EventArgs e)
{
Button btn = (Button) sender; //获取触发此事件处理程序的Button对象
if (!_flag)
{
_firstNumber += btn.Text;
resultBox.Text = _firstNumber;
}
else
{
_secondNumber += btn.Text;
resultBox.Text = _secondNumber;
}
} private void btnResult_Click(object sender, EventArgs e)
{
if (_firstNumber != String.Empty && _secondNumber != String.Empty)
{
double a = Convert.ToDouble(_firstNumber);
double b = Convert.ToDouble(_secondNumber);
_firstNumber = String.Empty;
_secondNumber = String.Empty; switch (_operation)
{
case "+":
resultBox.Text = Convert.ToString(a + b);
break;
case "-":
resultBox.Text = Convert.ToString(a - b);
break;
case "*":
resultBox.Text = Convert.ToString(a*b);
break;
case "/":
resultBox.Text = Convert.ToString(a/b);
break;
}
}
else
{
MessageBox.Show("Error!!!","Warning",MessageBoxButtons.OK,MessageBoxIcon.Warning);
}
} private void btnOperation_Click(object sender, EventArgs e)
{
Button btn = (Button) sender; //获取触发此事件处理程序的Button对象
_operation = btn.Text;
resultBox.Text = _operation;
_flag = !_flag;
} private void btnClear_Click(object sender, EventArgs e)
{
_flag = false;
resultBox.Text = ""; }
}
}

这段代码与上面要实现的功能完全一样,但只有4个时间处理程序,一个数字处理程序与一个字符处理程序,很明显,代码明显的精简的不少,而且修改起来也更加方便。从上面的例子可以看出使用object与不使用object有多么大的不同。

总结:

(1)在写事件处理程序时,可以将事件处理程序按事件的属性分类(如上面例子中的数字处理,操作处理,计算处理),再通过object这个类来实现用一个事件处理程序处理同一类事件。这样既避免了代码的重复,又增加了可读性和易维护性!

(2)object可以实现控件之间的交互,因为所有类都继承自object,所以通过多态性,可以将控件与object的引用绑定,以便于在不同的方法间传递,再通过向下转型解绑,就可以轻松实现控件之间的交互。

妙用Object的更多相关文章

  1. console中一些不常用的实用方法

    console.group('分组1'); console.table( [ {key1: 1,key2: 2,key3: 3}, {key1: 1,key2: 2,key3: 3}, {key1: ...

  2. js 基础拓展

    1.关于 try catch 的用法 <body> <div>请输出一个 5 到 10 之间的数字:</div> <input id="demo&q ...

  3. Tuple<int, int> Dictionary<string, object>妙用

    Tuple<int, int> Dictionary<string, object>妙用

  4. TestNG中的DataProvider返回Iterator<Object[]>的妙用

    TestNG中使用DataProvider有个好处,就是: 1. 在执行用例的时候dataProvider迭代中的每组数据都是作为一个用例执行 2. 在测试数据有规律的情况下,免去了添加testXML ...

  5. 【优雅代码】深入浅出 妙用Javascript中apply、call、bind

    这篇文章实在是很难下笔,因为网上相关文章不胜枚举. 巧合的是前些天看到阮老师的一篇文章的一句话: “对我来说,博客首先是一种知识管理工具,其次才是传播工具.我的技术文章,主要用来整理我还不懂的知识.我 ...

  6. 【javascript 技巧】Array.prototype.slice的妙用

    Array.prototype.slice的妙用 开门见山,关于Array 的slice的用法可以参考这里 http://www.w3school.com.cn/js/jsref_slice_arra ...

  7. 妙味课堂——HTML+CSS基础笔记

    妙味课堂的课程讲得非常的清楚,受益匪浅.先把HTML和CSS基础课程部分视频的学习笔记记录如下: padding #PS基础 ##前端需要的PS技能 - PS技能(前端需要):切图.修图.测量 - P ...

  8. (转)深入浅出 妙用Javascript中apply、call、bind

    原文连接 深入浅出 妙用Javascript中apply.call.bind 网上文章虽多,大多复制粘贴,且晦涩难懂,我希望能够通过这篇文章,能够清晰的提升对apply.call.bind的认识,并且 ...

  9. View.setTag(key,object)异常:The key must be an application-specific resource id.

    07-11 13:43:26.184: E/AndroidRuntime(10229): FATAL EXCEPTION: main07-11 13:43:26.184: E/AndroidRunti ...

随机推荐

  1. Windows 2008 远程登陆时提示"要登录到此远程计算机,您必须被授予允许通过终端登录登录的权限"

    ECS Win2008 远程时提示"要登录到此远程计算机,您必须被授予允许通过终端登录登录的权限"的解决方法   问题描述 ECS Windows 2008 远程登陆时提示&quo ...

  2. ASP.NET Core 中文文档 第四章 MVC(2.2)模型验证【转载】

    http://www.cnblogs.com/dotNETCoreSG/p/aspnetcore-4_2_2-validation.html 介绍模型验证 在一个应用程序将数据存储到数据库之前,这个应 ...

  3. 常见的CSS和HTML面试题

    1. 常用那几种浏览器测试?有哪些内核(Layout Engine)? 浏览器:IE,Chrome,FireFox,Safari,Opera. 内核:Trident,Gecko,Presto,Webk ...

  4. OpenGL 使用GLFW创建全屏窗口

    OpenGL 使用GLFW创建全屏窗口 GLFW库里面的glfwCreateWindow()函数是用来创建窗口的函数. 这样函数的原型是: GLFWwindow* glfwCreateWindow(i ...

  5. 使用python内置模块os和openpyxl搜索指定文件夹下Excel中的内容

    在指定路径下,搜索Excel文件中包含的指定内容,首先需要遍历指定路径,得到该路径下所有Excel文件的绝对/相对路径:然后读取Excel中内容,将文件中的每个单元格的值与要搜索的内容进行判断(正则比 ...

  6. JavaScript中的构造函数和工厂函数说明

    在cnblog上看到一篇文章,讲解JS中的构造函数和工厂函数,觉得讲的真好 JavaScript中的工厂函数和构造函数都能用来创建一个对象,我们可以来看看下面的例子 构造函数 function cre ...

  7. C# 开发网页的打印版

    在项目中,有一个需求时是需要打印产品页面.但是打印出来的版本和网页上的版本不太一致,有些图片不需要,网页上以tab选项卡显示的内容,都需要在打印页面中看到..等等 CSS针对这种需求,引入了一个@me ...

  8. debian系Linux中文系统目录改为英文目录的解决方法

    之前给笔记本装的kali是英文版,系统安装好了后再修改系统语言为中文,或者直接就用英文系统,也是可以的. 后来笔记本的硬盘坏掉了,换ssd,然后安装kali的中文版,中文是方便,但是进去后就不爽了. ...

  9. Python开发【第六篇】:文件处理

    1. 文件   文件处理流程: 打开文件,获得文件句柄,并赋值 通过句柄对文件进行操作 关闭文件 1.1 打开文件   在 Python 中使用 open()函数打开文件,并返回文件对象: open( ...

  10. Unity 点乘与叉乘 计算敌人位置

    点乘:表示两个向量夹角 a*b=|a| * |b| * cos(ab),判断敌人在前后方 叉乘:表示两向量的法线