C#을 공부하며 객체지향프로그래밍에서
을 구현해보겠다.
캡슐화의 주된 목적은 "데이터의 은닉"이다. 민감한 데이터를 private로 감추어서 public의 함수로만 접근/변경 가능하다.
따라서 이러한 private변수에 read-only(읽기전용) 또는 write-only(변경전용)한 선택접 접근을 제어할 수 있게된다.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace C_study
{
internal class Car
{
// 캡슐화
public string handle = "ABC";
private string hidden = "HIDE";
protected string a = "QQQ";
// Read Only
public string getCar1()
{
return hidden;
}
// Write Only
public void setCar1(string str)
{
hidden = str;
}
public void getCar2()
{
Console.WriteLine(handle);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace C_study
{
internal class Program
{
static void Main(string[] args)
{
// 캡슐화
// 생성자 car1 선언
Car car1 = new Car();
// hidden string 변수에 "HIDE Write Only" 저장
car1.setCar1("HIDE Write Only");
// getCar2()함수를 실행해서 handle 출력
car1.getCar2();
// handle값 출력
Console.WriteLine(car1.handle);
// getCar1()함수를 실행해서 반환값 hidden 출력
Console.WriteLine(car1.getCar1());
}
}
}
출력결과
ABC
ABC
HIDE Write Only
부모클래스에 함수나 변수를 선언하고 아이클래스에는 아무것도 없어도 상속만 해주면 다른클래스에서 아이클래스의 생성자를 만든 후 가져다 쓸 수 있다.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace C_study
{
internal class Suv : Car
{
// 상속
public void getA()
{
Console.WriteLine(a);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace C_study
{
internal class Program
{
static void Main(string[] args)
{
// 상속
// 생성자 suv 선언
Suv suv = new Suv();
// handle 변수에 "ZXC" 저장
suv.handle = "ZXC";
// getCar2()함수를 실행해서 handle의 값을 가져와 출력
suv.getCar2();
// handle의 값을 가져와 출력
Console.WriteLine(suv.handle);
// 접근제한자 protected의 변수를 가져와 출력
suv.getA();
}
}
}
출력결과
ZXC
ZXC
QQQ
여러가지 자료형을 가질 수 있는것. 부모클래스에서 virtual을 입력하여 아이클래스에서 함수의 재정의가 가능해진다. 객체만 변경하면 작동될 수 있다.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace C_study
{
internal class CarType
{
public virtual void carType()
{
// virtual : 오버라이딩
Console.WriteLine("모든차");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace C_study
{
internal class Embulance : CarType
{
public void carType()
{
Console.WriteLine("구급차");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace C_study
{
internal class FireCar : CarType
{
public void carType()
{
Console.WriteLine("소방차");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace C_study
{
internal class PoliceCar : CarType
{
public void carType()
{
Console.WriteLine("경찰차");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace C_study
{
internal class Program
{
static void Main(string[] args)
{
// 다형성
CarType carType = new CarType();
Embulance embulance = new Embulance();
PoliceCar policeCar = new PoliceCar();
FireCar fireCar = new FireCar();
carType.carType();
embulance.carType();
policeCar.carType();
fireCar.carType();
}
}
}
출력결과
모든차
구급차
경찰차
소방차