Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- public class Person
- {
- protected int age;
- protected string name;
- public Person(string name, int age)
- {
- this.Name = name;
- this.Age = age;
- }
- public virtual string Name
- {
- get { return this.name; }
- set
- {
- if (!ValidateName(value))
- {
- throw new ArgumentException("Name’s length should not be less than 3 symbols!");
- }
- name = value;
- }
- }
- public virtual int Age
- {
- get { return this.age; }
- set
- {
- if (!ValidateAge(value))
- {
- throw new ArgumentException("Age must be positive!");
- }
- age = value;
- }
- }
- protected virtual bool ValidateName(string value)
- {
- if (value.Length < 3 || string.IsNullOrWhiteSpace(value))
- {
- return false;
- }
- return true;
- }
- protected virtual bool ValidateAge(int age)
- {
- return age >= 0;
- }
- public override string ToString()
- {
- return $"Name: {this.name}, Age: {this.age}";
- }
- }
- using System;
- public class Child : Person
- {
- public Child(string name, int age)
- : base(name, age)
- {
- }
- public override int Age
- {
- get
- {
- return base.Age;
- }
- set
- {
- if (value > 15)
- {
- throw new ArgumentException("Child's age must be less than 15!");
- }
- base.Age = value;
- }
- }
- // protected override bool ValidateAge(int age)
- // {
- // return age < 15;
- // }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement