注册 登录
编程论坛 C# 论坛

怎么使用带参构造函数来实现多个对象的初始化

月下长相依 发布于 2015-04-14 14:53, 455 次点击
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            student s1 = new student();
            student s2 = new student();
            student s3 = new student();

            Console.WriteLine(s1.name);
            Console.WriteLine(s1.age);
            Console.WriteLine(s2.name);
            Console.WriteLine(s2.age);
            Console.WriteLine(s3.name);
            Console.WriteLine(s3.age);

            Console.ReadLine();
        }
    }

    public class student
    {
        public string name;
        public int age;
        public student(string n, int a)//构造函数
        {
            age = a;          //我想知道a和n怎么使用
            name = n;
        }
    }
}
1 回复
#2
hkkuality2015-04-14 15:44
            string n="";
            int m =0;
            student s1 = new student(n,m);
            student s2 = new student(n,m);
            student s3 = new student(n,m);

你写的有点问题好像。。。。

1