this在C#中的用法(网摘集合)
发布:k533 | 发布时间: 2010年10月29日如果定义的类中有一个成员变量为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的意思是实例化Car(String s, int petals)方法中的参数s(this.s = 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}\nAlias: {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()
{
// Create objects:
Employee E1 = new Employee ("John M. Trainer", "jtrainer");
// Display results:
E1.printEmployee();
}
}
输出
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}\t 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]);
}
}
this关键字用在类的方法声明内部,表示当前调用此方法的那个对象的引用
为什么这么用呢?首先看下面的例子:
public class TestThis {
public static void main(String[] args) {
Dog dog=new Dog();
dog.bark(2);
}
}
class Dog{
String ;
public Dog() {
}
public void bark(int n){
System.out.println("叫"+n+"声");
}
}
在面向对象的编程中,经常要做的事情,就是发送消息给对象,要就是指的是方法的调用过程
在上面的代码中指的就是:dog.bark(2);这句代码而为了发送消息给对象这个过程,编译器暗地里,在这个方法的调用过程中干了一件事,就是私底下将dog这个引用作为参数,传递到了bark方法的内部(当然,这个过程是编译器偷偷完成的,而不需要我们去明码表示出来,如果我们想自己明码完成这个过程,那一定会出错的,呵呵)
知道了上面的过程,那么,当你在定义一个类的方法时,如果想在这个方法内部获取被偷偷传进来的这个当前对象的引用呢?因为根本没有明码的标志符可用,所以,在Java中就有这个专门的关键字:this,用在方法定义的内部,表示当前正在调用此方法的那个对象的引用
所以,这个时候,如果写出下面的代码:就能从本质上理解类的各个方法之间是如何相互调用的了
public class TestThis {
public static void main(String[] args) {
Dog dog=new Dog();
dog.bark(2);
}
}
class Dog{
String ;
public Dog() {
}
public void bark(int n){
this.bark(); //这里直接使用bark();即可,不应该画蛇添足使用this
System.out.println("叫"+n+"声");
}
public void bark(){
System.out.println("狗要叫啦!");
}
}
但是应该注意到,在上面的bark(int n)这个方法的定义中,使用了this.bark();,这里使用this是没有必要的,因为,编译器能够自动帮助我们添加这个this但是,有的人却习惯这么使用,我现在刚刚学习Java,也是一直在这么使用,前段时间用swing写了点东西,里面也是这么用的后来,朋友看我写的那些代码,呵呵,很乱很乱,因为我把this用在了没有必要使用的地方,所以,看我的代码的人就会觉得很乱所以要养成良好的coding习惯啊
当然,关于这种说法也有例外,比如说,为一个类的属性,定义getter和setter时,往往setter的参数的标志符和相应的属性相同,这个时候就需要在setter的内部使用this加以区分了这也算是this关键字用在方法定义内部中的一种用法吧,当然,只是可能有的人在写代码时,没有这种习惯,比如说我就是而,一些主流的java IDE却有这种习惯,呵呵
class Dog{
String ;
public Dog() {
}
public void setName(String name){
this.name=name;
}
}
在类的方法定义中使用this的时候,就是当必需明确指出当前对象的引用时,才使用例如,我们在定义的方法中需要返回当前对象的引用时
public class TestThis {
public static void main(String[] args) {
Dog dog=new Dog();
dog.bark(2);
}
}
class Dog{
String ;
public Dog() {
}
public Dog bark(int n){
bark();
System.out.println("叫"+n+"声");
return this;
}
public void bark(){
System.out.println("狗要叫啦!");
}
}
或者,在方法定义中调用了其他的方法,而调用这个其它的方法时,需要将当前对象的引用传递给这个其它的方法作为参数,这个时候应该使用this
总结:通过理解上面的内容,就不难理解,为什么在静态方法(static)内部,不能使用this关键字的原因了
因为,在调用静态方法的时,不会将当前对象的引用传递进来(大多数情况,使用静态方法时,根本不会存在当前对象吧,呵呵)
这样就不难得出使用静态方法时,几个需要注意的地方:
1:静态方法中只能调用其他的静态方法
2:静态方法中只能访问静态属性
3:静态方法中不能使用this关键字(实际上也是上面的1和2)
当然,上面的12两点是相对的,只是帮助我们理解,如果你在静态方法中,创建了一个类的对象,这个时候当然可以通过这个对象的引用访问这个对象的静态数据了
总结:就是在调用静态方法时,一定要用类名去调用,而不建议使用对象的引用去调用(虽然,在语法上这样是允许的),以避免引起混淆。
- 相关文章:
构造函数和析构函数的功能与区别 (2010-10-18 8:58:31)
uml中如何定义类 (2010-10-18 8:53:51)
C#软件项目开发全程剖析 (2010-8-30 22:44:17)
C#流的概念 (2010-7-21 16:23:4)
C#流的概念-I/O流 (2010-7-21 16:23:2)
C#文件流-FileStream类 (2010-7-21 16:22:58)
C#文件流-FileStream对象创建 (2010-7-21 16:22:55)
C#泛型-使用IComparer子类的Sort排序方法 (2010-7-21 16:22:55)
C#文件处理-文件管理 (2010-7-21 16:22:47)
C#中File类操作文件实例-创建文件、复制文件、移动文件 (2010-7-21 16:22:44)
- 1.云朵
- this的用法写的很清楚,也很详细,学习了!
- 2011-7-22 11:45:37 [Report Spam] 回复该留言
发表评论
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。





