【问题标题】:How to pass parameters in constructor of inherited class program?如何在继承类程序的构造函数中传递参数?
【发布时间】:2020-02-25 18:23:43
【问题描述】:

这是我的代码有什么错误? 以及如何解决它。 如前所述,构造函数是一种方法,它不包含任何返回类型。如果我们想在c#中创建一个构造函数,那么我们需要创建一个名称与类名相同的方法。

class Program
    {
        public static int number;
        public static string name;
        public static float salary = 20000;
        static void Main(string[] args)
        {

            Console.WriteLine("Welcome");
            Console.WriteLine("Enter Your Name: ");
            name = Console.ReadLine();
            Employee System = new Employee("Sameed", 20000f);
            Bonus b1 = new Bonus();
            //b1.Employee(name, salary);
            b1.Salary(salary);
            b1.Bonuses();
            Display(number);
            Display(name,number);
    }
    static void Display(int n)
{

    Console.WriteLine("Employee Number is: " +n);
}
    static void Display(string name, int num)
{
    Console.WriteLine("Congratulations {0}, your Employee number has been generated {1}",name,num);
}
}
public class Employee
{
    public Employee(string n, float s)
    {
        Console.WriteLine("Hello Mr. {0}, Your Salary is {1}", n, s);
    }
}
public class Director:Employee
{
    public void Salary(float s)
    {
        Console.WriteLine("Your Salary is: " + s);
    }
}
    public class Bonus:Director
    {
        public void Bonuses()
        {

            int bonus = 40000;
            Console.WriteLine("Your Bonus is: " + bonus);
        }
    }

错误:Employee' 不包含采用 0 个参数的构造函数

【问题讨论】:

  • 类主管需要有一个您编写的构造函数。此构造函数需要调用 base(n,s),它是员工的构造函数。您可以将参数从 director 构造函数传递到 base,也可以将硬编码值提供给 base。满足您将正确数据提供给员工构造函数的需求。

标签: c# .net class constructor


【解决方案1】:

由于Director类继承了Employee类,它应该使用基类的构造函数来创建一个实际的实例。因此,在您的代码中,您还将构造函数添加到 Director 类并调用基类构造函数,就像这样

public Director(string n, float s) : base(n, s)
{
}

没有这个编译器会产生错误

错误 CS7036:没有给出与 必需的形参

Bonus 类也应该这样做

public Bonus(string n, float s) : base(n, s)
{
}

Bonus 实例创建如下

Bonus b1 = new Bonus(name, salary);

查看instance constructors 和using constructors 的文章。第二篇文章中的示例非常接近OP代码

【讨论】:

  • @Sohaib Riaz 是否回答了您的问题,您需要更多信息吗?
【解决方案2】:

所以,您的Director 是Employee。 Employee 只能用 2 个值实例化 - name 和 salary。因此,因为您的 Director 是 Employee,这意味着同样的规则适用于他 - 他必须拥有姓名和薪水。

解决方法是在 Director 上调用 base 构造函数 - 即调用 Employee 构造函数。

“我该怎么做?”我听到你问。好吧,通过特殊的base 通话:

public class Employee
{
    public Employee(string n, float s)
    {
        Console.WriteLine("Hello Mr. {0}, Your Salary is {1}", n, s);
    }
}

public class Director : Employee
{
    public void Salary(string name, float salary)
        : base(name, salary) // Make sure you include this line
    {
        Console.WriteLine("Your Salary is: " + s);
    }
}

但等等,还有更多!

我还想就您的代码提出一些建议。

1) 请使用更长的变量名:name 和 salary 比 n 和 s 更清晰。

2) 您从Director 继承Bonus。继承是is-a 关系。所以,说“董事是员工”是完全有道理的,但说“奖金是董事”则不然(除非你在非常不寻常的意义上使用“奖金”)。如果你不能说某事是——某事否则,继承不是正确的方法。

【讨论】:

    【解决方案3】:

    任何继承的类构造函数都必须将所需的参数传递给基类。 因此 public Director : base (此处为必填值)。 如果您不想将值传递给 Director 构造函数,则可以这样做。 但是,您仍然必须将某些内容传递给基类。 例如:

    using System;
    
    public class Program
    {
        public static void Main()
        {
            var bob = new Director("Bob");
            Console.WriteLine(bob.Salary);
    
        }
    }
    
    public class Employee
    {
        public string Name {get;set;}
        public float Salary {get;set;}
    
        public Employee(string name, float salary)
        {
            Name = name;
            Salary = salary;
        }
    }
    
    public class Director : Employee
    {
        public Director(string name) : base (name, 250000)
        {
    
        }
    }
    

    正如之前的答案所述,最好使用更具描述性的变量,将 Employee/Director 继承到 Bonus 是没有意义的。员工有奖金。当然,我认为一些员工是奖金:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-01
      相关资源
      最近更新 更多