温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

隐式调用父类的构造函数

发布时间:2020-05-30 23:00:52 来源:网络 阅读:759 作者:韩小妞 栏目:编程语言

代码如下:

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

 

 

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI