在C#

时间:2017-06-19 06:05:16

标签: c# inheritance

这个问题听起来很奇怪,发布它的原因是因为它始终是一个很大的混乱 下面是我使用继承的代码

class Program:Identity
    {
        static void Main(string[] args)
        {
            Identity id = new Identity();
            Console.WriteLine(id.name);
            Console.WriteLine(id.age);
            Console.WriteLine(id.DOB);
            Console.WriteLine(id.employed);
            Console.WriteLine(id.grade);
            Console.WriteLine(id.experience);
            Console.WriteLine(id.metric);
            Console.WriteLine(id.intermediate);
            Console.WriteLine(id.UG);
            Console.WriteLine(id.PG);
            Console.ReadKey();
        }
    }

    class Identity:Occupation
    {
        public string name{ get; set; }
        public int age { get; set; }
        public DateTime DOB { get; set; }
        public Identity()
        {
            name = "Mr. Xyz";
            age = 35;
            DOB = DateTime.Now.Date;
        }
    }

   class Occupation:Education
    {
        public string employed { get; set; }
        public string grade { get; set; }
        public int experience { get; set; }
        public Occupation()
        {
            employed = "employed-yes";
            grade = "B-Level";
            experience = 2;   
        }
    }

    class Education
    {
        public string metric { get; set; }
        public string intermediate { get; set; }
        public string UG { get; set; }
        public string PG { get; set; }
        public Education()
        {
            metric = "metric-yes";
            intermediate = "intermediate-yes";
            UG = "UG-yes";
            PG = "PG-No";
        }
    }

2.。现在使用外部类文件的代码相同

class Program
    {
        static void Main(string[] args)
        {
            Person p = new Person();
            p.set_values();
            Console.WriteLine(p.name);
            Console.WriteLine(p.age);
            Console.WriteLine(p.DOB);
            Console.WriteLine(p.employed);
            Console.WriteLine(p.grade);
            Console.WriteLine(p.experience);
            Console.WriteLine(p.metric);
            Console.WriteLine(p.intermediate);
            Console.WriteLine(p.UG);
            Console.WriteLine(p.PG);
            Console.ReadKey();
        }
    }
class Person
    {
        public string name;
        public int age;
        public DateTime DOB;
        public string employed;
        public string grade;
        public int experience;
        public string metric;
        public string intermediate;
        public string UG;
        public string PG;

        public void set_values()
        {
            name = "Mr. ABC";
            age = 20;
            DOB = DateTime.Now.Date;
            employed = "employed-No";
            grade = "None";
            experience = 2;
            metric = "metric-yes";
            intermediate = "intermediate-yes";
            UG = "UG-No";
            PG = "PG-No";
        }

    }

我的问题是继承的好处是什么,何时使用它,我总是可以通过创建那个对象来使用另一个类,在我的小编程生活中永远不会使用继承。
有人可以启发这一点 - 继承的时间和原因 如果您不清楚这个问题,请告诉我,我会尽量让它更清楚。

2 个答案:

答案 0 :(得分:0)

您需要这么做的原因很多。

  • 当您将标准化格式的数据库响应或JSON数据反序列化为第一个示例中提到的类结构时,将需要它。
  • 模块化代码总是更容易处理
  • 您可以重复使用该类来定义不属于同一结构的不同数据集(或者可以说处理不一致的数据)
  • 可以重用基类来定义另一组派生类

还有更多理由要继承。您可以随时谷歌查看它为何有用

答案 1 :(得分:0)

如果您考虑创建继承层次结构class B : A,请问自己以下问题:

  • 每个BA吗?
    • 如果没有,请不要使用继承,单独设计
  • 每个AB吗?
    • 如果是这样,请不要使用继承,而是将它们设计为一个类
  • 我的计划中A没有B是否有用?
    • 如果没有,不要使用继承,那就过度工程了。而是将它们设计为一个类

现在,如果达到这一点,您实际上可以将它们设计为继承层次结构。此时,收益也应该变得清晰。如果您在我的列表中找到了一个分支解决方案,那么在将继承应用于问题时,您看不到其好处,这是完全可以理解的。

此列表可能仍然不完整,仅将其视为起点

相关问题