动态创建更多对象

时间:2017-11-21 17:15:27

标签: c# object dynamic

所以我的想法是创建Runner类的更多实例,但是当我这样做并列出我创建的所有对象时,它们都是我创建的最后一个。据我所知,这是因为对象是引用类型。但有什么方法可以做到这一点?

class Marathon
{
    static void Main(string[] args)
    {
        List < Runner > athleteList = new List<Runner>();
        int opcao=0;
        while (true)
        {
            Console.WriteLine("1-Registar Atleta Profissional\n2-Registar Atleta Amador\n3-Registar Fancy Dresser\n4-Listar Atletas Inscritos\n5-Registar Fim de prova de um atleta\n6-Sair");
            opcao = Convert.ToInt32(Console.ReadLine());
            switch(opcao)
            {
                case 1:
                    Console.WriteLine("Introduza o numero do Atleta:");
                    int numeroAtletaP = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine("Introduza o ranking to atleta:");
                    int rankingAtletaP = Convert.ToInt32(Console.ReadLine());
                    athleteList.Add(new Professional(rankingAtletaP,numeroAtletaP));

                    break;

                case 2:
                    SponsorshipForm spFa = new SponsorshipForm();
                    Console.WriteLine("Introduza o numero do Atleta:");
                    int numeroAtletaA = Convert.ToInt32(Console.ReadLine());
                    athleteList.Add(new Amateur(numeroAtletaA,spFa));
                    break;

                case 3:
                    SponsorshipForm spfFD = new SponsorshipForm();
                    Console.WriteLine("Introduza o numero do Atleta:");
                    int numeroAtletaFD = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine("Costume:");
                    string costume =Console.ReadLine();
                    athleteList.Add(new FancyDresser(numeroAtletaFD, spfFD, costume));
                    break;

                case 4:
                    foreach (Runner athlete in athleteList)
                    {
                        Console.WriteLine(athlete.toString());
                    }
                    break;

                case 5:
                    Console.WriteLine("Introduza o numero do Atleta:");
                    int numeroAtleta = Convert.ToInt32(Console.ReadLine());
                    foreach (Runner athlete in athleteList)
                    {
                        if (numeroAtleta == athlete.getNumber())
                        {
                            Console.WriteLine("Hora chegada:");
                            int horaChegada = Convert.ToInt32(Console.ReadLine());
                            Console.WriteLine("Minuto chegada:");
                            int minutoChegada = Convert.ToInt32(Console.ReadLine());
                            Console.WriteLine("Segundo chegada:");
                            int segundoChegada = Convert.ToInt32(Console.ReadLine());
                            athlete.finish(horaChegada, minutoChegada, segundoChegada);
                        }
                        else
                        {
                            Console.WriteLine("Atleta não existe.");
                        }
                    }
                    break;

                case 6:
                    System.Environment.Exit(1);
                    break;

                default:
                    Console.WriteLine("Opcao invalida.");
                    break;
            }
        }
    }
}

Runner class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Marathon
{
    class Runner
    {
         static public int number;
         static public bool finished;
         static public int hours;
         static public int minutes;
         static public int secs;

        public Runner(int participationNumber)
        {
            number = participationNumber;
        }



        public int getNumber()
        {
            return number;
        }
        public void finish(int fHour, int fMinute, int fSecs)
        {
            hours = fHour;
            minutes = fMinute;
            secs = fSecs;
            finished = true;
        }

        public string toString()
        {
            return string.Format("Number: {0}, Finished:{1} Time:{2}:{3}:{4}", number, finished, hours, minutes, secs);

        }


    }


}

专业课

namespace Marathon

{
    class Professional : Runner
    {
        private int worldranking;

        public Professional(int worldRanking, int pNumber) : base (number)
        {
            number=pNumber;
            worldranking = worldRanking;
        }

        public string toString()
        {
            return string.Format("Number: {0}, Finished:{1} Time:{2}:{3}:{4}\nWorld Ranking: {5}", number, finished, hours, minutes, secs, worldranking);

        }
    }
}

业余班

namespace Marathon

{
    class Amateur : Runner
    {
        static public SponsorshipForm sponsForm;

        public Amateur(int aNumber,SponsorshipForm SponsForm) : base(number)
        {
            aNumber = number;
            sponsForm = SponsForm;
        }

        public string toString()
        {
            return string.Format("Number: {0}, Finished:{1} Time:{2}:{3}:{4}", number, finished, hours, minutes, secs);

        }

Fancy Dresser Class

namespace Marathon

{
    class FancyDresser : Amateur
    {
        string costume;
    public FancyDresser(int fnumber, SponsorshipForm sponsorsForm, string pCostume) : base(number,sponsForm)
        {
            number = fnumber;
            sponsForm = sponsorsForm;
            costume = pCostume;
        }
    }
}

目前尚未使用SponsorshipForm类

1 个答案:

答案 0 :(得分:0)

您无缘无故地使用静态变量。 只需更改您的代码:

    static public int number;
    static public bool finished;
    static public int hours;
    static public int minutes;
    static public int secs;

    public Runner(int participationNumber)
    {
        number = participationNumber;
    }

要:

    public int number;
    public bool finished;
    public int hours;
    public int minutes;
    public int secs;

    public Runner(int participationNumber)
    {
        number = participationNumber;
    }

并更改以下签名:

public Professional(int worldRanking, int pNumber) : base(pNumber)

public Amateur(int aNumber, SponsorshipForm SponsForm) : base(aNumber)

public FancyDresser(int fnumber, SponsorshipForm sponsorsForm, string pCostume) : base(fnumber, sponsForm)

请记住,静态属性将在该类的每个实例之间共享,您不需要它。

检查SponsorshipForm,它是静态的,也是唯一的。