추상 클래스
- 1. abstract 키워드만 붙인 case
- 2. abstract 키워드 + abstract 메서드
1번의 경우 상속 받으면 객체 생성 가능
2 번의 경우 상속 + 추상 메서드까지 구현해야 객체 생성 가능
목적
1.가이드 - 내가 만든 클래스로 객체를 못만들게 하려는 목적
2. 일관성유지 - 자식 클래스의 인터페이스를 동일하게 유지
3. 다형성 구현
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//추상 클래스
abstract class Smart{public int iNum;}
class Child1: Smart{ public new int iNum;}
class Program
{
static void Main(string[] args)
{
Child1 aChild1 = new Child1();
}
}
추상 메소드
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//추상 클래스
abstract class Smart // 추상 메소드를 가지면 자동으로 추상 클래스가 된다.
{
abstract public void Test1();
public abstract void Test2();// 순서 노상관
// 추상 메소드 조건
// 1. abstract 가 붙는다.
// 2. 본체(body)가 없다.
// 3. 반드시 private 으로 선언할 수 없다.
}
abstract class Child1: Smart
{// 추상 메소드를 가진 클래스를 상속 받으면 추상 클래스가 됨
}
class Child2 : Child1
{
public override void Test1()
{
}
public override void Test2()
{
}
}
class Program
{
static void Main(string[] args)
{
}
}
사용 예제
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
abstract class Musician
{
public abstract void greet();
}
class Singer : Musician
{
public override void greet()
{
Console.WriteLine("안녕하세요 저는 싱어입니다.");
}
}
class Drum : Musician
{
public override void greet()
{
Console.WriteLine("안녕하세요 저는 드러머입니다.");
}
}
class Bell : Musician
{
public override void greet()
{
Console.WriteLine("안녕하세요 저는 벨 연주자입니다.");
}
}
class Piano : Musician
{
public override void greet()
{
Console.WriteLine("안녕하세요 저는 피아노 연주자입니다.");
}
}
class Program
{
static void Main(string[] args)
{
// Musician M = new Musician();
Singer S = new Singer();
Drum D = new Drum();
Bell B = new Bell();
Piano P = new Piano();
// 일반적인 greet() 메소드 호출
S.greet();
D.greet();
B.greet();
P.greet();
// 배열을 통한 greet() 메소드 호출
Musician[] M = new Musician[] { S, D, B, P };
foreach (var item in M)
{
item.greet();
}
}
}
인덱서
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class SmartInt
{
public int[] iNum;
public SmartInt(int Len)
{
iNum = new int[Len];
for (int i = 0; i < iNum.Length; i++)
{
iNum[i] = 100 + i;
}
}
public int this[int i]
{
get { return iNum[i]; }
set { iNum[i] = value; }
}
}
class Program
{
static void Main(string[] args)
{
SmartInt I = new SmartInt(5);
Console.WriteLine(I.iNum[0]);
Console.WriteLine(I.iNum[1]);
Console.WriteLine(I.iNum[2]);
Console.WriteLine(I.iNum[3]);
Console.WriteLine(I.iNum[4]);
Console.WriteLine(I[0]);
Console.WriteLine(I[1]);
Console.WriteLine(I[2]);
Console.WriteLine(I[3]);
Console.WriteLine(I[4]);
}
}
구조체
구조체는 참조 형식일것 같지만 값형식이다.
1. 상속 불가
2. 스택에 할당
3. 필드 선언(int a =0;) 시 const 또는 static 으로 선언한 경우에만 초기화 가 가능
4. 디폴트 생성자와 소멸자 불가능
5. 생성자를 사용하려면 필드가 모두 할당이 되어 있어야함
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class Program
{
struct Point
{
//public int x = 0; 바로 초기화 안됨
public int x; // 바로 초기화 안됨
public int y;
public Point(int x, int y) // 구조체의 생성자는 무조건 모든 변수를 초기화 해줘야함
{
this.x = x;
this.y = y;
}
}
// generic class 도 가능
static void Main(string[] args)
{
// 구조체는 객체 참조 변수가 아니다. 실제로 값이 만들어진다.
// new Point() 명령어로 heap 영역에 구조체가 생성된다.
// = 연산자로 스택영역에 존재하는 구조체에 heap 영역의 구조체가 복사된다.
Point point = new Point();
Point point1 = new Point(4,5);
Console.WriteLine(point.x);
Console.WriteLine(point.y);
Console.WriteLine(point1.x);
Console.WriteLine(point1.y);
}
}
'공부,일 > C#' 카테고리의 다른 글
window forms (0) | 2021.06.04 |
---|---|
210604 (0) | 2021.06.04 |
List class (0) | 2021.06.03 |
210602 (0) | 2021.06.02 |
210525_2 (0) | 2021.05.25 |
댓글