【问题标题】:I am having trouble instantiating methods.我无法实例化方法。
【发布时间】:2016-01-08 00:52:28
【问题描述】:

为了说明问题,我构建了一个 ConsoleApplication。

在我看来,我应该能够实例化这样的方法:

Vehicles.Car car = new Vehicles.Car();

但它不起作用。对于我的生活,我看不出有什么问题。

Program.cs contains this:

namespace ConsoleApplicationTest
{
    public class Program
    {
        static void Main(string[] args)
        {
            Vehicles.Car car = new Vehicles.Car();
        }
    }
}

Vehicles.cs 包含以下内容:

using System;

namespace ConsoleApplicationTest
{
    public class Vehicles
    {
        public void Car()
        {
            Console.WriteLine("I am a car");
        }
    }
}

【问题讨论】:

  • 我忘了包含错误。错误 CS0426 类型名称“汽车”在类型“车辆”中不存在
  • 澄清您真正想要实现的目标可能会有所帮助 - 确实没有称为“实例化方法”的通用概念。根据示例代码,可以尝试使用委托、静态方法、类构造函数、命名空间或完全不同的东西。
  • 在我学习的过程中,我的词汇量没有达到标准。感谢您在我的术语方面帮助我。

标签: c# module instantiation


【解决方案1】:

您将Vehicles 用作命名空间,将Car 用作类。

应该是

var vehicles = new Vehicles();
vehicles.Car();

来自您问题中显示的代码。然而,这在我假设的目标中遗漏了许多方面,但在这个狭窄的范围内,这就是这里的问题。

【讨论】:

  • 谢谢!我真的很困惑!
【解决方案2】:

你也可以这样用:

class Program
{
    static void Main(string[] args)
    {
        Vehicles v = new Vehicles();
        v.Car();
    }
}

【讨论】:

  • 谢谢!您的扩展答案确实帮助我理解了您在说什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-03
  • 2020-06-30
  • 1970-01-01
  • 2016-06-09
  • 2020-10-26
  • 2016-11-22
  • 2016-02-29
相关资源
最近更新 更多