-
[C#]3. 입양자관리 프로그램 (enum, 상속, 리스트<T> 사용)05. Programing Language/[C#] 1.C#강좌 2020. 2. 21. 20:20
좋은 하루 참조 : Alongside Y 님의 강좌
비쥬얼 스튜디오를 사용했으며 디자인은 따로 설명 하지 않겠습니다.
각 클래스마다 중요한 Point를 위주로 설명하겠습니다
1. 상위 클래스 Pet
namespace AnimalShelter { public class Pet { public int PetId; public string Name; public string Color; public string Gender; public string Description; public Pet(int petId, string name, string color, string gender) { PetId = petId; Name = name; Color = color; Gender = gender; } public virtual string MakeSound() { return ""; } } }
- 부모클래스로서 사용됩니다.
- virtual 라는 부분은 가상 메서드로 정의해주는 부분입니다.
즉 public virtual string MakeSound() 메서드의 의미는
자식 클래스에 정의가 되어있다면 그것을 사용하겠다는 의미입니다.
2. Dog 클래스
namespace AnimalShelter { public enum DogBreed { Mixed, Bulldog, Jindo, Yorkshire} public class Dog : Pet { public DogBreed Breed; public Dog(int petId, string name, string color, string gender, DogBreed breed) : base(petId, name, color, gender) { Breed = breed; } public override string MakeSound() { return "멍멍"; } public string Bite() { return "물어뜯기"; } } }
- enum 은 새로운 타입을 선언 해주는 것입니다. 중괄호 안에 사용자가 원하는 것을 직접 정의해줍니다.
- public class Dog : Pet
Dog클래스가 Pet클래스를 상속 받음
상속받게되면 Pet클래스의 변수와 메서드를 마음대로 사용할 수 있음.
- : base 는 부모 클래스 즉 pet의 생성자에 대입을 해주기 위해 적은 것.
-public override string MakeSound()
부모클래스의 메서드를 상속받아 사용하겠다는 것을 의미
3.Cat 클래스
namespace AnimalShelter { public class Cat : Pet { public Cat(int petId ,string name, string color, string gender) : base(petId, name, color, gender) { } public override string MakeSound() { return "야옹"; } public string Scratch() { return "할퀴기"; } } }
4. Customer 클래스
namespace AnimalShelter { public class Customer { public string FirstName; public string LastName; private DateTime _Birthday; private bool _IsQualified; public string Address; public string Description; private List<Pet> _MyPets = new List<Pet>(); public List<Pet> MyPets { get { return _MyPets; } } public bool Adopt(Pet pet) { if (_IsQualified == true) { _MyPets.Add(pet); return true; } else { return false; } } public Customer(string firstName, string lastName, DateTime birthday) { this.FirstName = firstName; this.LastName = lastName; this._Birthday = birthday; this._IsQualified = Age >= 18; } public DateTime Birthday { get { return _Birthday;} set { _Birthday = value; _IsQualified = Age >= 18; } } public int Age { get { return DateTime.Now.Year - _Birthday.Year; } } public bool IsQualified { get { return _IsQualified; } } public string FullName { get { return FirstName + " " + LastName; } } }
- private List<Pet> _MyPets = New List<Pet>();
배열의 단점 - 개수를 미리 지정
ArrayList의 단점 - 잦은 형 변환으로 인한 속도 저하
List<T> - 위 단점들을 보완하고자 나옴, T에 원하는 타입을 지정하여 사용.
5. Form1의 클래스
namespace AnimalShelter { public partial class Form1 : Form { public List<Customer> Customers = new List<Customer>(); public Form1() { InitializeComponent(); } private void CreateCustomer_Click(object sender, EventArgs e) { Customer cus = new Customer(CusNewFirstName.Text, CusNewLastName.Text, DateTime.Parse(CusNewBirthday.Text)); cus.Address = CusNewAddress.Text; cus.Description = CusNewDescription.Text; CusList.Rows.Add(cus.FirstName, cus.Age, cus.IsQualified); Customers.Add(cus); CusNewFirstName.Text = ""; CusNewLastName.Text = ""; CusNewBirthday.Text = ""; CusNewDescription.Text = ""; CusNewAddress.Text = ""; } public void ShowDetails(Customer cus) { CusFullName.Text = cus.FullName; CusAge.Text = cus.Age.ToString(); CusAddress.Text = cus.Address; CusDescription.Text = cus.Description; CusIsQualified.Text = cus.IsQualified.ToString(); CusPetInfo.Text = ""; foreach (Pet pet in cus.MyPets) { CusPetInfo.Text += pet.Name + "/" + pet.MakeSound(); if (pet is Cat) { CusPetInfo.Text += "/" + (pet as Cat).Scratch(); } else if(pet is Dog) { CusPetInfo.Text += "/" + (pet as Dog).Bite(); } CusPetInfo.Text += Environment.NewLine; } } private void CusList_CellClick(object sender, DataGridViewCellEventArgs e) { string firstName = (string)CusList.Rows[e.RowIndex].Cells[0].Value; foreach (Customer cus in Customers) { if(cus.FirstName == firstName) { ShowDetails(cus); break; } } CusDetailPanel.Show(); CusNewPanel.Hide(); } private void Form1_Load(object sender, EventArgs e) { CusListPanel.Dock = DockStyle.Fill; CusDetailPanel.Dock = DockStyle.Right; CusNewPanel.Dock = DockStyle.Right; Customer cus = new Customer("Ian", "Na", new DateTime(2000, 1, 2)); Cat cat = new Cat(1, "Locas", "White", "Male"); cus.Adopt(cat); Cat cat2 = new Cat(2, "Ruby", "Brown", "FeMale"); cus.Adopt(cat); Dog dog = new Dog(3, "Happy", "Black", "Male",DogBreed.Jindo); cus.Adopt(dog); Customers.Add(cus); CusList.Rows.Add(cus.FirstName, cus.Age, cus.IsQualified); } private void toolStripMenuItem1_Click(object sender, EventArgs e) { CusNewPanel.Show(); CusDetailPanel.Hide(); } } }
- if (pet is Cat)
is : pet이 Cat과 같은 Class인지 묻는 것이고 true와 false로 답한다.
- (pet as Cat).Scrath()
as : pet을 Cat클래스로 변환
- DateTime.Parse()
DateTime형으로 변환
'05. Programing Language > [C#] 1.C#강좌' 카테고리의 다른 글
[C#] 2. 속성 (0) 2020.02.20 [C#] 1. 계산기 만들기 (0) 2020.02.19