【问题标题】:does not contain a constructor不包含构造函数
【发布时间】:2020-08-25 07:14:24
【问题描述】:

类型定义

using System;
    
public enum Direction { Right, Left, Forward };

class Chaharpa
{
    private int age;
    private int height;
    private int cordinates_x;
    private int cordinates_y;

    public Chaharpa(int a, int b, int x, int y)
    {
        age = a;
        height = b;
        cordinates_x = x;
        cordinates_y = y;
    }

    public Chaharpa(int c, int d)
    {
        age = c;
        height = d;
        cordinates_x = 0;
        cordinates_y = 0;
    }

    public int Age
    {
        get { return age; }
    }

    public int Height
    {
        get { return height; }
    }

    public int Cordinates_x
    {
        get { return cordinates_x; }
        set { cordinates_x = value; }
    }

    public int Cordinates_y
    {
        get { return cordinates_y; }
        set { if (value > 0) cordinates_y = value; }
    }

    public void Move(Direction direction)
    {
        if (direction == Direction.Forward)
            Cordinates_y++;
        else if (direction == Direction.Right)
            Cordinates_x++;
        else if (direction == Direction.Left)
            Cordinates_x--;
    }

    class horse : Chaharpa
    {
        public bool is_wild;

        public void Jump(int x)
        {
            x = Cordinates_y;
            Cordinates_y += 5;
        }

        public void Print()
        {
            Console.WriteLine("Horse Information: Age," + Age + ", Height: " + Height + ", Wild: " + is_wild + ", X: " + Cordinates_x + ", Y: " + Cordinates_y);
        }
    }
}

用法

class Program
{
    static void Main(string[] args)
    {
        int age, x, y, minAge = 0;
        int height;
        bool wild;

        for (int i = 0; i < 3; i++)
        {

            Console.WriteLine("Horse #" + (i + 1));

            Console.Write("Enter Age: ");
            age = int.Parse(Console.ReadLine());
            Console.Write("Enter Height: ");
            height = int.Parse(Console.ReadLine());
            Console.Write("Enter X: ");
            x = int.Parse(Console.ReadLine());
            Console.Write("Enter Y: ");
            y = int.Parse(Console.ReadLine());
            Console.Write("Is Wild: ");
            wild = bool.Parse(Console.ReadLine());

            minAge = age;
            if (minAge > age)
            {
                minAge = age;
            }
        }

        Console.WriteLine();

        horse ob = new horse();

        ob.Jump(minAge);

        ob.move();

        ob.Print();

        Console.ReadKey();
    }
}

我在 Visual Studio 中遇到这些错误:

'Chaharpa' 不包含带 0 个参数的构造函数

找不到类型或命名空间名称“horse”(您是否缺少 using 指令或程序集引用?)

找不到类型或命名空间名称“horse”(您是否缺少 using 指令或程序集引用?)

【问题讨论】:

  • Class horse嵌套 在 class Chaharpa 中的,它不是公开的。因此,它在 Main 中不可见。使其成为公开的一级课程(对于初学者)。然后你从 Chaharpa 派生 horse(应该是 Horse),而不指定 CTOR。这就是为什么将为您创建一个默认 ctor,它会尝试调用其父级(默认)ctor,它不存在,因为您定义了两个参数化 ctor。
  • Chaharpa 没有采用 0 个参数的构造函数。它有带有 2 或 4 个参数的构造函数。 horse 类必须调用其中之一,但甚至没有自己的构造函数
  • 如果在一个类(马类)中不声明任何构造函数,编译器会自动提供一个公有参少构造函数(没有参数少构造函数就是Chaharpa类)。
  • c# 的命名约定:PublicThingsPascalCase、nonPublicThingsCamelCase,避免在包含小写字母的名称中使用下划线
  • 这是请求。马类错了。

标签: c# class constructor


