日期:2014-05-18  浏览次数:21254 次

将类的对象作为方法参数该怎么写?
比如说有个关于Member的类
  public class Member
{
  string name;
  string age;

  public static void AddMember() //实例化member类
  {
  Member mymember = New Member();
  mymember.name = LiMing;
  mymember.age = 26;
  }

  public static void ViewMember() //显示mymember实例的成员  
  {
  Console.WriteLine("mymember的名字叫:{0}", mymember.name);
  Console.WriteLine("mymember的年龄:{0}", mymember.age);
  }
}
   
然后我编译Main函数
  public static void Main()
  {
  AddMember();
  ViewMember();
  }

 当然出错提示说找不到mymember,于是我想到在AddMember()末尾加上return mymember,然后通过ViewMember()的参数来把mymember传递进来,然后就可以输出了,可不知道该怎么写。也就是我的问题:“将类的对象作为方法参数该怎么写?“




------解决方案--------------------
public class Member 

string name; 
string age; 
static Member mymember = New Member();
public static void AddMember() //实例化member类 


mymember.name = LiMing; 
mymember.age = 26; 


public static void ViewMember() //显示mymember实例的成员

Console.WriteLine("mymember的名字叫:{0}", mymember.name); 
Console.WriteLine("mymember的年龄:{0}", mymember.age); 



然后我编译Main函数 
public static void Main() 

AddMember(); 
ViewMember(); 
}
------解决方案--------------------
把static去掉,mymember改为this

public class Member 

string name; 
string age; 
public void AddMember() //实例化member类 
{ this.name = "LiMing"; 
this.age = "26"; 

public void ViewMember() //显示mymember实例的成员
{ Console.WriteLine("mymember的名字叫:{0}", this.name); 
Console.WriteLine("mymember的年龄:{0}", this.age); 


public static void Main() 

Member mb = new Member();
mb.AddMember(); 
mb.ViewMember(); 
}
有空多看书,静态函数最好不要与类内的变量有关系,如果真要用请在变量前加 static
------解决方案--------------------
public class Member 

string name; 
string age; 
static Member mymember = new Member(); 
public static void AddMember() //实例化member类 


mymember.name = "iMing"; 
mymember.age = "26"; 


public static void ViewMember() //显示mymember实例的成员

Console.WriteLine("mymember的名字叫:{0}", mymember.name); 
Console.WriteLine("mymember的年龄:{0}", mymember.age); 



然后我编译Main函数 
public static void Main() 

AddMember(); 
ViewMember(); 


错误真多 看好了红到地方
------解决方案--------------------
凑合看看吧.lz多补补基础吧.
C# code

namespace ConsoleApplication1
{
    public class Program
    { 

        static void Main(string[] args)
        {

            Member m = new Member();
            Member.AddMember(m);
            Member.ViewMember(m);
        }
    }

    public class Member
    {
        string name;