Класс Human: изменить в коде пол человека с int на bool - C#
Формулировка задачи:
Привет! Вот такой код, класс Human, пол человека сделан через int, нужно переделать что было через bool и был не Female и Male, а 1 и 0.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Laba3
{
enum Pol_hum { Male, Female };
public class Human
{
protected int vozrast;
protected string nation;
protected int POL;
public Human()
{
this.vozrast = 0;
this.nation = "undefined";
this.POL = (int)Pol_hum.Male;
}
public Human(int v, string n, int p)
{
this.vozrast = v;
this.nation = n;
this.POL = (p == 1) ? (int)Pol_hum.Male : (int)Pol_hum.Female;
}
public virtual int ivozrast
{
get { return this.vozrast; }
set { this.vozrast = value; } // value - ключевое слово
}
public virtual string ination
{
get { return this.nation; }
set { this.nation = value; }
}
public virtual int IPOL
{
get { return this.POL; }
set { this.POL = (value == 1) ? (int)Pol_hum.Male : (int)Pol_hum.Female; }
}
}
public class Student : Human
{
public Student(int v, string n, int p)
{
this.vozrast = v;
this.nation = n;
this.POL = (p == 1) ? (int)Pol_hum.Male : (int)Pol_hum.Female;
}
public override int ivozrast
{
get { return this.vozrast; }
set { this.vozrast = value; }
}
public override string ination
{
get { return this.nation; }
set { this.nation = value; }
}
public override int IPOL
{
get { return this.POL; }
set { this.POL = value; }
}
public override string ToString()
{
return "Возраст - " + this.ivozrast + ", Национальность - " + this.ination + ", Пол - " + ((this.IPOL == (int)Pol_hum.Male) ? "муж" : "жен");
}
}
class Program
{
static void Main(string[] args)
{
Student q1 = new Student(12, "немец", 1);
Console.WriteLine(q1.ToString());
Student h1 = new Student(40, "русский", 2);
Console.WriteLine(h1.ToString());
Student f1 = new Student(15, "американец", 1);
Console.WriteLine(f1.ToString());
Console.ReadLine();
}
}
}Решение задачи: «Класс Human: изменить в коде пол человека с int на bool»
textual
Листинг программы
class Program
{
static void Main(string[] args)
{
try
{
Student q1 = new Student(17, "немец", 1);
Console.WriteLine(q1.ToString());
Student h1 = new Student(19, "русская", 0); // и вообще стоит подумать над окончаниями в национальности в зависимости от пола
Console.WriteLine(h1.ToString());
Student f1 = new Student(40, "американец", 1);
Console.WriteLine(f1.ToString());
}
catch(Exception ex)
{
Console.WriteLine(ex);
}
Console.ReadLine();
}
}
public class Human
{
private byte _years;
public bool IsMale { get; set; }
public Human(): this(0, "undefined", true)
{
}
public Human(byte years, string n, bool isMale)
{
Nationality = n;
Years = years;
IsMale = isMale;
}
//virtual неверное архитектурное решение, т.к. возраст он для всех - возраст, а вот проверка возраста это уже другое
public byte Years //Ege ? ну наверное все таки Years, byte - т.к. возраст не может быть меньше нуля и больше 255
{
get { return _years; }
set
{
CheckYears(value);
_years = value;
}
}
//А вот тут нужен virtual, т.к. студент не может быть 14 летним подростком
protected virtual void CheckYears(byte years)
{
if(years > 120)
throw new Exception("уже мертв, определите тип Сorpse");
}
//virtual не нужен
public string Nationality { get; set; } //хотя в паспорте это не пишут
}
public class Student : Human
{
public Student(byte y, string n, byte isMale) : base(y, n, isMale != 0) //читернул
{
}
protected override void CheckYears(byte years)
{
base.CheckYears(years);
if (years < 16)
throw new Exception(string.Format("Слишком молод для студента: {0}", Nationality));
}
public override string ToString()
{
return "Возраст - " + Years + ", Национальность - " + Nationality + ", Пол - " + (IsMale ? "муж" : "жен");
}
}