abstract
https://learn.microsoft.com/ko-kr/dotnet/csharp/language-reference/keywords/abstract
abstract - C# 참조 - C#
abstract - C# 참조
learn.microsoft.com
abstract class로 우선 추상화할 클래스를 선택한다
이 클래스는 인스턴스가 생성될 수 없고
상속을 한 후 자식 클래스에서 강제로 구현을 해야하는 요소들에 abstract로 지정한다.
만약 도형이라는 추상적인 클래스가 있다면 인스턴스 생산을 금지하는편이 편의성에 도움이 될 것이다.
파생된 삼각형 사각형같은 클래스는 실제로 사용을 하기위한 클래스로
상속 받은 후 강제로 구현해야하는부분들을 구현하고 인스턴스를 생산할 수 있게 된다.
abstract는 구현이 되지 않았음을 냄
abstract한정자는 클래스, 메서드, 속성, 인덱서 및 이벤트와 함께 사용될 수 있음
클래스 선언에 abstract를 사용하면 클래스 자체를 인스턴스화하는것이 불가능해짐
다른 클래스의 기본 클래스(부모클래스)로만 사용됨을 나타냄
표시된 멤버는 추상클래스에서 파생된 비 추상 클래스에 의해 구현되어야 함
추상 메서드는 암시적으로 가상 메서드이다(virtual)
실제 구현이 없으므로 본문이 없음 ({})
구현은 자손클래스의 override에 의해 제공
추상메서드 선언에 static, virtual한정자 사용은 오류
속성(프로퍼티) 추상화
추상 속성 정의 방법 - C# 프로그래밍 가이드 - C#
C#에서 추상 속성을 정의하는 방법에 대해 알아봅니다. 추상 속성을 선언하는 것은 클래스가 속성을 지원함을 의미합니다. 파생된 클래스는 접근자를 구현합니다.
learn.microsoft.com
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Systehttp://m.Threading.Tasks;
namespace _2024_04_06_변석진
{
/*
* 상속은 is a
* abstract 도 중간에 들어가 질까?
* 콘솔창에 색깔도 넣어서
*
*/
class Primitive
{
virtual public void PrintPrim()
{
Console.WriteLine("Prim");
}
}
abstract class A_Foo : Primitive
{
//abstract int a; //abstract는 멤버 변수에는 당연히 못씀
//int a_property { abstract get; abstract set; } //속성에 사용가능하다했는데 자동 속성 에는 안되나
int a;
//추상 메서드는 추상 클래스에서만 사용이 가능하다
public abstract int A
{
get;
set;
}
//abstract가 아닌것은 구현이 이미 완료된 메서드라는것
//상속된 메서드도 다시 추상화를 시킬수가 있나? 된다 재정의를 강제한다 /////아마 아에 다른 형으로 이름이 덮어씌워지는것이지도
public abstract override string ToString();
virtual public string GetHello()
{
return "안녕\n";
}
public abstract override void PrintPrim();
public abstract void Bark();
}
class Bar : A_Foo
{
int b;
public override int A
{
get => throw new NotImplementedException();
set => throw new NotImplementedException();
}
public override void Bark()
{
throw new NotImplementedException();
}
public override void PrintPrim()
{
//레전드 ㅋㅋㅋㅋㅋㅋㅋ virtual로 숨어있는 메서드 꺼내기 https://stackoverflow.com/questions/2323401/how-to-call-base-base-method
Console.WriteLine("Called from Special Derived.");
var ptr = typeof(Primitive).GetMethod("PrintPrim").MethodHandle.GetFunctionPointer();
var baseSay = (Action)Activator.CreateInstance(typeof(Action), this, ptr);
baseSay();
}
public override string ToString()
{
throw new NotImplementedException();
}
}
//이런건 주로 어떤 도형이라는것이 있을때
//삼각형 사각형같은것들은 인스턴스가 필요하지만
//도형 그자체는 인스턴스가 만들어지면 안되니까
//그 이외에도 인스턴스의 생성을 막을 용도로 많이 사용하면 되지 않을까
internal class Testring
{
static void Main(string[] args)
{
Primitive p_Bar = new Bar();
p_Bar.PrintPrim();
}
}
}