Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text;
- namespace ExercisesInheritance
- {
- class Program
- {
- static void Main()
- {
- string name = Console.ReadLine();
- int age = int.Parse(Console.ReadLine());
- try
- {
- Child child = new Child(name, age);
- Console.WriteLine(child);
- }
- catch (ArgumentException ae)
- {
- Console.WriteLine(ae.Message);
- }
- }
- }
- public class Child : Person
- {
- public Child(string name, int age) : base(name, age)
- {
- }
- public override int Age
- {
- get { return base.Age; }
- protected set
- {
- if (value >= 15)
- {
- throw new ArgumentException("Child’s age must be less than 15!");
- }
- base.Age = value;
- }
- }
- }
- public class Person
- {
- private string name;
- private int age;
- public Person(string name, int age)
- {
- this.Age = age;
- this.Name = name;
- }
- public virtual string Name
- {
- get { return this.name; }
- protected set
- {
- if (value.Length < 3 || string.IsNullOrWhiteSpace(value))
- {
- throw new ArgumentException("Name’s length should not be less than 3 symbols!");
- }
- this.name = value;
- }
- }
- public virtual int Age
- {
- get { return this.age; }
- protected set
- {
- if (value < 0)
- {
- throw new ArgumentException("Age must be positive!");
- }
- this.age = value;
- }
- }
- public override string ToString()
- {
- StringBuilder sb = new StringBuilder();
- sb.Append($"Name: {this.Name}, Age: {this.Age}");
- return sb.ToString();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement