Created
October 7, 2025 05:07
-
-
Save sunmeat/86b11a1d08256e81cfecfa1ecb894117 to your computer and use it in GitHub Desktop.
early binding + reference array C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Transport | |
{ | |
public void Drive() | |
{ | |
Console.WriteLine("Transport::Drive()"); | |
} | |
} | |
class Car : Transport | |
{ | |
public new void Drive() | |
{ | |
Console.WriteLine("Car::Drive()"); | |
} | |
} | |
class Bike : Transport | |
{ | |
public new void Drive() | |
{ | |
Console.WriteLine("Bike::Drive()"); | |
} | |
} | |
class Tram : Transport | |
{ | |
public new void Drive() | |
{ | |
Console.WriteLine("Tram::Drive()"); | |
} | |
} | |
class Program | |
{ | |
static Transport GetSomeTransport() | |
{ | |
var rand = new Random(); | |
int r = rand.Next(3); | |
if (r == 0) return new Car(); | |
if (r == 1) return new Tram(); | |
return new Bike(); | |
} | |
static void Main() | |
{ | |
int count = 5; | |
// count = int.Parse(Console.ReadLine()); | |
// Random rand = new Random(); | |
// count = rand.Next(20); | |
Transport[] traffic = new Transport[count]; | |
traffic[0] = new Car(); | |
traffic[1] = new Car(); | |
traffic[2] = new Bike(); | |
traffic[3] = new Tram(); | |
traffic[4] = GetSomeTransport(); | |
///////////////////////// чекаємо смарагдовий колір на світлофорі... | |
Console.BackgroundColor = ConsoleColor.Black; | |
Console.ForegroundColor = ConsoleColor.Red; | |
Console.Clear(); | |
Console.Write("Wait 3 "); | |
Thread.Sleep(1000); | |
Console.Write("2 "); | |
Thread.Sleep(1000); | |
Console.Write("1 "); | |
Thread.Sleep(1000); | |
Console.Clear(); | |
Console.BackgroundColor = ConsoleColor.Black; | |
Console.ForegroundColor = ConsoleColor.Green; | |
Console.WriteLine("Go!!!"); | |
Console.WriteLine(); | |
Thread.Sleep(1000); | |
Console.BackgroundColor = ConsoleColor.Black; | |
Console.ForegroundColor = ConsoleColor.Gray; | |
Console.Clear(); | |
for (int i = 0; i < count; i++) | |
traffic[i].Drive(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment