资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

隐式调用父类的构造函数-创新互联

代码如下:

成都创新互联公司-专业网站定制、快速模板网站建设、高性价比白水网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式白水网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖白水地区。费用合理售后完善,十余年实体公司更值得信赖。

Person.cs文件。

using System;
using System.Collections.Generic;
using System.Text;

namespace InheritDemo
{
   public class Person
   {
       private string id;
       public string Id
       {
           get
           {
               return id;
           }
           set
           {
               id = value;
           }
       }
       private string name;
       public string Name
       {
           get
           {
               return name;
           }
           set
           {
               name = value;
           }
       }
       private int age;
       public int Age
       {
           get
           {
               return age;
           }
           set
           {
               age = value;
           }
       }
       private string gender;
       public string Gender
       {
           get
           {
               return gender;
           }
           set
           {
               gender = value;
           }
       }
         public Person(string name1, int age1, string gender1)
       {
           name = name1;
           age = age1;
           gender = gender1;
       }
       public void Display()
       {
           Console.WriteLine("{0}是{1}性,年龄为{2}",this.name,this.gender,this.age);
       }

   }

   public class Student : Person
   {
       private string class1;
       public string Class
       {
           get
           {
               return class1;
           }
           set
           {
               class1 = value;
           }
       }
       private string department;
       public string Department
       {
           get
           {
               return department;
           }
           set
           {
               department = value;
           }
       }
       private string no;
       public string No
       {
           get
           {
               return no;
           }
           set
           {
               no = value;
           }
       }
        public void Study()
       {
           Console.WriteLine("学生在学校学习。");
           Console.Read();
       }
   }
}

Program.cs文件。

using System;
using System.Collections.Generic;
using System.Text;

namespace InheritDemo
{
   class Program
   {
       static void Main(string[] args)
       {
           Person objPerson = new Person("XXX",XX,"X");
           objPerson.Display();
           Student objStudent = new Student();
           objStudent.Name = "XXX";
           objStudent.Age = XX;
           objStudent.Gender = "X";
           objStudent.Class = "XXXXXXXXX";
           objStudent.Display();
           Console.Read();
       }
   }
}

错误:“Person”方法没有采用“0”个参数的重载。

原因:子类的构造函数没有使用base指明调用父类哪个构造函数。

解决办法:

using System;
using System.Collections.Generic;
using System.Text;

namespace InheritDemo
{
   public class Person
   {
       private string id;
       public string Id
       {
           get
           {
               return id;
           }
           set
           {
               id = value;
           }
       }
       private string name;
       public string Name
       {
           get
           {
               return name;
           }
           set
           {
               name = value;
           }
       }
       private int age;
       public int Age
       {
           get
           {
               return age;
           }
           set
           {
               age = value;
           }
       }
       private string gender;
       public string Gender
       {
           get
           {
               return gender;
           }
           set
           {
               gender = value;
           }
       }
       public Person() { }  //隐式的调用,base(),父类要提供无参的构造函数。
       public Person(string name1, int age1, string gender1)
       {
           name = name1;
           age = age1;
           gender = gender1;
       }
       public void Display()
       {
           Console.WriteLine("{0}是{1}性,年龄为{2}",this.name,this.gender,this.age);
       }

   }

   public class Student : Person
   {
       private string class1;
       public string Class
       {
           get
           {
               return class1;
           }
           set
           {
               class1 = value;
           }
       }
       private string department;
       public string Department
       {
           get
           {
               return department;
           }
           set
           {
               department = value;
           }
       }
       private string no;
       public string No
       {
           get
           {
               return no;
           }
           set
           {
               no = value;
           }
       }
       public Student() { }  //隐式的调用,base(),父类要提供无参的构造函数。
       public void Study()
       {
           Console.WriteLine("学生在学校学习。");
           Console.Read();
       }
   }
}

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网页标题:隐式调用父类的构造函数-创新互联
当前链接:http://cdkjz.cn/article/cocggd.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220