Загрузка данных
using System;
using System.Drawing;
using System.Windows.Forms;
namespace RocketApp
{
// ====================== ИНТЕРФЕЙС ======================
public interface IRocket
{
void DisplayInfo();
}
// ====================== БАЗОВЫЙ КЛАСС ======================
public abstract class Rocket : IRocket
{
public string Name { get; protected set; }
public string Manufacturer { get; protected set; }
public double PayloadToLEO { get; protected set; }
public string Sound { get; protected set; }
public string FlightDescription { get; protected set; }
public virtual string GetRocketArt() => "";
public virtual void DisplayInfo()
{
AppendText(GetRocketArt());
AppendText($"Ракета: {Name}\n");
AppendText($"Производитель: {Manufacturer}\n");
AppendText($"Полезная нагрузка: {PayloadToLEO} тонн\n");
AppendText($"Ракета издаёт звук: {Sound}\n");
AppendText($"Ракета летит: {FlightDescription}\n\n");
}
protected void AppendText(string text)
{
if (Application.OpenForms.Count > 0 && Application.OpenForms[0] is Form1 form)
{
if (form.Controls["txtOutput"] is TextBox txt)
{
txt.AppendText(text);
txt.ScrollToCaret();
}
}
}
}
// ====================== FALCON 9 ======================
public class Falcon9 : Rocket
{
public Falcon9()
{
Name = "Falcon 9";
Manufacturer = "SpaceX";
PayloadToLEO = 22.8;
Sound = "РРРРРРРРР!!! (рев двигателей Merlin)";
FlightDescription = "мощный вертикальный старт с возвращаемой первой ступенью";
}
public override string GetRocketArt()
{
return
" ▲\n" +
" / \\\n" +
" /F9\\\n" +
" /___\\\n" +
" | □ |\n" +
" |___|\n" +
" |||\n" +
" |||\n\n";
}
}
// ====================== STARSHIP ======================
public class Starship : Rocket
{
public Starship()
{
Name = "Starship";
Manufacturer = "SpaceX";
PayloadToLEO = 150;
Sound = "ГРОМОВОЙ РЁВ Raptor-ов!!!";
FlightDescription = "полностью многоразовая сверхтяжёлая ракета к Марсу";
}
public override string GetRocketArt()
{
return
" ▲\n" +
" / \\\n" +
" / \\\n" +
" /STAR\\\n" +
" / SHIP \\\n" +
" /_______\\\n" +
" | □ |\n" +
" |_____|\n" +
" |||||\n" +
" |||||\n\n";
}
}
// ====================== ПРОТОН-М ======================
public class ProtonM : Rocket
{
public ProtonM()
{
Name = "Протон-М";
Manufacturer = "Роскосмос";
PayloadToLEO = 23;
Sound = "МОЩНЫЙ ГУЛ РД-276!!!";
FlightDescription = "классическая тяжёлая ракета на гиперголе";
}
public override string GetRocketArt()
{
return
" ▲\n" +
" / \\\n" +
" /ПРО\\\n" +
" / ТОН \\\n" +
" / М \\\n" +
" /_______\\\n" +
" | □ |\n" +
" |_____|\n" +
" |||||\n" +
" |||||\n\n";
}
}
// ====================== ГЛАВНАЯ ФОРМА ======================
public partial class Form1 : Form
{
private Falcon9 falcon;
private Starship starship;
private ProtonM proton;
public Form1()
{
this.Text = "Лабораторная работа - Полиморфизм";
this.Size = new Size(920, 720);
this.StartPosition = FormStartPosition.CenterScreen;
this.BackColor = Color.WhiteSmoke;
CreateControls();
falcon = new Falcon9();
starship = new Starship();
proton = new ProtonM();
}
private void CreateControls()
{
// Кнопки
Button btnFalcon = new Button
{
Text = "Falcon 9",
Location = new Point(30, 40),
Size = new Size(220, 50),
Font = new Font("Segoe UI", 11, FontStyle.Bold)
};
Button btnStarship = new Button
{
Text = "Starship",
Location = new Point(30, 110),
Size = new Size(220, 50),
Font = new Font("Segoe UI", 11, FontStyle.Bold)
};
Button btnProton = new Button
{
Text = "Протон-М",
Location = new Point(30, 180),
Size = new Size(220, 50),
Font = new Font("Segoe UI", 11, FontStyle.Bold)
};
Button btnPolymorphism = new Button
{
Text = "Демонстрация полиморфизма",
Location = new Point(30, 270),
Size = new Size(220, 55),
Font = new Font("Segoe UI", 11, FontStyle.Bold),
BackColor = Color.LightSkyBlue
};
// Поле вывода
TextBox txtOutput = new TextBox
{
Name = "txtOutput",
Multiline = true,
ScrollBars = ScrollBars.Vertical,
Location = new Point(280, 30),
Size = new Size(600, 630),
Font = new Font("Consolas", 10.5f), // Важно: моноширинный шрифт
BackColor = Color.Black,
ForeColor = Color.LimeGreen,
ReadOnly = true
};
// События
btnFalcon.Click += (s, e) => { ClearAndShowHeader("FALCON 9"); falcon.DisplayInfo(); };
btnStarship.Click += (s, e) => { ClearAndShowHeader("STARSHIP"); starship.DisplayInfo(); };
btnProton.Click += (s, e) => { ClearAndShowHeader("ПРОТОН-М"); proton.DisplayInfo(); };
btnPolymorphism.Click += (s, e) => DemonstratePolymorphism();
this.Controls.Add(btnFalcon);
this.Controls.Add(btnStarship);
this.Controls.Add(btnProton);
this.Controls.Add(btnPolymorphism);
this.Controls.Add(txtOutput);
}
private void ClearAndShowHeader(string rocketName)
{
if (this.Controls["txtOutput"] is TextBox txt)
{
txt.Clear();
txt.AppendText($"=== ЗАПУСК РАКЕТЫ {rocketName} ===\n\n");
}
}
private void DemonstratePolymorphism()
{
if (this.Controls["txtOutput"] is TextBox txt)
{
txt.Clear();
txt.AppendText("=== ДЕМОНСТРАЦИЯ ПОЛИМОРФИЗМА ===\n");
txt.AppendText(new string('=', 60) + "\n\n");
Rocket[] rockets = { falcon, starship, proton };
foreach (var rocket in rockets)
{
rocket.DisplayInfo();
txt.AppendText(new string('-', 60) + "\n\n");
}
}
}
}
// ====================== ЗАПУСК ======================
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}