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

二个构造函数如何传递参数?
using System;
public class person
{
  public string name,sex;
  public int age;
}
public class student:person
{
  int chinese, meths, english;
  double evg;

   
  public student(int i,int j,int k)
  {
  chinese = i;
  meths = j;
  english=k;
  }
  public student(string a,string b,int m)
  {
  name = a;
  sex = b;
  age=m;
  } 
  public void com()
  {
  evg = (double)((chinese + meths + english) / 3);
  }
  public void write()
  {
  Console.WriteLine("名字:{0} 性别:{1} 年龄:{2}\n", name, sex, age);
  Console.WriteLine("成绩\nchinese:{0} methd:{1} english:{2}\n", chinese, meths, english);
  Console.WriteLine("平均成绩:" + evg);
  }
}
class text
{
  static public void Main()
  {
  ........
  }
}

偶做到这就不懂做了
如何给构造函数:
public student(int i,int j,int k)
public student(string a,string b,int m)
传递参数呢?
这个程序的要求是: 由类person派生出student,并且要对构造函数最少要二次重载
基类有name,sex,age域。
在新人交流区 没人理 只好才发多一次了


------解决方案--------------------
public class person 

public string name,sex; 
public int age; 

public person()
{}
public person(string name,string sex,int age)
{
this.name = name;
this.sex = sex;
this.age = age;
}

public class student:person 

int chinese, meths, english; 
double evg; 

public student(int i,int j,int k) 

chinese = i; 
meths = j; 
english=k; 
}

public student(string a,string b,int m) : base(a,b,m)
{ }

......
}

name,sex,age都从父类继承了,所以 base(a,b,m)就调用了父类的构造函数