C# 类(9) This关键字

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    //定义一个类.
    class MyClass
    {
        string MyString = "Tony";
        public void ShowMessage(string MyString)
        {
            if (MyString == this.MyString)  // 第一个MyString是这个方法中的MyString, 第二个this.MyString则是这个类中的成员. 所以,this的作用就是表示当前对象.
            { Console.WriteLine("是相等的"); }
            else
            { Console.WriteLine("不是相等的"); }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            MyClass My = new MyClass();
            My.ShowMessage(Console.ReadLine());
        }
    }
}
posted @ 2012-10-01 20:06  梦断难寻  阅读(447)  评论(0编辑  收藏  举报