| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 745 人关注过本帖
标题:[求助]this 是对哪个对象的引用
只看楼主 加入收藏
wohenguaia
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2006-1-9
收藏
 问题点数:0 回复次数:3 
[求助]this 是对哪个对象的引用
原代码如下:
using System;
class Address {
public string name;
public string address;
}
class MethodParams {

public static void Main() {
string myChoice;
MethodParams mp = new MethodParams();
do {
// show menu and get input from user
myChoice = mp.getChoice();
// Make a decision based on the user's choice
mp.makeDecision(myChoice);
// Pause to allow the user to see the results
Console.Write("Press any key to continue...");
Console.ReadLine();
Console.WriteLine();
} while (myChoice != "Q" && myChoice != "q"); // Keep going until the user wants to quit
}

// show menu and get user's choice
string getChoice() {
string myChoice;
// Print A Menu
Console.WriteLine("My Address Book\n");
Console.WriteLine("A - Add New Address");
Console.WriteLine("D - Delete Address");
Console.WriteLine("M - Modify Address");
Console.WriteLine("V - View Addresses");
Console.WriteLine("Q - Quit\n");
Console.WriteLine("Choice (A,D,M,V,or Q): ");
// Retrieve the user's choice
myChoice = Console.ReadLine();
return myChoice;
}

// make decision
void makeDecision(string myChoice) {
Address addr = new Address();
switch(myChoice) {
case "A":
case "a":
addr.name = "Joe";
addr.address = "C# Station";
this.addAddress(ref addr);
break;
case "D":
case "d":
addr.name = "Robert";
this.deleteAddress(addr.name);
break;
case "M":
case "m":
addr.name = "Matt";
this.modifyAddress(out addr);
Console.WriteLine("Name is now {0}.", addr.name);
break;
case "V":
case "v":
this.viewAddresses("Cheryl", "Joe", "Matt", "Robert");
break;
case "Q":
case "q":
Console.WriteLine("Bye.");
break;
default:
Console.WriteLine("{0} is not a valid choice", myChoice);
}
}

// insert an address
void addAddress(ref Address addr) {
Console.WriteLine("Name: {0}, Address: {1} added.", addr.name, addr.address);
}
// remove an address
void deleteAddress(string name) {
Console.WriteLine("You wish to delete {0}'s address.", name);
}
// change an address
void modifyAddress(out Address addr) {
//Console.WriteLine("Name: {0}.", addr.name); // causes error!
addr = new Address();
addr.name = "Joe";
addr.address = "C# Station";
}
// show addresses
void viewAddresses(params string[] names) {
foreach (string name in names) {
Console.WriteLine("Name: {0}", name);
}
}
}
带颜色的this是对哪个对象的引用?
谢谢!
搜索更多相关主题的帖子: 对象 
2006-01-16 13:51
jackey163
Rank: 1
等 级:新手上路
帖 子:230
专家分:0
注 册:2005-7-21
收藏
得分:0 
好象是当前CLASS里面的MethodParams实例吧,我是这么理解的,不对请指正!

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 是错误的。


.net 方面可以交流下 MSN:jackeyhuang123@
2006-01-17 11:18
liuhy_2001
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2005-12-26
收藏
得分:0 

这个this就是指的addAddress这个方法。

2006-01-17 13:53
whzym111
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2005-10-21
收藏
得分:0 
这个this 是指的当前类!因为addAddress方法是在类中的所以this.addAddress=MethodParams.addAddress

没有退路的一条路!我选择我无悔!
2006-01-24 09:49
快速回复:[求助]this 是对哪个对象的引用
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018010 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved