【问题标题】:I can't solve error CS7036 in unity: There is no argument given that corresponds to the required formal parameter [closed]我无法统一解决错误CS7036:没有给出与所需形式参数相对应的参数[关闭]
【发布时间】:2021-12-08 15:39:11
【问题描述】:

我在统一控制台中收到此错误,但我找不到问题所在。我认为一切都是正确的?我只是从 udemy 的 curs 中写下来,他使用 Visual Studio,而我使用 vsc,我认为这不是问题。但它显然是相同的,它在教程中的外观。感谢帮助。

1/2 properties.cs:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Properties : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        /* EIGENSCHAFTEN (C# Properties) */

        Student Jordan = new Student("Jordan", "Smith", 31);

        Debug.Log(Jordan.FirstName);
        Debug.Log(Jordan.LastName);
        Debug.Log(Jordan.FullName);
        Debug.Log(Jordan.Age);




    }
}

2/2 student.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Student
{

    public string FirstName { get; set; }
    public string LastName { get; set; }

    public string FullName { get { return this.FirstName + " " + this.LastName; } }

    public int Age { get; private set; }

    public Student(string firstName, string lastName, string fullName, int age)
    {
        FirstName = firstName;
        LastName = lastName;
        Age = age;
    }

}

统一错误 -> 错误 CS7036:没有给出与“Student.Student(string, string, string, int)”所需的形参“age”相对应的参数

【问题讨论】:

  • 您在 `Student("Jordan", "Smith", 31);` 中缺少一个参数;` 构造函数有 4 个参数而不是 3 个
  • 不清楚为什么您希望调用者在计算时传递fullName,所以也许您打算删除该参数?

标签: c# unity3d


【解决方案1】:

您只向构造函数传递了 3 个参数,但您用 4 个参数定义了它

由于您有 FullName 的自定义 getter,我想您最好的解决方案是将其从构造中删除

public Student(string firstName, string lastName, int age)
{
    FirstName = firstName;
    LastName = lastName;
    Age = age;
}

替代方案只是重载构造函数并为其编写另一种组合

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-10
    • 2021-07-18
    • 1970-01-01
    • 2019-10-22
    • 2017-07-18
    • 1970-01-01
    相关资源
    最近更新 更多