Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.55 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Linq;
  4.  
  5. // Задача: используя ключевые слова языка - только модификаторы и модификаторы доступа, изменить код таким образом, чтобы компиляции и запуск происходили без ошибок и предупреждений (в консоли).
  6. //   * Для классов BaseClass, DerivedClassA, DerivedClassB и DerivedClassImpl установить модификаторы доступа:
  7. //      * Класс BaseClass должен быть виден классам из других сборок.
  8. //      * Класс DerivedClassA не должен быть виден классам из других сборок.
  9. //      * Класс DerivedClassA должен быть виден классам только из текущей сборки.
  10. //   * Для методов классов установить модификаторы и модификаторы доступа, чтобы код компилировался без ошибок.
  11. //   * Справочник - https://docs.microsoft.com/ru-ru/dotnet/csharp/language-reference/keywords/access-modifiers
  12.  
  13. // ----- ДОБАВИТЬ МОДИФИКАТОРЫ И МОДИФИКАТОРЫ ДОСТУПА -----
  14.  
  15. public abstract class BaseClass
  16. {
  17.     internal string GetCode() { return "CODE-1"; }
  18.     public virtual string GetDescription() { return this.GetDefaultDescription() + this.GetClassSymbol(); }
  19.     protected virtual string GetDefaultDescription() { return "CLASS-"; }
  20.     protected abstract string GetClassSymbol();
  21. }
  22.  
  23. internal class DerivedClassA : BaseClass
  24. {
  25.     internal string GetCode() { return this.GetCurrentCode(); }
  26.     string GetCurrentCode() { return "CODE-2"; }
  27.     protected override string GetClassSymbol() { return "A"; } // hz
  28. }
  29.  
  30. internal class DerivedClassB : DerivedClassA
  31. {
  32.     private class DerivedClassImpl
  33.     {
  34.         internal static string GetCode() { return "CODE-3"; }
  35.     }
  36.     public string  GetCode() { return DerivedClassImpl.GetCode(); }
  37.     protected override string GetDefaultDescription() { return string.Empty; } //hz
  38.     protected override string GetClassSymbol() { return "B"; } //hz
  39. }
  40.  
  41. // ----- ЗАПРЕЩЕНО ИЗМЕНЯТЬ КОД В КЛАССЕ PROGRAM -----
  42.  
  43. public class Program
  44. {
  45.     public static void Main()
  46.     {
  47.         Debug.Listeners.Clear();
  48.         Debug.Listeners.Add(new TextWriterTraceListener(Console.Out));
  49.  
  50.         VerifyModifiers();
  51.         VerifyAccessibilityLevels(typeof(BaseClass), s2, typeof(DerivedClassA), s3, typeof(DerivedClassB));
  52.  
  53.         Console.Read();
  54.     }
  55.  
  56.     private static void VerifyModifiers()
  57.     {
  58.         DerivedClassA class_a = new DerivedClassA();
  59.         DerivedClassB class_b = new DerivedClassB();
  60.         BaseClass base_a = class_a;
  61.         BaseClass base_b = class_b;
  62.  
  63.         Debug.Assert(class_a.GetCode() == "CODE-2", "class_a.GetCode() should return CODE-2");
  64.         Debug.Assert(class_b.GetCode() == "CODE-3", "class_b.GetCode() should return CODE-3");
  65.         Debug.Assert(base_a.GetCode() == "CODE-1", "base_a.GetCode() should return CODE-1");
  66.         Debug.Assert(base_b.GetCode() == "CODE-1", "base_b.GetCode() should return CODE-1");
  67.  
  68.         Debug.Assert(class_a.GetDescription() == "CLASS-A", "class_a.GetDescription() sould return CLASS-A");
  69.         Debug.Assert(class_b.GetDescription() == "B", "class_b.GetDescription() should return B");
  70.         Debug.Assert(base_a.GetDescription() == "CLASS-A", "base_1.GetDescription() should return CLASS-A");
  71.         Debug.Assert(base_b.GetDescription() == "B", "base_b.GetDescription() should return B");
  72.     }
  73.  
  74.     private static void VerifyAccessibilityLevels(Type x935, string x807, Type x742, string x458, Type x671)
  75.     {
  76.         TestType(x935, t => t.IsPublic, s1);
  77.         TestType(x742, t => t.IsPublic == false, s1);
  78.         TestType(x671, t => !t.IsPublic, s1);
  79.         TestType(x935, t => t.GetMethods().FirstOrDefault(m => m.Name == x807) == null, string.Format(s4, x807));
  80.         TestType(x935, t => t.GetMethods().FirstOrDefault(m => m.Name == x458) == null, string.Format(s4, x458));
  81.     }
  82.     private static void TestType(Type t, Func<Type, bool> f, string m) {
  83.         Debug.Assert(f(t), string.Format("{0}{1}", t.Name, m));
  84.     }
  85.     const string s1 = " has wrong accessibility modifier.";
  86.     const string s2 = "GetDefaultDescription";
  87.     const string s3 = "GetClassSymbol";
  88.     const string s4 = "::{0} has wrong accessibility level";
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement