namespace DZ_4 { class Program { static void Main(string[] args) { TeamLeader pr1 = new TeamLeader(); pr1.Condition();//проверяем состояние стройки: построен или нет фундамент Basement bsm = new Basement(); bsm.Installation(); pr1.Condition();//проверяем состояние стройки: построен или нет фундамент (ОПЯТЬ ВОЗВРАЩАЕТ, ЧТО ФУНДАМЕНТ НЕ ПОСТРОЕН) } class TeamLeader { public void Condition()//метод для проверки состояния строительства { House b = new House(); b.Condition();//метод для проверки постройки фундамента } } class House : TeamLeader { private int Bsement { get; set; } public new void Condition() { if (Bsement == 1) { Console.WriteLine("Фундамент построен"); } else { Console.WriteLine("Фундамент не построен"); } } public void Result(int b) { Bsement = b; } } class Basement : House { public void Installation()//отрисовка фундамента { Console.WriteLine("Солнце всходит - пора за работу!"); System.Threading.Thread.Sleep(4000); Console.BackgroundColor = ConsoleColor.DarkBlue; Console.Clear(); for (int i = 0; i <= 50; i++) { Console.Write("_"); System.Threading.Thread.Sleep(40); } Console.WriteLine(); for (int i = 0; i <= 5; i++) { Console.Write("I"); for (int j = 0; j <= 48; j++) { Console.Write(" "); } Console.WriteLine("I"); System.Threading.Thread.Sleep(40); } System.Threading.Thread.Sleep(100); Console.BackgroundColor = ConsoleColor.Blue; for (int i = 0; i <= 50; i++) { Console.Write("-"); System.Threading.Thread.Sleep(40); } Console.WriteLine(); Console.WriteLine("Стоп!"); Result(1);//передаем флажок об окончании строительства } } } } 

When you call the Condition () method of the TeamLeader pr1 instance again, after drawing the foundation and setting the completion flag, the method returns 0, that is, the "foundation is not built." What is the catch and how to fix it?

  • Every time you Bsement to Condition you create a new instance of the house with a zero Bsement value. - Alexey Shimansky
  • understood - how to make it so that the current value can be obtained? - Proshka
  • @Proshka: Depending on the meaning, of course. You do not have to plug the mistake, but how to do it right? For example, I have a suspicion that constructing an instance of the derived House inside the base TeamLeader object is most likely wrong. Describe in more detail your task. - VladD
  • 1. There is a foreman - he should monitor the condition of the house construction 2. The house consists of parts: foundation, walls, roof, windows 3. When the foreman requests, you need to receive information that has already been built, that is, construction status - Proshka
  • one
    @ Alexey Shimansky: The “Foreman” pattern! - VladD

2 answers 2

I would do this:

  1. The foreman and the house are separate entities. The foreman can be instructed to take care of the house. The foundation is not home either.
  2. The house includes a foundation.
  3. It is not up to the main program to create a foundation. And it is not a matter of foundation to establish oneself in a house.

This results in the following code:

 static void Main(string[] args) { House house = new House(); TeamLeader pr1 = new TeamLeader(); pr1.WorkOnHouse(house); bool hasBasement = house.Basement != null; if (hasBasement) Console.WriteLine("Фундамент есть"); else Console.WriteLine("Фундамента нет"); pr1.ConstructBasement(); hasBasement = house.Basement != null; if (hasBasement) Console.WriteLine("Фундамент есть"); else Console.WriteLine("Фундамента нет"); } 

Now a foreman.

 class TeamLeader { House house; // текущая стройка // посылаем на объект public WorkOnHouse(House house) { this.house = house; } public ConstructBasement() { if (house == null) throw new InvalidOperationException("Сначала направьте на стройку"); if (house.Basement != null) throw new InvalidOperationException("Да есть уже фундамент, чё совсем ку-ку?"); Basement basement = new Basement(); basement.Construct(); // строим house.Basement = basement; } } 

Now the house. It is as simple as ... the corner of the house!

 class House { public Basement Basement { get; set; } // тут будут другие части дома } 

Well, the foundation.

 class Basement { bool IsConstructed { get; private set; } = false; public void Construct() { Console.WriteLine("Солнце всходит - пора за работу!"); // и так далее Console.WriteLine("Стоп!"); IsConstructed = true; // Result(1); // это не нужно, не дело фундамента ставить флаги } } 
  • Who put a minus, is it possible to justify? - VladD

Here you have nothing to lose just the OBJECTS bsm and pr1 are not connected in any way, and as a result of the change in one does not appear on the other. In addition, you have a rather strange inheritance architecture - the foundation inherits the building, and the foreman includes the building (probably as a third leg). Here perhaps for the foundation should be used composition, and the superintendent to rework the interface.

  • 2
    superintendent to recycle ....... into sausage for such) - Alexey Shimansky
  • please do not judge strictly - I'm just learning - Proshka
  • @Mirdin Thanks for the reply - laughed heartily with my own code - Proshka