如何在c#中将不同的对象传递给单个对象数组

时间:2017-11-20 18:46:09

标签: c# arrays

我想将所有对象组合在一起,但我认为我的方式是错误的,我的老师没有解释很多关于c sharp的对象,我只是想知道创建,分组和创建的最佳方法是什么显示这些对象?任何帮助非常感谢,我不知道如何将对象传递给数组但我已经在网上寻找我没有找到的答案然后我正在查看数组列表但我不确定如何初始化或利用它们或如果我甚至应该使用它们并感觉我在这里输了

using System;

namespace delivery
{
public abstract class Package
{
    public string fullName { get; set; }
    public string address { get; set; }
    public string city { get; set; }
    public string state { get; set; }
    public string zipCode { get; set; }
    public double weight { get; set; }
    public double costPerOunce { get; set; }

    public Package(string fullName, string address, string city, string state, string zipCode)
    {
        this.fullName = fullName;
        this.address = address;
        this.city = city;
        this.state = state;
        this.zipCode = zipCode;
        this.costPerOunce = 10.00;
    }

    public Package(double weight)
    {
        this.weight = weight;
    }

    public virtual double CalculateCost()
    {
        double cost;
        cost = this.weight * this.costPerOunce;
        return cost;
    }
}

public class TwoDayPackage : Package
{
    public double flatFee { get; set; }

    public TwoDayPackage(string fullName, string address, string city, string state, string zipCode) : base(fullName, address, city, state, zipCode)
    {

    }

    public TwoDayPackage(double weight) : base(weight)
    {
        this.flatFee = 10.00;
    }

    public override double CalculateCost()
    {
        double cost = base.CalculateCost() + flatFee;
        return cost;
    }

}

public class OvernightPackage : Package
{
    public double feePerOunce { get; set; }

    public OvernightPackage(string fullName, string address, string city, string state, string zipCode) : base(fullName, address, city, state, zipCode)
    {

    }

    public OvernightPackage(double weight) : base(weight)
    {
        this.feePerOunce = .10;
    }

    public override double CalculateCost()
    {
        double cost = (this.weight * this.feePerOunce) + base.CalculateCost();
        return cost;
    }
}

class MainClass
{
    public static void Main(string[] args)
    {


    }

    public static int ShowMenu()
    {
        int option = 0;
        Console.WriteLine("1.) Create Parcels");
        Console.WriteLine("2.) Display Shipping Details");
        Console.WriteLine("3.) Cost Analysis ");
        Console.WriteLine("4.) Display Couriers Details");
        Console.WriteLine("5.) Exit");
        option = int.Parse(Console.ReadLine());
        return option;
    }

    public static void ProcessMenu()
    {
        int response = ShowMenu();
        do
        {
            switch (response)
            {
                case 1:
                    CreateParcels();
                    break;
                case 2:
                    DisplayShippingDetails();
                    break;
                case 3:
                    CostAnalysis();
                    break;
                case 4:
                    DisplayCouriersDetails();
                    break;
                case 5:
                    Exit();
                    break;
                default:
                    Console.WriteLine("invalid option\n");
                    break;
            }
        } while (response != 5);
    }

    public static void CreateParcels()
    {
        TwoDayPackage s1 = new TwoDayPackage("s1","add1","city1","state1","zip1");
        TwoDayPackage p1 = new TwoDayPackage(5.0);
        TwoDayPackage r1 = new TwoDayPackage("r1", "add2", "city2", "state2", "zip2");
    }
    public static void DisplayShippingDetails()
    {

    }
    public static void CostAnalysis()
    {

    }
    public static void DisplayCouriersDetails()
    {

    }
public static void Exit()
    {
        Environment.Exit(0);
    }
}
} 

2 个答案:

答案 0 :(得分:0)

回答有关如何将对象传递到数组的问题。让我们带你的

拿你的包类,然后假设你想制作一个数组。

1.Declare类型数组符号和数组的名称:

Package[] myPackageArray = new Package[3];

所以知道你让你的阵列能够容纳4个包。它是4,因为数组和列表从0开始。

所以如果你想添加一个包,你需要告诉数组把它放在哪里。

Package LApackage = new Package("James","fake st","LA","CA","90210")
Package Neighborpackage = new Package("Mo","1 fake st","LA","CA","90210")

让我们在第一个索引添加它:

myPackageArray[0] = LApackage;

 myPackageArray[1] = Neighborpackage;

现在通过它们使用foreach循环:

foreach (Package item in  myPackageArray )
    {
              Console.WriteLine(item.name);//you can choose any propety

    }

答案 1 :(得分:0)

我会使用类型对象列表:

var stuff = List<object>() 

填写你想要的任何东西。然后迭代它:

foreach(var thing in stuff)
if (thing.GetType() == typeof(SpecificThing))
{
     var tmpThing = thing as SpecificThing
     //Do stuff with tmpThing
}

然后你可以使用Lists .ToArray()方法提取你想要的任何“东西”并将它们转换为数组。

var _SpecificThings = stuff.Select(t => t.GetType() == typeof(SpecificThing)).ToArray()