网站建设资讯

NEWS

网站建设资讯

隐式调用父类的构造函数

代码如下:

创新互联是专业的息烽网站建设公司,息烽接单;提供网站设计、成都做网站,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行息烽网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

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();
        }
    }
}

 

 


当前题目:隐式调用父类的构造函数
网站URL:http://cdweb.net/article/jgodep.html