static Prison createSimplePrison() { Prisoner Haskell = new Prisoner("Haskell", "Johnson", 24, "Murder", 7); Prisoner CPP = new Prisoner("Cpp", "Anderson", 44, "Thief", 4); Prisoner Scala = new Prisoner("Scala", "Jason", 36, "Slayer", 9); Prison blackstorm = new Prison(); Console.WriteLine("Put Haskell to A1"); Console.WriteLine("Put Scala to B1"); Console.WriteLine("Put CPP to A2"); blackstorm["A1"] = Haskell; blackstorm["B1"] = Scala; blackstorm["A2"] = CPP; return(blackstorm); }
public int CompareTo(object obj) { if (obj == null) { return(1); // Использование положительных условий } Prisoner otherPrisoner = obj as Prisoner; if (otherPrisoner != null) { return(this.dangerous.CompareTo(otherPrisoner.dangerous)); } else { throw new ArgumentException("Object is not a Prisoner"); } }
public int CompareTo(object obj) { if (obj == null) { return(1); } Prisoner otherPrisoner = obj as Prisoner; if (otherPrisoner != null) { return(this.dangerous.CompareTo(otherPrisoner.dangerous)); } else { throw new ArgumentException("Object is not a Prisoner"); } }
//Все функции принимают 1, 2 аргумента static void Main(string[] args) { Shop mainShop = new Shop(); Policeman Alex = new Policeman(1); Policeman John = new Policeman(2); Policeman Rick = new Policeman(3); Alex.subscribeOnWeaponBought(mainShop); John.subscribeOnWeaponBought(mainShop); Gun darrelGun = mainShop.getGun("Darrel"); // Содержательные имена Gun peterGun = mainShop.getGun("Peter"); darrelGun.safetyOff(); peterGun.safetyOff(); Gun[] testGuns = { darrelGun, peterGun }; for (int i = 0; i < 16; i++) { fireGun(darrelGun); } Action <Gun>[] actions = new Action <Gun>[3] { recharge, fire, fire }; foreach (Action <Gun> act in actions) { act(darrelGun); } Func <Gun, bool> isGood = ((gun) => { return(gun.getPrecision() > 0.999); }); foreach (Gun g in testGuns) // Длина области видимости определяется длиной области видимости { checkGun(g, isGood); // Блоки и отсупы } Console.WriteLine("prisoners test"); Prison blackstorm = createSimplePrison(); Console.WriteLine("Prisoner from B1"); Prisoner p1 = blackstorm["B1"]; Console.WriteLine(p1.FirstName + " " + p1.LastName); Console.WriteLine("-------Iterate prison-------"); foreach (Prisoner p in blackstorm) { Console.WriteLine(p.FirstName + " " + p.LastName + " " + p.Dangerous); } Console.WriteLine("Total dangerous: " + blackstorm.getTotalDangerous()); Console.WriteLine("Total size before garbage collection: " + GC.GetTotalMemory(false)); GC.Collect(); Console.WriteLine("Total size after garbage collection: " + GC.GetTotalMemory(true)); Gun g1 = new Gun("1 gun"); Gun g2 = new Gun("2 gun"); Gun g3 = new Gun("3 gun"); WeakReference wr = new WeakReference(new Gun("4 gun")); g1 = null; g2 = null; Console.WriteLine("Total size before garbage collection: " + GC.GetTotalMemory(false)); GC.Collect(); Console.WriteLine("Total size after garbage collection: " + GC.GetTotalMemory(true)); //Увидев закомментированный код, удалите его saveBinary(blackstorm); saveJson(blackstorm); Console.In.Read(); Alex.Dispose(); John.Dispose(); Rick.Dispose(); }
static void Main(string[] args) { Shop mainShop = new Shop(); Policeman Alex = new Policeman(1); Policeman John = new Policeman(2); Policeman Rick = new Policeman(3); Alex.subscribe_on_shop_weapon_bought(mainShop); John.subscribe_on_shop_weapon_bought(mainShop); Gun my_gun = mainShop.get_gun("Darrel"); Gun new_gun = mainShop.get_gun("Peter"); my_gun.safety_off(); new_gun.safety_off(); Gun[] guns = { my_gun, new_gun }; for (int i = 0; i < 16; i++) { try { my_gun.fire(); } catch (NoAmmoException e) { Console.WriteLine("NoAmmoException exception caught"); Console.WriteLine(String.Format("Bullets ended on " + e.Args.Time.ToString())); my_gun.recharge(); } } Action <Gun>[] actions = new Action <Gun>[3] { recharge, fire, fire }; foreach (Action <Gun> act in actions) { act(my_gun); } Func <Gun, bool> isGood = ((gun) => { return(gun.get_precision() > 0.999); }); foreach (Gun g in guns) { if (isGood(g)) { Console.WriteLine("Good gun"); } else { Console.WriteLine("Bad gun"); } } Console.WriteLine("prisoners test"); Prison blackstorm = create_simple_prison(); Console.WriteLine("Prisoner from B1"); Prisoner p1 = blackstorm["B1"]; Console.WriteLine(p1.FirstName + " " + p1.LastName); Console.WriteLine("-------Iterate prison-------"); foreach (Prisoner p in blackstorm) { Console.WriteLine(p.FirstName + " " + p.LastName + " " + p.Dangerous); } Console.WriteLine("Total dangerous: " + blackstorm.total_dangerous()); save_binary(blackstorm); save_json(blackstorm); Console.In.Read(); }