zoukankan      html  css  js  c++  java
  • C# this的用法

    如果定义的类中有一个成员变量为a,在类的成员函数中又定义了一个局部变量a,此时就必须使用this关键字来指示类的成员,也就是类的成员变量a写为this.a 。写的那些响应函数,其实都是类方法。  
    在程序运行后,方法可能会被很多这个类的实例(对象)来调用。那么请问,系统怎么知道调用这个类方法的是谁?是哪个对象?  所以,这时this就发挥它的作用了 
    每当一个对象调用这个类方法的时候,系统就会自动把这个对象的指针赋给this指针  this指当前类  
    比如在一个AAA类里有一个aaa的方法  
    在这个AAA类中调用这个aaa方法就可以用this.aaa  如果是在别的类中就要实例化一个对象来调用这个方法  AAA a=new AAA();  a.aaa;  
    在静态的方法中不能使用this  如main方法就是一个静态方法  
    this是保留的指针指向当前对象它的好处就是在编译期就可以获得对象的地址,比如一个类中有个成员类,成员类需使用上层类的函数,就可以把this传到成员类中 ,this 关键字引用类的当前实例 。
    以下是调用本内中的构造函数,用this

     using System; 
    namespace CallConstructor 


        public class Car     


            int petalCount = 0;       

      String s = "null";         
            Car(int petals)         


                petalCount = petals; 
                Console.WriteLine("Constructor w/int arg only,petalCount = " + petalCount);        

     }  
            Car(String s, int petals)   : this(petals)   //第一个this的意思是调用Car(int petals)方法的属性petals       

      { 

                this.s = s;     //第二个this的意思是实例化Car(String s, int petals)方法中的参数s(this.s = s)      

                Console.WriteLine("String & int args");     

        }  
            Car() 
     : this("hi", 47)   //第三个this是调用Car(String s, int petals)方法的两个参数并传参 

            { 
                Console.WriteLine("default constructor");     

        }  
            public static void Main()      

       { 
        Car x = new Car();         

        Console.Read();        

     }    

     } 

    }  
    //结果: 
    //Constructor w/int arg only,petalCount = 47 //String & int args //default constructor 
    this出现了,代表它所在的类的对象。 c#中 this保留字的用法:  
     this 关键字将引用类的当前实例,静态成员函数没有 this 指针,this 关键字可用于从构造函数实例方法和实例访问器中访问成员。

    以下是 this 的常用用途: 
    限定被相似的名称隐藏的成员,例如: 

     public Employee(string name, string alias) 

     { 
        this.name = name;     this.alias = alias;

     } 
    将对象作为参数传递到其他方法,例如:  CalcTax(this); 


    声明索引器,例如: 

     public int this [int param]

     { 
           get        


              return array[param];     

       }        

    set      

      { 
              array[param] = value;    

        }   

      } 

    在静态方法静态属性访问器或字段声明的变量初始值设定项中引用 this 是错误的。 示例 :
    在本例中,this 用于限定 Employee 类成员 name 和 alias,它们都被相似的名称隐藏。

    this 还用于将对象传递到属于其他类的方法 CalcTax // keywords_this.cs // this example using System; 
    public class Employee 

     { 
        public string name;  

       public string alias; 
        public decimal salary = 3000.00m;     // Constructor: 
        public Employee(string name, string alias)    

      { 
           // Use this to qualify the fields, name and alias:     

       this.name = name;        

    this.alias = alias;   

      } 
        // Printing method: 
        public void printEmployee()    

      { 
           Console.WriteLine("Name: {0} Alias: {1}", name, alias);        // Passing the object to the CalcTax method by using this:      

           Console.WriteLine("Taxes: {0:C}", Tax.CalcTax(this));    

     }

     } 
    public class Tax  


        public static decimal CalcTax(Employee E)   

       { 
           return (0.08m*(E.salary));   

      } 


    public class MainClass

      { 
        public static void Main()   

       { 
           Employee E1 = new Employee ("John M. Trainer", "jtrainer");        // Create objects: 

           E1.printEmployee();            // Display results:     

        } 

    }

    输出 
    Name: John M. Trainer Alias: jtrainer Taxes: $240.00   
    this关键字--精通c#编程  
    this关键字引用被访问成员所在的当前实例,静态成员函数没有this指针,this关键字可以用来从构造函数,实例方法和实例化访问器中访问成员 
    不能在静态方法静态属性访问器或者域声明的变量初始化程序中使用this关键字,这将会产生错误 
    1.在类的构造函数中出现的this作为一个值类型表示对正在构造的对象本身的引用 2.在类的方法中出现this作为一个值类型表示对调用该方法的对象的引用 
    3.在结构的构造函数中出现的this作为一个变量类型表示对正在构造的结构的引用 4.在结构的方法中出现的this作为一个变量类型表示对调用该方法的结构 ,使用this访问被相似名字隐藏员 

    using System; class E 


    public int id; 
    public string name; 
    public E(string name,int id)

     { 
       this.name=name; 
       this.id=id;//限定被相似的名称隐藏的成员 


    public void P()

     { 
       Print.pe(this); //作为参数传递到其他方法中去

     } 
    class Print 


       public static void pe(E x)   

     { 
        Console.WriteLine("name:{0}  id:{1}",x.name,x.id); 

       } 


    class Test

     { 
    public static void Main() 


       Console.WriteLine("请输入姓名:"); 

       string >   Console.WriteLine("请输入id号");    string s=Console.ReadLine();    int >   E e=new E(name,id);    e.P(); } } 
    ================================ 利用this在 索引中的引用访问类 using System; class Ic { 
    private int []arr=new int[10]; public int this[int index] { 
       get    { 
        if(index<0 || index>=10)     { 
         Console.WriteLine("下标越界!");      return 0;     }     else 
         return arr[index];    }    set    { 
        if(!(index>=10 || index<0))      arr[index] = value;    } } } 
    class T { 
    static void Main() { 
       Ic mic=new Ic(); 
       Console.WriteLine("输入要赋值元素的下标:");    string s=Console.ReadLine();    int index=int.Parse(s); 
       Console.WriteLine("请输入值:");    string str=Console.ReadLine();    int n=int.Parse(str);    mic[index]=n; 
       Console.WriteLine("current value:mic[index]={0}",mic[index]); 

    高山仰止, 景行行止。 四牡鲱鲱, 六辔如琴。 觏尔新婚, 以慰我心。
  • 相关阅读:
    mysql中给查询结果添加序号
    Mysql如何取当日的数据
    nginx 出现413 Request Entity Too Large问题的解决方法
    Mac 安装Jupyter Notebook
    Python-用xlrd模块读取excel,数字都是浮点型,日期格式是数字的解决办法
    sql-exists、not exists的用法
    sql语句replace函数的使用
    Python-日期格式化
    Python-自动用0补取长度
    Flask-实现下载功能
  • 原文地址:https://www.cnblogs.com/davidshi/p/3291264.html
Copyright © 2011-2022 走看看