【解决方案1】:
  1. Chaharpa 类中,您定义了两个构造函数,都带有参数。 创建自己的构造函数会覆盖默认构造函数。 通常,当从基类继承时,您希望使用用于初始化基类的参数来初始化继承类,请查看此thread

  2. ChaharpaChaharpaclass 中的 horse 类不公开。 将类 horseChaharpa 更改为 public 并以以下方式访问它: Chaharpa.horse ob = new Chaharpa.horse(); 是正确的方法。

以下是对代码的一些缓解措施:

using System;
using System.ComponentModel;

public enum Direction
{ Right, Left, Forward };
public class Chaharpa
{
    private int age;
    private int height;
    private int cordinates_x;
    private int cordinates_y;
    public Chaharpa()
    {
    }

    public Chaharpa(int a, int b, int x, int y)
    {
        age = a;
        height = b;
        cordinates_x = x;
        cordinates_y = y;
    }
    public Chaharpa(int c, int d)
    {
        age = c;
        height = d;
        cordinates_x = 0;
        cordinates_y = 0;
    }
    public int Age
    {
        get { return age; }
    }

    public int Height
    {
        get { return height; }
    }

    public int Cordinates_x
    {
        get { return cordinates_x; }
        set { cordinates_x = value; }
    }
    public int Cordinates_y
    {
        get { return cordinates_y; }
        set { if (value > 0) cordinates_y = value; }
    }

    public void Move(Direction direction)
    {
        if (direction == Direction.Forward)
            Cordinates_y++;
        else if (direction == Direction.Right)
            Cordinates_x++;
        else if (direction == Direction.Left)
            Cordinates_x--;
    }
    public class horse : Chaharpa
    {
        public bool is_wild;

        public void Jump(int x)
        {
            x = Cordinates_y;
            Cordinates_y += 5;        
        }

        public void move()
        {
            throw new NotImplementedException();
        }

        public void Print()
        {
            Console.WriteLine("Horse Information: Age," + Age + ", Height: " + Height + ", Wild: " + is_wild + ", X: " + Cordinates_x + ", Y: " + Cordinates_y);
        }
    }
}


class Program
{
    static void Main(string[] args)
    {
        int age, x, y, minAge = 0;
        int height;
        bool wild;

        for (int i = 0; i < 3; i++)
        {

            Console.WriteLine("Horse #" + (i + 1));

            Console.Write("Enter Age: ");
            age = int.Parse(Console.ReadLine());
            Console.Write("Enter Height: ");
            height = int.Parse(Console.ReadLine());
            Console.Write("Enter X: ");
            x = int.Parse(Console.ReadLine());
            Console.Write("Enter Y: ");
            y = int.Parse(Console.ReadLine());
            Console.Write("Is Wild: ");
            wild = bool.Parse(Console.ReadLine());

            minAge = age;
            if (minAge > age)
            {
                minAge = age;
            }
        }
        Console.WriteLine();

        Chaharpa.horse ob = new Chaharpa.horse();

        ob.Jump(minAge);
                
        // You can call the Move defined in Chaharpa
        ob.Move(<PASS DIRECTION PARAMETER HERE>);

        ob.Print();

        Console.ReadKey();

    }
}

【讨论】:

  • 每个类使用一个文件不是最佳实践,它只是一种常见的代码组织实践。当类很小时,将相关定义放在同一个文件中是非常有意义的。现在 C# 9 记录允许用单行定义一个类,预计这会更常见
  • 事实上,如果您查看 .NET 自己的 Github 存储库,您会发现 许多 文件包含多个紧密相关的类
  • 完全不嵌套类可能是更好的方法
  • @PanagiotisKanavos 我同意在 C# 中将类放在同一个文件中是可以的,有时甚至会有所帮助。我想就如何更好地组织它的代码给 OP 一些建议,作为定义的对象类 'Chaharpa' 和 'horse' 上的主程序类 - 绝对不应该驻留在同一个文件中。
【解决方案2】:

首先,您必须学习更多关于在 C# 中创建对象的知识。在子类构造函数中创建子类对象时,调用的是基类构造函数。

using System;
                    
public class Program
{
    public static void Main()
    {
        Child child = new Child();
    }
}

public class Parent{
    
    public Parent(){
        Console.WriteLine("I am parent");
    }
    
}

public class Child : Parent {
    public Child(){
        Console.WriteLine("I am child");
    }
}

输出

I am parent
I am child

当你创建一个类时,会有一个默认的构造函数(这个构造函数接受 0 个参数)。但是当你创建另一个构造函数(它需要超过 0 个参数)时,默认构造函数将被替换。您必须手动创建默认构造函数。所以在运行时,它试图调用基类构造函数的默认构造函数。但既然它消失了,就会发生这个错误。

  • 'Chaharpa' 不包含采用 0 个参数的构造函数

那你得学习一下 C# 中的嵌套类。

你不能只通过它的名字来调用内部类。您必须从外部类中引用它。

using System;
                    
public class Program
{
    public static void Main()
    {
        Outer.Inner child = new Outer.Inner();
    }
}

public class Outer{
    
    public Outer(){
        Console.WriteLine("I am outer");
    }
    
    public class Inner : Outer {
        public Inner(){
            Console.WriteLine("I am inner");
        }
    }
}

输出

I am outer
I am inner

这就是您收到错误The type or namespace name 'horse' could not be found (are you missing a using directive or an assembly reference?)的原因

此代码将起作用。

using System;
public enum Direction
{ Right, Left,Forward};


 class Chaharpa
{
    private int age;
    private int height;
    private int cordinates_x;
    private int cordinates_y;
     
     public Chaharpa(){}
     
    public Chaharpa(int a, int b, int x, int y)
    {
    age = a;
    height = b;
    cordinates_x = x;
    cordinates_y = y;
    }
    public Chaharpa(int c, int d)
    {
        age = c;
        height = d;
        cordinates_x = 0;
        cordinates_y = 0;
    }
    public int Age
    {
        get{ return age; }
    }

    public int Height
    {
        get{ return height;}
    }

    public int Cordinates_x
    {
        get{ return cordinates_x;}
        set{ cordinates_x = value;}
    }
    public int Cordinates_y
    {
        get{return cordinates_y;}
        set{ if (value > 0)cordinates_y = value;}
    }

    public void Move(Direction direction)
    {
        if (direction == Direction.Forward)
            Cordinates_y++;
        else if (direction == Direction.Right)
            Cordinates_x++;
        else if (direction == Direction.Left)
            Cordinates_x--;
    }
     
     public class horse : Chaharpa
    {
        public bool is_wild;
       
        public void Jump(int x)
        {
            x = Cordinates_y;
            Cordinates_y += 5;
        }
        public void Print()
        {
            Console.WriteLine("Horse Information: Age," + Age + ", Height: " + Height + ", Wild: " + is_wild + ", X: " + Cordinates_x + ", Y: " + Cordinates_y);
        }
        
    }
}


public class Program
{
    public static void Main(string[] args)
    {
        int age, x, y, minAge = 0;
        int height;
        bool wild;
         
        for (int i = 0; i < 3; i++)
        {
            
            Console.WriteLine("Horse #" + (i + 1));
    
            Console.Write("Enter Age: ");
            age = int.Parse(Console.ReadLine());
            Console.Write("Enter Height: ");
            height = int.Parse(Console.ReadLine());
            Console.Write("Enter X: ");
            x = int.Parse(Console.ReadLine());
            Console.Write("Enter Y: ");
            y = int.Parse(Console.ReadLine());
            Console.Write("Is Wild: ");
            wild = bool.Parse(Console.ReadLine());
        
                minAge = age;
            if (minAge > age)
            {
                minAge = age;
            }
        }
        Console.WriteLine();

    Chaharpa.horse ob = new Chaharpa.horse();

    ob.Jump(minAge);
   
    ob.Print();

    }
}

对一个类使用一个文件是一种很好的做法。

【讨论】:

    猜你喜欢
    • 2014-06-30
    • 1970-01-01
    • 2011-12-03
    • 1970-01-01
    • 2013-02-17
    • 2012-08-21
    • 1970-01-01
    • 1970-01-01
    • 2012-08-13
    相关资源
    最近更新 更多