Answer the question
In order to leave comments, you need to log in
The ban on overriding methods does not work. Why?
There is a code where I override methods:
class Program
{
static void Main(string[] args)
{
Person p1 = new Person("Bill");
p1.Display(); // вызов метода Display из класса Person
Employee p2 = new Employee("Tom", "Microsoft");
p2.Display(); // вызов метода Display из класса Employee
Person p3 = new Employee("Bill", "Microsoft");
p3.Display();
Console.ReadKey();
}
}
class Person
{
public string Name { get; set; }
public Person(string name)
{
Name = name;
}
public virtual void Display()
{
Console.WriteLine(Name);
}
}
class Employee : Person
{
public string Company { get; set; }
public Employee(string name, string company)
: base(name)
{
Company = company;
}
public override void Display()
{
Console.WriteLine($"{Name} работает в {Company}");
}
}
Bill
Tom works at Microsoft
Bill works at Microsoft
class Program
{
static void Main(string[] args)
{
Person p1 = new Person("Bill");
p1.Display(); // вызов метода Display из класса Person
Employee p2 = new Employee("Tom", "Microsoft");
p2.Display(); // вызов метода Display из класса Employee
Person p3 = new Employee("Bill", "Microsoft");
p3.Display();
Console.ReadKey();
}
}
class Person
{
public string Name { get; set; }
public Person(string name)
{
Name = name;
}
public virtual void Display()
{
Console.WriteLine(Name);
}
}
class Employee : Person
{
public string Company { get; set; }
public Employee(string name, string company)
: base(name)
{
Company = company;
}
public override <b><i>sealed </i></b> void Display()
{
Console.WriteLine($"{Name} работает в {Company}");
}
}
Bill
Tom works at Microsoft
Bill works at Microsoft
Bill
Tom works at Microsoft
Bill
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question