繁体   English   中英

在C#中访问对象的'名称'属性

[英]Accessing the 'Name' property of an object in C#

我正在学习C#(是我的新手),并且正在构建一个小型Airport应用程序。 我希望能够获取机场中飞机的名称,而不是仅仅知道列表包含各种Airport.Plane对象。 目前,我的代码是:

Airport.cs:

using System;
using System.Collections.Generic;

namespace Airport
{
    public class Airport
    {
        public List<object> planes;

        public Airport()
        {
            planes = new List<object>();
        }

        public List<object> Land(object plane)
        {
            planes.Add(plane);
            Console.WriteLine("Currently in the airport:");
            planes.ForEach(Console.WriteLine);
            return planes;
        }

        public List<object> TakeOff(object plane)
        {
            planes.Remove(plane);
            Console.WriteLine("Currently in the airport:");
            planes.ForEach(Console.WriteLine);
            return planes;
        }

        public int GetPlaneCount()
        {
            Console.WriteLine(planes.Count);
            return planes.Count;
        }
    }
}

Plane.cs:

using System;
namespace Airport
{
    public class Plane
    {
        public string Name { get; set; }

        public Plane(string name)
        {
            Name = name;
        }
    }
}



Program.cs中:

using System;

namespace Airport
{
    class Program
    {
        static void Main(string[] args)
        {
            Airport airport = new Airport();
            Plane plane = new Plane("Private Jet");
            airport.Land(plane);
            airport.TakeOff(plane);
            airport.GetPlaneCount();
        }
    }
}


我试图通过Console.Log(plane.Name);访问机场中飞机的名称Console.Log(plane.Name); 但出现以下错误:“'对象'不包含名称的定义”。 任何帮助将非常感激!

谢谢

一些伪代码可以帮助您理解

public class Airport
{
    public string AirportName { get; set;}

    public List<Plane> planes;

    public Airport(string airportName)
    {
        AirportName = airportName;
        planes = new List<Plane>();
    }

    public List<Plane> Land(Plane plane)
    {
        planes.Add(plane);
        return planes;
    }

    public List<Plane> TakeOff(Plane plane)
    {
        planes.Remove(plane);
        return planes;
    }

    public int GetPlaneCount()
    {
        Console.WriteLine(planes.Count);
        return planes.Count;
    }
}

public void Main()
{
    var capeTownAirport = new Airport("Cape Town")

    var planeA = new Plane("facy plane");
    var planeB = new Plane("Another facy plane");

    var currentPlans = capeTownAirport.Land(planeA);            

    var currentPlans2 = capeTownAirport.Land(planeA);

    foreach(var plane in currentPlans2)
    {
         Console.WriteLine(plane.Name);
    }
}

基本上, C#在大多数情况下是一种静态类型的编程语言 ,因此,如果将某些内容声明为对象,则只会看到对象的属性,字段和方法。 您可以通过将对象投射到“平面”来轻松解决此问题,但是坚持使用已知类型始终是最好的方法。

var plane = new Plane("My plane");

object myObject = plane;
// Console.WriteLine(myObject.Name); // Compiler complains here - object doesn't have Name property or field defined

Console.WriteLine((Plane)myObject); // Compiler is happy here, but you should never do it like that!

正如您对问题的评论中所说的那样,请使用强类型列表(使用List <Plane>而不是List <object>),它将可以正常工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM