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

c#刚学,请教高手
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace public与私有的运用类2
{
  class Program
  {
  static void Main(string[] args)
  {
  Class1 student = new Class1();
  student.show();
  student.stName = Console.ReadLine();
  student.stAge = Convert.ToInt32(Console.ReadLine());
  int stuo = Convert.ToInt32(Console.ReadLine());
  student.stno(stuo);
  student.stSex = "男";
  student.stpwd("2011123011");
  student.jieguo();
  Console.ReadKey();
  }
  }

  class Class1
  {
  public string stName;
  public int stAge;
  public string stSex;
  private int stNo;
  private string stPwd;

  public int stno(int st_no)
  {
  if (stNo < 0)
  {
  Console.WriteLine("请输入真确的年龄!谢谢合作!");
  }
  return st_no = this.stNo;
  }

  public string stpwd(string st_pwd)
  {
  return st_pwd = this.stPwd;
  }

  public void show()
  {
  Console.WriteLine("*********************");
  Console.WriteLine(" Lam制作 ");
  Console.WriteLine("*********************");
  }

  public void jieguo()
  {
  Console.WriteLine("他叫{0},性别{1},年龄{2},学号为{3},密码为:{4}", this.stName, this.stSex, this.stAge, this.stNo, this.stPwd);
  }
  }
}
stPwd怎么是为空的?还有stNO为什么是0。为什么我的值,赋值不进去。我明明为stPwd返回值了。




------解决方案--------------------
public string stpwd(string st_pwd)
{
this.stPwd = st_pwd ;
return st_pwd ;
}


public int stno(int st_no)
{
if (stNo < 0)
{
Console.WriteLine("请输入真确的年龄!谢谢合作!");
}
this.stNo = st_no ;
return st_no ;
}


------解决方案--------------------
public int stno(int st_no)
{
if (stNo < 0)
{
Console.WriteLine("请输入真确的年龄!谢谢合作!");
}
return this.stNo = st_no;
}

public string stpwd(string st_pwd)
{
return this.stPwd = st_pwd;
}
你赋值的方向是错的,应该是将参数的值付给对象的字段,你却是将字段的值赋给参数。结果就是字段的值仍然是默认值,int的默认值就是0,字符串的默认值就是“”
------解决方案--------------------
接受值的一方要在等号左边

this.stPwd = st_pwd,才是给这个实例的stPwd赋值

就像
int i = 0;一样,是把0赋给i
------解决方案--------------------
赋值运算符(=)的左边是要被赋值的变量,右边是值。
如,要将整形变量age赋值为20:int age=20;
具体到你的代码里应该是this.stPwd = st_pwd;
你把你的顺序改过来就可以正确赋值了。
------解决方案--------------------
student.stno(stuo);只是有个返回值关键是返回值给谁了呢?
Console.WriteLine(student.stno(stuo));
你就可以看到你想要的stno了.但是stno还是0,为什么呢?因为你在初始化student的时候就给stno赋值为0了。
而使用return st_no = this.stNo;这个语句的时候实际执行的是将This.stNo的值0复制给st_no然后将其值返回。
单步追踪过程中你可以看到很明显的结果。
在int stuo = Convert.ToInt32(Console.ReadLine());处打一个断点,执行到这个地方的时候就可以知道student里面值的存放状况了。其中stSex为null,stPwd也是null啊,当然就没有值了。