Экземпляры класса, вызов переопределенного метода - C#

Узнай цену своей работы

Формулировка задачи:

как лучше вызвать переопределенный метод?
Листинг программы
  1. Console.WriteLine(m1.ToString());
или через статический метод(так как сделал ниже в программе)?
Листинг программы
  1. class MyClass
  2. {
  3. public string Name { get; set; }
  4. public int Age { get; set; }
  5. public MyClass(string name, int age)
  6. {
  7. Name = name;
  8. Age = age;
  9. }
  10. public override string ToString()
  11. {
  12. return string.Format(" Имя: {0}\tВозраст: {1}", Name, Age);
  13. }
  14. }
  15. class Program
  16. {
  17. static void Main()
  18. {
  19. MyClass m1 = new MyClass("Vlad", 10);
  20. Console.WriteLine(m1.ToString());
  21. MyClass m2 = m1;
  22. m2.Name = "Tom";
  23. m2.Age = 15;
  24. Display(m1);
  25. Display(m2);
  26. Console.ReadKey();
  27. }
  28. static void Display(MyClass m1 )
  29. {
  30. Console.WriteLine(m1);
  31. }
  32. }

Решение задачи: «Экземпляры класса, вызов переопределенного метода»

textual
Листинг программы
  1. .method private hidebysig static void  Main() cil managed
  2.   {
  3.     // Code size       70 (0x46)
  4.     .maxstack  3
  5.     .locals init ([0] class ClassLibrary1.MyClass m1,
  6.              [1] class ClassLibrary1.MyClass m2)
  7.     .line 27,27 : 9,10 ''
  8.     IL_0000:  nop
  9.     .line 28,28 : 13,50 ''
  10.     IL_0001:  ldstr      "Vlad"
  11.     IL_0006:  ldc.i4.s   10
  12.     IL_0008:  newobj     instance void ClassLibrary1.MyClass::.ctor(string,
  13.                                                                     int32)
  14.     IL_000d:  stloc.0
  15.     .line 29,29 : 13,46 ''
  16.     IL_000e:  ldloc.0
  17.     IL_000f:  callvirt   instance string [mscorlib]System.Object::ToString()
  18.     IL_0014:  call       void [mscorlib]System.Console::WriteLine(string)
  19.     IL_0019:  nop
  20.     .line 31,31 : 13,29 ''
  21.     IL_001a:  ldloc.0
  22.     IL_001b:  stloc.1
  23.     .line 32,32 : 13,29 ''
  24.     IL_001c:  ldloc.1
  25.     IL_001d:  ldstr      "Tom"
  26.     IL_0022:  callvirt   instance void ClassLibrary1.MyClass::set_Name(string)
  27.     IL_0027:  nop
  28.     .line 33,33 : 13,25 ''
  29.     IL_0028:  ldloc.1
  30.     IL_0029:  ldc.i4.s   15
  31.     IL_002b:  callvirt   instance void ClassLibrary1.MyClass::set_Age(int32)
  32.     IL_0030:  nop
  33.     .line 34,34 : 13,25 ''
  34.     IL_0031:  ldloc.0
  35.     IL_0032:  call       void ClassLibrary1.Program::Display(class ClassLibrary1.MyClass)
  36.     IL_0037:  nop
  37.     .line 35,35 : 13,25 ''
  38.     IL_0038:  ldloc.1
  39.     IL_0039:  call       void ClassLibrary1.Program::Display(class ClassLibrary1.MyClass)
  40.     IL_003e:  nop
  41.     .line 37,37 : 13,31 ''
  42.     IL_003f:  call       valuetype [mscorlib]System.ConsoleKeyInfo [mscorlib]System.Console::ReadKey()
  43.     IL_0044:  pop
  44.     .line 38,38 : 9,10 ''
  45.     IL_0045:  ret
  46.   } // end of method Program::Main
  47.  
  48.   .method private hidebysig static void  Display(class ClassLibrary1.MyClass m1) cil managed
  49.   {
  50.     // Code size       9 (0x9)
  51.     .maxstack  8
  52.     .line 40,40 : 9,10 ''
  53.     IL_0000:  nop
  54.     .line 41,41 : 13,35 ''
  55.     IL_0001:  ldarg.0
  56.     IL_0002:  call       void [mscorlib]System.Console::WriteLine(object)
  57.     IL_0007:  nop
  58.     .line 42,42 : 9,10 ''
  59.     IL_0008:  ret
  60.   } // end of method Program::Display

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

5   голосов , оценка 4.2 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы