关于继承构造函数的写法,求教。

6lilu9 2019-08-18 05:42:14
没系统学过C#,只是边看公开课边学,但公开课中对于继承一直绕着不讲,
所以我现在一直不明白到底是怎么回事。
想通过下面这个例子,来问问继承的构造函数怎么写?

正题:
关键代码处的代码,我自已琢磨了三种试探写法,显然都是胡闹。请高手指点一下。

using System.Collections.Generic;

namespace ConsoleApp1
{
public class 百家祖谱库
{
public static string 姓氏 = string.Empty;
public 百家祖谱库(string XS)
{
百家祖谱库.姓氏 = XS;
}

public static string Get第N代祖先(int N)
{
//实际是连接数据库,从库里获取祖先List
List<string> 祖先List = new List<string>();
祖先List.Add("龙");
祖先List.Add("腾");
祖先List.Add("四");
祖先List.Add("海");
祖先List.Add("云");
祖先List.Add("鹤");
祖先List.Add("九");
祖先List.Add("霄");


return 祖先List[N - 1];
}
}

public class 李家祖谱Manager:百家祖谱库
{
//试探写法一
//public 李家祖谱Manager()
//{
// MyBaseClass.姓氏 = "李";
//}

//试探写法二
public 李家祖谱Manager()
:base("李")
{
百家祖谱库.姓氏 = "李";
}

//试探写法三
public 李家祖谱Manager("李")
: base()
{
百家祖谱库.姓氏 = "李";
}

}

public class 王家祖谱Manager
{
//根据李家一样
}

class Program
{
static void Main(string[] args)
{
System.Console.WriteLine("李家第3代祖先是:"李家祖谱Manager.Get第N代祖先(3));
}
}
}

...全文
150 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
Dear200892 2019-08-20
  • 打赏
  • 举报
回复
引用 11 楼 wanghui0380 的回复:
[quote=引用 10 楼 Dear200892 的回复:] ChineseSurnames li = new LiFamilyTree("郭"); Console.WriteLine(li.GetAncestor(2));
李家的先祖,冒出来一个姓郭的,而且你居然还知道李家的祖宗居然姓郭,而且还要查这个姓郭的第2代李姓子孙是谁 好吧,老郭的相声“于谦的父亲王老爷子那个NX啊” [/quote] 那你就创建一个【郭】族谱 ,我上面只是写了一个【李】族谱
wanghui0380 2019-08-20
  • 打赏
  • 举报
回复
引用 10 楼 Dear200892 的回复:
ChineseSurnames li = new LiFamilyTree("郭"); Console.WriteLine(li.GetAncestor(2));
李家的先祖,冒出来一个姓郭的,而且你居然还知道李家的祖宗居然姓郭,而且还要查这个姓郭的第2代李姓子孙是谁 好吧,老郭的相声“于谦的父亲王老爷子那个NX啊”
Dear200892 2019-08-20
  • 打赏
  • 举报
回复

    /// <summary>
    /// 百家姓
    /// </summary>
    public class ChineseSurnames
    {
        /// <summary>
        /// 姓氏
        /// </summary>
        public string FamilyName { get; set; }
        private List<string> ancestor = new List<string>();
        /// <summary>
        /// 祖先名
        /// </summary>
        public List<string> Ancestor { get {
                if (ancestor==null || ancestor.Count()==0)
                {
                    ancestor.Add("龙");
                    ancestor.Add("腾");
                    ancestor.Add("四");
                    ancestor.Add("海");
                    ancestor.Add("云");
                    ancestor.Add("鹤");
                    ancestor.Add("九");
                    ancestor.Add("霄");
                }
                return ancestor;
            } }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="familyName">姓氏</param>
        public ChineseSurnames(string familyName)
        {
            this.FamilyName = familyName;
        }
        /// <summary>
        /// 获取第N代祖先名字
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public virtual string GetAncestor(int num)
        {
            string msg = FamilyName + "家族谱第" + num + "代祖先名字" + FamilyName + Ancestor[num - 1];
            return msg;
        }
    }
    /// <summary>
    /// 【李】家姓氏
    /// </summary>
    public class LiFamilyTree : ChineseSurnames
    {
        public LiFamilyTree(string familyName) : base(familyName)
        {
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            ChineseSurnames li = new LiFamilyTree("李");
            Console.WriteLine(li.GetAncestor(2));
            Console.ReadKey();
        }
    }
XBodhi. 2019-08-20
  • 打赏
  • 举报
回复
public static string 姓氏 = string.Empty; 不能静态。如果你静态了它就无法参与多态了,而且实例里也没有这个成员。
冰川711 2019-08-20
  • 打赏
  • 举报
回复
一个带有笑点的帖子~
正怒月神 2019-08-20
  • 打赏
  • 举报
回复
通过base来执行指定的父类构造函数(有参/无参) 通过this来指定自己的构造函数(有参/无参) 就这么简单。
threenewbee 2019-08-18
  • 打赏
  • 举报
回复
构造函数不能继承,派生类用冒号那个是调用,不是继承
wanghui0380 2019-08-18
  • 打赏
  • 举报
回复
public 李家祖谱Manager("李") : base() 这个写法多余,因为这是无参构造,你写不写,他都会进无参构造
wanghui0380 2019-08-18
  • 打赏
  • 举报
回复
public 李家祖谱Manager(string x) 这个没指定构造,默认调用父类无参构造 public 李家祖谱Manager(string x):base(x) // 这个指定使用父类带参构造,参数x,当然就例子来说,老李家就应该姓李,不应该是别的,所以语句没问题,逻辑有问题 public 李家祖谱Manager():base("李") //老李家,默认就应该姓李,所以不必给参数,但显示调用父类带参构造,以便初始化。这个逻辑正确,语法正确
秋的红果实 2019-08-18
  • 打赏
  • 举报
回复
建议系统学习,别浪费时间 建议使用英文变量 public 李家祖谱Manager(string x) :base(x) { // } 李家祖谱Manager obj=new 李家祖谱Manager("李"); System.Console.WriteLine("李家第3代祖先是:"obj.Get第N代祖先(3));
wanghui0380 2019-08-18
  • 打赏
  • 举报
回复
微软默认调用路径:先调用父类无参构造,然后在进入自己的语句块。 当你觉着你的逻辑不应该这样,可以更改顺序,比如调用自己的this,或者调用父类的base。这个顺序还是有base用base,没base还是用父类无参构造 所以,没啥可以讲。也就是无论如何都会先执行父类构造,你显示指定用那个就用那个,如果你没指定就用父类无参构造。就这么简单的,不必纠结,不要累着自己
wanghui0380 2019-08-18
  • 打赏
  • 举报
回复
哎,为啥目前的程序员都过的这么纠结?我都为你们累 根本就不需要讲的地方,也需要纠结? 基本准则如果你没覆盖,就隐式调用父类。如果你覆盖了就用你自己的。如果你想控制路径就自己显式 base或this public 李家祖谱Manager() :base("李") -----------------------需要纠结么,给自己弄了默认无参构造,调用路径--先调用父类的一个有参构造 //public 李家祖谱Manager() -----------还是不需要纠结,覆盖了父类无参构造 //{ // MyBaseClass.姓氏 = "李"; //} public 李家祖谱Manager("李")------依旧不需要纠结,定义了一个有参构造,调用路径,先去调用父类无参构造 : base() { 百家祖谱库.姓氏 = "李"; } 你们太累了,休息把。这么学,不得其法,本来是你写代码,怎么实现是你的想法,你到时搞成了,怎么实现是人家规定的,没人规定,你想怎么实现,就怎么实现
6lilu9 2019-08-18
  • 打赏
  • 举报
回复
问题没表述清楚,重新表述一遍


using System.Collections.Generic;

namespace ConsoleApp1
{
    public class 百家祖谱库
    {
        public static string 姓氏 = string.Empty;
        public 百家祖谱库(string XS)
        {
            百家祖谱库.姓氏 = XS;
        }

        public static string Get第N代祖先(int N)
        {
            //实际是连接数据库,从库里获取祖先List
            List<string> 祖先List = new List<string>();
            祖先List.Add("龙");
            祖先List.Add("腾");
            祖先List.Add("四");
            祖先List.Add("海");
            祖先List.Add("云");
            祖先List.Add("鹤");
            祖先List.Add("九");
            祖先List.Add("霄");


            //每次到这处,姓氏总为空
             return 姓氏+祖先List[N - 1];
        }
    }

    public class 李家祖谱Manager:百家祖谱库
    {
        //试探写法一
        //public 李家祖谱Manager()
        //{
        //    MyBaseClass.姓氏 = "李";
        //}

        //试探写法二
        public 李家祖谱Manager()
            :base("李")
        {
            百家祖谱库.姓氏 = "李";
        }

        //试探写法三
        public 李家祖谱Manager("李")
    : base()
        {
            百家祖谱库.姓氏 = "李";
        }

    }

    public class 王家祖谱Manager
    {
        //根据李家一样
    }

    class Program
    {
        static void Main(string[] args)
        {
            System.Console.WriteLine("李家第3代祖先是:"李家祖谱Manager.Get第N代祖先(3));
        }
    }
}


wanghui0380 2019-08-18
  • 打赏
  • 举报
回复
额,讲不了,没得讲。 这块根本就不需要讲

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