A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 孔健 中级黑马   /  2013-3-11 23:46  /  1566 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

在类的继承中,子类不会继承父类构造函数,但是会默认调用父类无参的构造函数;
如果要显示的调用父类的有参数构造函数,要用base关键字。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace base调用基类构造方法
{
    class Program
    {
        static void Main(string[] args)
        {
            Teacher teacher = new Teacher("tom",25,"20130311");
            Console.ReadKey();
        }
    }
    class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public Person(string name ,int age)
        {
            this.Name = name;
            this.Age = age;
        }
    }
    class Teacher : Person
    {
        public string Id { get; set; }
        public Teacher(string name ,int age,string id)
            :base(name,age)
        {
            this.Id = id;
            Console.WriteLine("大家好,我叫{0},今年{1}了,我的编号是{2}.", this.Name, this.Age, this.Id);
        }
    }
}

运行结果:

      大家好,我叫tom,今年25了,我的编号是20130311.


1 个回复

正序浏览
继续努力

点评

那是必须地!!呵呵 ..  发表于 2013-3-11 23:59
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马