Advertisement
social1986

Untitled

Feb 24th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.72 KB | None | 0 0
  1. using System;
  2.  
  3. public class Person
  4. {
  5.     protected int age;
  6.     protected string name;
  7.  
  8.     public Person(string name, int age)
  9.     {
  10.         this.Name = name;
  11.         this.Age = age;
  12.     }
  13.  
  14.     public virtual string Name
  15.     {
  16.         get { return this.name; }
  17.         set
  18.         {
  19.             if (!ValidateName(value))
  20.             {
  21.                 throw new ArgumentException("Name’s length should not be less than 3 symbols!");
  22.             }
  23.             name = value;
  24.         }
  25.     }
  26.    
  27.     public virtual int Age
  28.     {
  29.         get { return this.age; }
  30.         set
  31.         {
  32.             if (!ValidateAge(value))
  33.             {
  34.                 throw new ArgumentException("Age must be positive!");
  35.             }
  36.             age = value;
  37.         }
  38.     }
  39.  
  40.     protected virtual bool ValidateName(string value)
  41.     {
  42.         if (value.Length < 3 || string.IsNullOrWhiteSpace(value))
  43.         {
  44.             return false;
  45.         }
  46.         return true;
  47.     }
  48.  
  49.     protected virtual bool ValidateAge(int age)
  50.     {
  51.         return age >= 0;
  52.     }
  53.  
  54.     public override string ToString()
  55.     {
  56.         return $"Name: {this.name}, Age: {this.age}";
  57.     }
  58. }
  59.  
  60.  
  61. using System;
  62.  
  63. public class Child : Person
  64. {
  65.     public Child(string name, int age)
  66.         : base(name, age)
  67.     {
  68.     }
  69.  
  70.     public override int Age
  71.     {
  72.         get
  73.         {
  74.             return base.Age;
  75.         }
  76.         set
  77.         {
  78.             if (value > 15)
  79.             {
  80.                 throw new ArgumentException("Child's age must be less than 15!");
  81.             }
  82.             base.Age = value;
  83.         }
  84.     }
  85.  
  86.   // protected override bool ValidateAge(int age)
  87.   // {
  88.   //     return age < 15;
  89.   // }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement