... | ... | @@ -116,5 +116,286 @@ public static void Main(string[] args) |
|
|
|
|
|

|
|
|
|
|
|
###정적 생성자
|
|
|
정적 멤버를 초기화하는 기능을 한다. 클래스에 단 한개만 존재 할 수 있다. 최초로 접근하는 시점에 우선적으로 단 한 번만 실행된다.
|
|
|
|
|
|
```c#
|
|
|
public static Dog KingDog = new Dog("왕개"); //위 아래는 같은 코드가 된다.
|
|
|
|
|
|
public static Dog KingDog = new Dog();
|
|
|
static Dog()
|
|
|
{
|
|
|
KingDog = new Dog("왕개");
|
|
|
}
|
|
|
```
|
|
|
|
|
|
<br />
|
|
|
|
|
|
## 네임스페이스 , using
|
|
|
자바의 패키지 개념과 같다. 이름이 같지만 소속도 다르고 실제 쓰임도 다른 클래스들의 이름 충돌을 막기 위해서 사용한다. 그리고 클래스들의 소속을 구분하는데 사용되는 것이 더 일반적이다.
|
|
|
|
|
|
```c#
|
|
|
namespace NamespaceEx1
|
|
|
{
|
|
|
class Dog
|
|
|
{
|
|
|
public Dog()
|
|
|
{
|
|
|
Console.WriteLine("네임스페이스1");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
namespace NamespaceEx2
|
|
|
{
|
|
|
class Dog
|
|
|
{
|
|
|
public Dog()
|
|
|
{
|
|
|
Console.WriteLine("네임스페이스2");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//dog3를 위해선 여기서 using으로 어떤 네임스페이스를 쓸 건지 선언해줘야 함.
|
|
|
|
|
|
namespace ConsoleApplication1
|
|
|
{
|
|
|
class Program
|
|
|
{
|
|
|
static void Main(string[] args)
|
|
|
{
|
|
|
NamespaceEx1.Dog dog1 = new NamespaceEx1.Dog(); //"네임스페이스1" 출력
|
|
|
NamespaceEx2.Dog dog2 = new NamespaceEx2.Dog(); //"네임스페이스2" 출력
|
|
|
|
|
|
Dog dog3 = new Dog(); //에러 코드 상단에 using으로 어떤 네임스페이스를 선언하냐에 따라서 다른 결과 출력
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
```
|
|
|
|
|
|
<br />
|
|
|
|
|
|
## 캡슐화
|
|
|
|
|
|
접근 제한 유형
|
|
|
|
|
|
|이름|의미|
|
|
|
|-----|-----|
|
|
|
|internal|동일한 어셈블리 내에서는 public에 준한 접근을 허용한다. 다른 어셈블리에서는 접근할 수 없다.|
|
|
|
|internal protected|protected와 internal의 조합이다. 동일 어셈블리 내에서 정의된 파생 클래스에만 접근을 허용한다.|
|
|
|
|
|
|
접근 제한자를 명시하지 않은 경우에는 클래스는 internal, class 내부의 멤버들은 private으로 설정된다.
|
|
|
```c#
|
|
|
class Dog //접근 제한자 생략한 상태는 internal로 되기 때문에 객체 생성이 가능하나, private으로 접근 제한자를 바꾼다면 에러 발생.
|
|
|
{
|
|
|
void bark()
|
|
|
{
|
|
|
Console.WriteLine("멍멍!");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
class Program
|
|
|
{
|
|
|
static void Main(string[] args)
|
|
|
{
|
|
|
Dog dog = new Dog();
|
|
|
dog.bark(); //bark() 메서드의 접근 제한자가 생략되서 private으로 설정되었기 때문에 호출 불가.
|
|
|
}
|
|
|
}
|
|
|
```
|
|
|
### 프로퍼티
|
|
|
접근자/설정자를 편하게 정의하기 위해서 C#에서 제공하는 문법. (attribute(field)와 똑같이 속성으로 번역되기 때문에 주의)
|
|
|
|
|
|
```c#
|
|
|
class Dog
|
|
|
{
|
|
|
private string name;
|
|
|
|
|
|
public string Name //Visual Studio에서 name을 눌러 자동생성 가능.
|
|
|
{
|
|
|
get
|
|
|
{
|
|
|
Console.WriteLine("get호출");
|
|
|
return name;
|
|
|
}
|
|
|
|
|
|
set
|
|
|
{
|
|
|
Console.WriteLine("set호출");
|
|
|
name = value;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
class Program
|
|
|
{
|
|
|
static void Main(string[] args)
|
|
|
{
|
|
|
Dog dog = new Dog();
|
|
|
dog.Name = "검둥개"; //"set호출" 출력
|
|
|
string name = dog.Name; //"get호출" 출력
|
|
|
}
|
|
|
}
|
|
|
```
|
|
|
|
|
|
<br />
|
|
|
|
|
|
## 상속
|
|
|
|
|
|
```c#
|
|
|
class Animal
|
|
|
{
|
|
|
private string name;
|
|
|
|
|
|
private void Roam() //private으로 선언되어 있어서 다른 클래스에서 호출 불가.
|
|
|
{
|
|
|
Console.WriteLine("돌아다니기");
|
|
|
}
|
|
|
|
|
|
public void Eat() //public으로 선언되어 있어서 다른 클래스에서 호출 가능
|
|
|
{
|
|
|
Console.WriteLine("쩝쩝");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
class Dog : Animal
|
|
|
{
|
|
|
protected string specific= "개"; //protected로 선언되어 있어서 Dog를 상속받는 BlackDog 클래스에서만 접근 가능.
|
|
|
|
|
|
public void Bark()
|
|
|
{
|
|
|
Console.WriteLine("멍멍!");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
class BlackDog : Dog
|
|
|
{
|
|
|
public void PrintSpecific()
|
|
|
{
|
|
|
Console.WriteLine("나는 " + specific + "과에 속합니다."); //Dog클래스의 specific에 접근하여 읽어옴.
|
|
|
}
|
|
|
}
|
|
|
|
|
|
sealed class Cat : Animal
|
|
|
{
|
|
|
public void Meow()
|
|
|
{
|
|
|
Console.WriteLine("야옹!");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//class BlackCat : Cat //Cat클래스가 sealed 되어있기 때문에 상속 불가
|
|
|
//{
|
|
|
//}
|
|
|
|
|
|
class Program
|
|
|
{
|
|
|
static void Main(string[] args)
|
|
|
{
|
|
|
Dog dog = new Dog();
|
|
|
dog.Eat(); //Animal 클래스에서 상속받은 메서드. "쩝쩝" 출력.
|
|
|
dog.Bark();
|
|
|
//dog.Roam(); //private로 선언되어 있어서 호출 불가.
|
|
|
|
|
|
BlackDog blackDog = new BlackDog();
|
|
|
blackDog.PrintSpecific(); //protected로 선언된 specific 변수에 접근해서 "개"를 읽어옴. "나는 개과입니다." 출력.
|
|
|
|
|
|
Cat cat = new Cat();
|
|
|
cat.Eat(); //Animal 클래스에서 상속받은 메서드. "쩝쩝" 출력.
|
|
|
cat.Meow();
|
|
|
}
|
|
|
}
|
|
|
```
|
|
|
|
|
|
### 형변환
|
|
|
* as 연산자 : 형변환이 가능하면 지정된 타입의 인스탄스 값 반환, 그렇지 않으면 null 반환.
|
|
|
* is 연산자 : 형변환이 가능하면 true, 그렇지 않으면 false 반환.
|
|
|
|
|
|
```c#
|
|
|
class Program
|
|
|
{
|
|
|
static void Main(string[] args)
|
|
|
{
|
|
|
Animal animal = new Animal();
|
|
|
Dog dog = animal as Dog;
|
|
|
Cat cat = new Cat();
|
|
|
|
|
|
if (dog == null) //animal은 Dog으로 변환 불가해서 null을 반환하므로 실행된다.
|
|
|
{
|
|
|
Console.WriteLine("Animal은 Dog로 변환 불가능!");
|
|
|
}
|
|
|
|
|
|
if (cat is Animal)
|
|
|
{
|
|
|
Console.WriteLine("cat는 Animal이다."); //변환 가능하므로 실행됨
|
|
|
}
|
|
|
|
|
|
if (animal is Cat)
|
|
|
{
|
|
|
Console.WriteLine("animal은 Dog다."); //변환이 안되므로 실행 안됨
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
```
|
|
|
|
|
|
### System.Object
|
|
|
자바의 Object 클래스처럼 C#에도 모든 타입의 조상격인 object 타입이 있다. object는 참조형인데 값 형식의 부모 타입이기도 하다. 이런 불일치를 해소하기 위해서 모든 값 형식은 object 밑에 존재하는 System.ValueType에서 상속받게 하고있다. 이를 도식화하면 다음과 같다.
|
|
|
|
|
|

|
|
|
|
|
|
따라서 참조형식 = object로부터 상속받는 모든 타입 - System.ValueType로부터 상속받는 모든 값 타입 이다.
|
|
|
C#에서 정의되는 모든 형식은 object로 변환하고 다시 되돌리는 것이 가능하다.
|
|
|
|
|
|
#### ToString
|
|
|
```c#
|
|
|
static void Main(string[] args)
|
|
|
{
|
|
|
Animal animal = new Animal();
|
|
|
|
|
|
Console.WriteLine(animal.ToString()); //클래스의 전체 이름인 ConsoleApplication1.Animal 출력.
|
|
|
Console.WriteLine(animal.numberOfAnimal.ToString()); //기본 타입에선 갖고 있는 값 출력.
|
|
|
}
|
|
|
```
|
|
|
|
|
|
#### GetType
|
|
|
|
|
|
```c#
|
|
|
static void Main(string[] args)
|
|
|
{
|
|
|
Animal animal = new Animal();
|
|
|
int num = 5;
|
|
|
|
|
|
Type type = animal.GetType(); //Animal 클래스의 정보를 가지고 있는 System.Type의 인스턴스 호출.
|
|
|
Type intType = num.GetType(); //기본 자료형도 호출 가능.
|
|
|
|
|
|
Console.WriteLine(type.FullName); //ConsoleApplication1.Animal 출력.
|
|
|
Console.WriteLine(type.IsClass); //true 출력.
|
|
|
|
|
|
Console.WriteLine(intType.FullName); //System.Int32 출력.
|
|
|
Console.WriteLine(typeof(Animal).FullName);
|
|
|
//typeof 예약어는 클래스의 이름에서 Type 반환. ConsoleApplication1.Animal 출력.
|
|
|
}
|
|
|
```
|
|
|
|
|
|
####Equals
|
|
|
자바와 동일. 주의할 점은 값 형식은 해당 인스턴스가 소유하고 있는 값을 대상으로 비교한다. 참조 형식은 할당된 메모리 위치를 가리키는 값이 같은지 비교한다.
|
|
|
```c#
|
|
|
static void Main(string[] args)
|
|
|
{
|
|
|
int num1 = 5;
|
|
|
int num2 = 5;
|
|
|
int num3 = 6;
|
|
|
|
|
|
Console.WriteLine(num1.Equals(num2)); //가리키는 값이 같으므로 True.
|
|
|
Console.WriteLine(num3.Equals(num2)); //가리키는 값이 다르므로 False.
|
|
|
|
|
|
Animal animal1 = new Animal();
|
|
|
Animal animal2 = new Animal();
|
|
|
Animal animal3 = animal1;
|
|
|
|
|
|
Console.WriteLine(animal1.Equals(animal2)); //서로 힙 메모리의 위치가 다르기 때문에 False.
|
|
|
Console.WriteLine(animal1.Equals(animal3)); //서로 같은 메모리를 가리키고 있기 떄문에 True.
|
|
|
}
|
|
|
```
|
|
|
|