C# ????????????? ??????????: ????????? ??????????? ???????????. ??????? #15

C# ????????????? ??????????: ????????? ??????????? ???????????. ??????? #15

????????

????????????, ??? ????? ???? ???????????? ?? ???????? ???? ?????. ??????? ?????, ??? ????????? ??????? ??????? ????? ????????, ????? ??????? ?????????? ??????????? ?? ????????????? - ????????, ???????, ??????????? ??????. ??????? ?? ???????????? ???? ?????? ??????? ?? ????? ??????? "???????" ???, ???? ?????????.

??????????????, ???????? ???? ?????? ????? "?????????????". ????? ????? ?????????? ?????????, ???????? ????????? ????????? ??????????, ?? ???? "????????" ???, ???? ????????? ???? ??????????. ?? ??????????? ???? ?????????, ????? "???????????" ?? ??????????? ?? ???????? ?????????? ????.

1. ????????? ?????????? ????? ????????

????????????, ??? ????? ???? ?????? ?? ?????? ????? ??????. ????? ??????? ??????? ?? ???????? ???????????? ????? ?????????. ????? ?? ????????? ?????? ??????? ?? ???? ?????? ?????? ????????.

??????????????, ??????? ???????? ?????? ????? ???????? (?? "??????"), ?? ?????? ??????? ????????.

?????????? ????????

class Home
{
    public void DrinkWater()
    {
        Console.WriteLine("?????? ?????");
    }

    public void DoMorningRoutine()
    {
        DrinkWater(); // ??????? DrinkWater ??????
        Console.WriteLine("????????? ????? ??????");
    }
}        

?? ?????????, DoMorningRoutine ?????? ??????? DrinkWater ?????? ???, ?????? ????? ??????? ?????????? ???????????? ????? ????????? ????? ??????? ????.

2. ?????????? ???????????? ??????????

????????????, ??? ????? ?????????? ???????? ????????? ???????? ????????. ???????? ???????? ?? ??????? ???????. ????? ????? ?? ??????? ?? ??????? ??? - ??????, ???????? ?????? ?????????.

??????????????, ??????? ?????? "????????" ???????, ?? ???????, ??? ?? ??????? ????? ????????????, ??????? ????????? ??????????.

?????????? ????????

class Bakery
{
    public int BuyApples()
    {
        return 5; // "????????" ?? ???????? 5 ?????
    }

    public void BakeApplePie()
    {
        int apples = BuyApples(); // ????? ???????? ??????????
        Console.WriteLine($"???????? ?????? ????????? {apples} ??????");
    }
}        

?? BakeApplePie ?????? ??????? BuyApples ??????? ???? ?????????? ????????????.

3. ???? ???????? ??????? ??????????

???? ????????????, ??? ????? ????????? ????? ???, ??? ?????? ?????? ?? ???? - ??????, ?????????? ????????. ????? ?????? ?????????? ?? ?????? ?? ????????? ???????????.

??????????????, ??????? ????????? ???? ???????? (?? "???????? ??????") ??????? ??????????, ???? ?????? ?? ??????? ?? ?????? ???????? ??? ??????.

?????????? ????????

class SpecialTool
{
    public void Use()
    {
        Console.WriteLine("??????? ????????? ?????????");
    }
}

class You
{
    public void DoTask()
    {
        SpecialTool tool = new SpecialTool(); // "?????? ??????????"
        tool.Use(); // ??????? ???????? ?????????
        Console.WriteLine("???????? ????????????");
    }
}        

?? You ????? ????? SpecialTool ??????? ?? ??????? ??? Use ??????.

4. ?????????? ???????????

????????????, ??? ????? ?????????? ???????? ????????? ??? ?????? ???????? - ???????? ?? ???????????. ???????? ???????? ?? ??????? ?????? ???-?????.

??????????????, out ??????????? ?????????? ???????? ??????? "?????????" ????? ???? ???????????.

?????????? ????????

class Store
{
    public void BuyFruits(out int apples, out int oranges)
    {
        apples = 5;
        oranges = 3;
    }
}

class You
{
    public void MakeFruitSalad()
    {
        int appleCount, orangeCount;
        Store store = new Store();
        store.BuyFruits(out appleCount, out orangeCount);
        Console.WriteLine($"???????? ????? ?????? {appleCount} ?????? ?? {orangeCount} ?????????");
    }
}        

?? BuyFruits ?????? "????????" ?? ???????????? out ???????????? ???????????.

5. ???????????? ????????

????????????, ??? ????? ?????? ???????? ?????? ??????? ?? ??????, ??? ????? ????? ????. ??????? ???????? ????????? ???????, ????? ???? ?????.

??????????????, ref ???????? ?????? ?????????? ???????? ???????? ?????? ?????? ???, ??? ??????? ?????? ???? ??????.

class Friend
{
    public void AddMoney(ref int wallet)
    {
        wallet += 50; // ??????? 50 ???? ????????
    }
}

class You
{
    public void IncreaseWealth()
    {
        int myMoney = 100;
        Friend friend = new Friend();
        friend.AddMoney(ref myMoney);
        Console.WriteLine($"???? ????? {myMoney} ????"); // ????????? 150-?
    }
}        

?? AddMoney ?????? ????? myMoney ??????? ???????????? ref ???????? ??????? ???????????.

6. ????????? ??????????? (Overloading)

????????????, ??? ????? ????? ???????????? ????. ?? ????? ????????? ?????? ????, ????, ?? ?????????. ?? ???? ?? ????? ?????????? (???), ?????? ?????????? ????? ????????????.

??????????????, ????????? ??????????? ?????????? ???????? ???????? ???? ?? ????? ??????? ????? ????????? ??????, ???????? ???????? ?????????? ????? ?? ?????????? ????????????.

?????????? ????????

class UniversalKnife
{
    public void Cut(string bread)
    {
        Console.WriteLine($"???? {bread}-?");
    }

    public void Cut(string fruit, int pieces)
    {
        Console.WriteLine($"???? {fruit}-? {pieces} ??????");
    }

    public void Cut(string[] vegetables)
    {
        Console.WriteLine($"???? {string.Join(", ", vegetables)}-?");
    }
}

class Chef
{
    public void PrepareFood()
    {
        UniversalKnife knife = new UniversalKnife();
        knife.Cut("????");
        knife.Cut("?????", 4);
        knife.Cut(new string[] { "???????", "?????????", "?????" });
    }
}        

?? Cut ?????? ????????????? ??????, ?????????? ????? ?? ?????????? ????????????.

?????????? ????????: ??????? ??????????

?????, ???????????? ????? ?? ????????? ??? ????????? ????????? - ?????? ????????????.

using System;

class Kitchen {
  private int eggs = 5;
  private int milk = 1000; // ????????????

  public void CookOmelette(int eggCount, out bool success) {
    if (eggs >= eggCount) {
      eggs -= eggCount;
      Console.WriteLine($"????????? ?????? {eggCount} ????????");
      success = true;
    } else {
      Console.WriteLine("?? ???? ????????? ??????????????????");
      success = false;
    }
  }
  public int MakeMilkshake(int milkAmount) {
    if (milk >= milkAmount) {
      milk -= milkAmount;
      return milkAmount / 250; // ???? ???? ???? 250 ??
    }
    return 0;
  }

  public void AddEggs(ref int eggsToAdd) {
    eggs += eggsToAdd;
    eggsToAdd = 0; // ????? ??????? ???????
    Console.WriteLine($"???? ???????????? ???? {eggs} ???????");
  }

  // ????????? ???????????? ????????
  public void Clean() {
    Console.WriteLine("?????????? ???? ???????????");
  }

  public void Clean(string item) {
    Console.WriteLine($"?????????? {item}-?");
  }

  public void Clean(string[] items) {
    Console.WriteLine($"??????????: {string.Join(", ", items)}");
  }
}
class Chef {
  public void Work() {
    Kitchen kitchen = new Kitchen();
    // ?????????? ?????????? ??????????
    bool omeletteMade;
    kitchen.CookOmelette(3, out omeletteMade);
    if (omeletteMade) {
      Console.WriteLine("?????? ??????!");
    }

    // ?????????? ???????????? ??????????
    int milkshakes = kitchen.MakeMilkshake(750);
    Console.WriteLine($"????????? {milkshakes} ?????????");

    // ???????????? ????????
    int newEggs = 6;
    kitchen.AddEggs(ref newEggs);
    Console.WriteLine($"????????? ????????? ????????????: {newEggs}");

    // ????????? ???????????? ??????????
    kitchen.Clean();
    kitchen.Clean("????????");
    kitchen.Clean(new string[] {
      "?????",
      "????",
      "????"
    });
  }
}
class Program {
  static void Main() {
    Chef chef = new Chef();
    chef.Work();
  }
}        

?? ???????? ?????????? ????? ??????????, ??????? ???? ??????????:

  1. ????????? ?????????? ????? ???????? (Kitchen ??????)
  2. ?????????? ???????????? ?????????? (MakeMilkshake ??????)
  3. ???? ???????? ??????? ?????????? (Chef ????? ??????? Kitchen ?????? ????????)
  4. ?????????? ??????????? (CookOmelette ??????)
  5. ???????????? ???????? (AddEggs ??????)
  6. ????????? ??????????? (Clean ????????)

???????: "???? ???????? ?????? ???????"

????, ???? ?????? ????????? ??????????? ?????????? ???????, ?????? ??????? ??????? ?????? ???????? ???????. ?? ????????? ????:

  1. ???????? ????? ??????? Book ??????? ??????????: Title, Author, Price.
  2. ???????? ????? ??????? Bookstore, ???????? ?????? ???????? ??? ?? ??????? ????????: AddBook: ??????? ???? ????? ?????????. FindBook: ??????? ????? ???????? ???????? ?? ???????? ???. SellBook: ????? ????? (???????? ??? ??????) ?? ???????? ??? ????. GetInventoryValue: ?????? ????????? ???????? ????? ?????? ????? ???????????. GetBookCount: ???????? ???????? ?????????? ?????????.
  3. ???????? ????? ??????? Customer, ???????? ?????? name ?? balance (???????) ?????????.
  4. ???????? ?????? BuyBook Customer ??????, ??????? ?????? ????? Bookstore-??? ?? ?????????? ???????????? ???????.

?????????? ?????????? ????? ????????? ??????????, ??? ????? out ???????????, ref ??????????? ?? ????????? ???????????.

?????????:

  • ?????????? out ????????? FindBook ???????, ??? ????????? ????? ?? ???????, ??????? ?????????, ????? ?? ??? ?????.
  • ?????????? ref ????????? SellBook ???????, ??? ???????? ???????? ???????.
  • ??????????? AddBook ??????, ??? ?????? ?????? ???????? ?????? ???????? ????????????, ????? Book ????????.

?? ??????? ???????????? ?????????? ????? ?? ?????????, ??????? ?????????, ?????? ????????. ???????????!


???????

????????? ?????????? ???? ????????????? ???????????? ??????. ?? ???? ???, ???????? ???? ???????????? ?? ???????? ???? ?????????. ?????? ??????, ???????? ????????? ??????????? ?????????? ?????, ???????? ?????????? ?????????? ??????????:

  1. ????? ???????? ?????????? - ??????? ????????? ??????? ???????? "????? ??????".
  2. ?????????? ???????????? ?????????? - ??????? ????????? ??????????? ?????? ?????????.
  3. ???? ???????? ??????? ?????????? - ??????? ????????? ???? "??????" ?????????? ??????????.
  4. ?????????? ??????????? - ??????? ????????? ????? ???? ??????????? ?????? ?????????.
  5. ???????????? ???????? - ??????? ??????, ??? ??????? ???????? ?????????? ??????.
  6. ????????? ??????????? - ??????? ?????? ???? ?? ????? ?????????? ????????? ?????????? ????? ???????????.

?? ???????????? ?????? ?? ?????????? ???????????? ??????? ???? ???????, ???????? ?? ???????? ????.

?????????, ???????? ???? ????????? ??? ?? ???????? ??????????. ????????? ????? ????, ???????????????? ?????????? ????????? ?? ???? ?????????, ??? ????????? ??????? ???? ????? ?? ?????????? ??????????.


要查看或添加评论,请登录