使用类的相同对象,但成员不同

时间:2019-02-10 06:59:35

标签: c# console-application

好的,所以我想在一个类中使用五次相同的对象。它们都具有相同的属性,但是它们只是多次出现。

WITH f (
    SELECT  
        /*+ BROADCAST(h) */
        /*+ COALESCE(36) */
        CONCAT(f.outboundlegid, '-', f.inboundlegid, '-', f.agent) AS key,
        f.querydatetime,

        f.outboundlegid,
        f.inboundlegid,
        f.agent,
        f.queryoutbounddate,
        f.queryinbounddate,
        f.price,
        f.outdeparture,
        f.outarrival,
        f.indeparture,
        f.inarrival,
        f.querydestinationplace,
        CASE WHEN type = 'HOLIDAY' AND (out_date BETWEEN start AND end)
            THEN true
            ELSE false
            END out_is_holiday,
        CASE WHEN type = 'LONG_WEEKENDS' AND (out_date BETWEEN start AND end)
            THEN true
            ELSE false
            END out_is_longweekends,
        CASE WHEN type = 'HOLIDAY' AND (in_date BETWEEN start AND end)
            THEN true
            ELSE false
            END in_is_holiday,
        CASE WHEN type = 'LONG_WEEKENDS' AND (in_date BETWEEN start AND end)
            THEN true
            ELSE false
            END in_is_longweekends
    FROM flights f
    CROSS JOIN holidays h
    LIMIT 10
)
 SELECT
    /*+ BROADCAST(a) */
    /*+ BROADCAST(p) */
    key,
    querydatetime,
    first(outboundlegid) as outboundlegid,
    first(inboundlegid) as inboundlegid,
    first(agent) as agent,
    first(p.countryName) as countryName,
    first(p.airportName) as airportName,
    first(a.name) as agentName,
    first(queryoutbounddate) as queryoutbounddate,
    first(queryinbounddate) as queryinbounddate,
    first(price) as price,
    first(outdeparture) as outdeparture,
    first(outarrival) as outarrival,
    first(indeparture) as indeparture,
    first(inarrival) as inarrival,
    first(querydestinationplace) as querydestinationplace,
    CASE WHEN array_contains(collect_set(out_is_holiday), true)
        THEN true
        ELSE false
        END out_is_holiday,
    CASE WHEN array_contains(collect_set(out_is_longweekends), true)
        THEN true
        ELSE false
        END out_is_longweekends,
    CASE WHEN array_contains(collect_set(in_is_holiday), true)
        THEN true
        ELSE false
        END in_is_holiday,
    CASE WHEN array_contains(collect_set(in_is_longweekends), true)
        THEN true
        ELSE false
        END in_is_longweekends
FROM f
INNER JOIN agents a
ON f.agent = a.id
INNER JOIN airports p
ON f.querydestinationplace = p.airportId
GROUP BY 
    querydatetime, 
    key

Troops类包含两个属性:Name和Health。我希望所有的猴子都具有不同的实例,但又不要做出不同的变量(例如monkey1,monkey2)或不使用数组。所有的猴子都受到不同程度的伤害,但是现在,伤害正在累积。

代码:https://dotnetfiddle.net/YRukEz

2 个答案:

答案 0 :(得分:0)

产生不同输出的原因是填充Troop对象列表的方式:

 for(int i=0;i<5;i++){
     army1.Add(monkey);
     army1.Add(flyingMonkey);
     army1.Add(wizard);
     army1.Add(balloon);
 }

您正在循环进行 五次迭代。因此,您的每个Troop实例将被添加到列表中 5次(留下一个包含20个元素的army1列表)。因此,您的Troop元素中的每一个元素都必须被删除5次,直到它们不再出现在列表中。

只需删除循环,代码2中得到的行数将与代码1中生成的行数匹配。

 army1.Add(monkey);
 army1.Add(flyingMonkey);
 army1.Add(wizard);
 army1.Add(balloon);

话虽如此,在循环中添加单位不一定是错误的。在阅读代码时,我首先想到的是,您正在尝试创建一支包含数个相同类型单位的部队(四只猴子,四只飞行的猴子,四个巫师和四个气球)。

您可以这样做,但是请注意,您这样做的方式是,您的军队不会包含四只不同的猴子,而是四只相同的猴子,因为您要添加同一对象实例四次。当您减少其中之一的运行状况时,您会看到该变化反映在同一类型的其他三个项目中。

要生成一支具有多个相同类型单位的军队,您必须在每次迭代中创建单位,而不是通过将相应的初始化代码也放入循环中来

for(int i=0;i<5;i++){
    Troop monkey = new Troop();
    Troop flyingMonkey = new Troop();
    Troop wizard = new Troop();
    Troop balloon = new Troop();

    monkey.name = "Monkey " + i;
    flyingMonkey.name = "Flying Monkey " + i;
    wizard.name = "Wizard " + i;
    balloon.name = "Balloon " + i;

    monkey.health = 50;
    flyingMonkey.health = 50;
    wizard.health = 60;
    balloon.health = 55;

    army1.Add(monkey);
    army1.Add(flyingMonkey);
    army1.Add(wizard);
    army1.Add(balloon);
}

编辑:顺便说一句,在您的代码和课程的当前阶段,您可以轻松编写此代码,而无需任何变量来创建单个部队:

for (int i=0; i<5; i++){
    army1.Add(new Troop
    {
        name = "Monkey " + i,
        health = 50
    });
    army1.Add(new Troop
    {
        name = "Flying Monkey " + i,
        health = 50
    });
    army1.Add(new Troop
    {
        name = "Wizard " + i,
        health = 60
    });
    army1.Add(new Troop
    {
        name = "Balloon " + i,
        health = 55
    });
}

答案 1 :(得分:0)

您有4组向导,猴子,飞行猴子和气球(20个部队装备)。

 for(int i=0;i<5;i++)
 {
             army1.Add(monkey);
             army1.Add(flyingMonkey);
             army1.Add(wizard);
             army1.Add(balloon);
 }

这意味着,每个人都必须死4次,然后才能再次露面。但是在您的非Class代码中,您只有4个。

var health = new List<int>() {50,50,60,55};

这说明了为什么向导会在非类代码中早死,而又不断返回(嗯,实际上,它不是向导的另一个实例)