๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐ŸŽฎunity/C#

Interface, Abstract, Virtual

by ojui 2021. 11. 12.
๋ฐ˜์‘ํ˜•

๐ŸŸฆ Interface, Abstract, Virtual

- ํŒจํ„ด ์ฑ…์„ ๊ณต๋ถ€ํ•˜๋Š”๋ฐ ์œ„์˜ ๊ฐœ๋…์ด ์žกํžˆ์ง€์•Š์•„ ์ดํ•ด๊ฐ€ ์–ด๋ ค์› ์Œ

- ๋‚˜๋ฆ„๋Œ€๋กœ ์ดํ•ดํ•œ ๊ฒƒ์„ ์ •๋ฆฌํ•จ

 

 

๐ŸŸฆ Interface

๐ŸŸง Document

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/interface

 

interface - C# Reference

:::no-loc text=interface::: (C# Reference)

docs.microsoft.com

- ์ธํ„ฐํŽ˜์ด์Šค

    - ๊ตฌํ˜„ํ•ด์•ผํ•  ๋ฉค๋ฒ„๋“ค์„ ์„ ์–ธํ•˜๋Š” ๊ฒƒ

    - ์ธํ„ฐํŽ˜์ด์Šค ์•ˆ์—์„œ ๊ตฌํ˜„ ๋ถˆ๊ฐ€

- ์ธํ„ฐํŽ˜์ด์Šค ์•ˆ์— ์“ธ ์ˆ˜ ์žˆ๋Š” ๋ฉค๋ฒ„๋Š” ์ด 4๊ฐœ

    - Methods

    - Properties

    - Indexers    // ์ธ๋ฑ์„œ๋ฅผ ๊ณต๋ถ€ํ•œ ํ›„ ์ž‘์„ฑํ•  ์˜ˆ์ •

    - Events

- ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์†๋ฐ›์€ ํด๋ž˜์Šค๋Š” ๊ผญ ๋ชจ๋“  ๋ฉค๋ฒ„๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•จ

- ๋‹ค์ค‘์ƒ์† ๊ฐ€๋Šฅ : ์ธํ„ฐํŽ˜์ด์Šค๋Š” ๋ฌด์ œํ•œ์œผ๋กœ ์ƒ์†๋ฐ›์„ ์ˆ˜ ์žˆ์Œ

 

 

 

๐ŸŸง Methods

interface ICharacter
{
    // Method
    void Fire();
    void Move();
    void Jump();
}

- ์ธํ„ฐํŽ˜์ด์Šค ์ž‘์„ฑ

 

 

 

- ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์†๋ฐ›์•„ ๊ตฌํ˜„ํ•  ํด๋ž˜์Šค

- ๋‹จ์ถ•ํ‚ค : Ctrl + .

- ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌํ˜„ ํด๋ฆญ

 

 

 

๐ŸŸง Properties

interface ICharacter
{
    // Method
    void Fire();
    void Move();
    void Jump();

    // Properties
    int Score { get; set; }
}

class player : ICharacter
{
    public int Score { get => throw new System.NotImplementedException(); set => throw new System.NotImplementedException(); }

    public void Fire()
    {
        throw new System.NotImplementedException();
    }

    public void Jump()
    {
        throw new System.NotImplementedException();
    }

    public void Move()
    {
        throw new System.NotImplementedException();
    }
}

 

 

 

๐ŸŸง Events

interface ICharacter
{
    // Method
    void Fire();
    void Move();
    void Jump();

    // Properties
    int Score { get; set; }

    // Events
    delegate void WinHandler(int min, int sec);
    event WinHandler winEvent;
}

class player : ICharacter
{
    public int Score { get => throw new System.NotImplementedException(); set => throw new System.NotImplementedException(); }

    public event ICharacter.WinHandler winEvent;

    public void Fire()
    {
        throw new System.NotImplementedException();
    }

    public void Jump()
    {
        throw new System.NotImplementedException();
    }

    public void Move()
    {
        throw new System.NotImplementedException();
    }
}

 

 

 

๐ŸŸฆ Abstract

๐ŸŸง Document

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/abstract-and-sealed-classes-and-class-members

 

Abstract and Sealed Classes and Class Members - C# Programming Guide

The abstract keyword in C# creates incomplete classes and class members. The sealed keyword prevents inheritance of previously virtual classes or class members.

docs.microsoft.com

- ์ถ”์ƒํด๋ž˜์Šค

    - ์ธํ„ฐํŽ˜์ด์Šค๋Š” ๋ฉค๋ฒ„ ์ „์ฒด๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์—†๊ณ , ์„ ์–ธ๋งŒ ํ•จ

    - ์ถ”์ƒํด๋ž˜์Šค๋Š” ๋ฉค๋ฒ„๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๊ณ , ์ถ”์ƒ ๋ฉ”์„œ๋“œ๋ฅผ ์„ ์–ธํ•จ

- ์ถ”์ƒ๋ฉ”์„œ๋“œ๋Š” ๋ฌด์กฐ๊ฑด ๊ตฌํ˜„ํ•ด์•ผํ•จ

- ์ถ”์ƒํด๋ž˜์Šค๋Š” ์ธ์Šคํ„ด์Šค ์ƒ์„ฑํ•  ์ˆ˜ ์—†์Œ

- ํด๋ž˜์Šค์ด๊ธฐ๋•Œ๋ฌธ์— ๋‹ค์ค‘์ƒ์†์ด ๋ถˆ๊ฐ€๋Šฅ

 

 

 

๐ŸŸง abstract

public abstract class Monster : MonoBehaviour
{
    // ๊ตฌํ˜„๋ถ€
    public int hp = 100;
    public void Jump()
    {
        Debug.Log("jump!");
    }

    // ์ถ”์ƒ๋ฉ”์„œ๋“œ
    public abstract void Move();
}

class Gorlem : Monster
{
    public override void Move()
    {
        throw new System.NotImplementedException();
    }
}

- ์ธํ„ฐํŽ˜์ด์Šค์™€๋Š” ๋‹ค๋ฅด๊ฒŒ ๋ฉค๋ฒ„๋“ค์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Œ

- 1๊ฐœ์˜ ํด๋ž˜์Šค๋งŒ ์ƒ์†๋ฐ›์„ ์ˆ˜ ์žˆ๊ธฐ๋•Œ๋ฌธ์— ์ตœ์ƒ์œ„ ํด๋ž˜์Šค์— MonoBehaviour๋ฅผ ์ƒ์†๋ฐ›์•˜์Œ

    - ์œ ๋‹ˆํ‹ฐ์—์„œ ๊ฒŒ์ž„์˜ค๋ธŒ์ ํŠธ์— ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋„ฃ๊ธฐ์œ„ํ•ด์„œ๋Š” MonoBehaviour ํด๋ž˜์Šค๋ฅผ ๋ฌด์กฐ๊ฑด ์ƒ์†๋ฐ›์•„์•ผํ•จ

 

 

 

๐ŸŸง sealed

public abstract class Monster : MonoBehaviour
{
    // ๊ตฌํ˜„๋ถ€
    public int hp = 100;
    public void Jump()
    {
        Debug.Log("jump!");
    }

    // ์ถ”์ƒ๋ฉ”์„œ๋“œ
    public abstract void Move();
}

class Gorlem : Monster
{
    public override void Move()
    {
        throw new System.NotImplementedException();
    }
}

class WoodGorlem : Gorlem
{
    public override void Move()
    {
        // ์žฌ์ •์˜
    }
}

- ๋ชฌ์Šคํ„ฐ ์ถ”์ƒํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์€ ๊ณจ๋ ˜ ํด๋ž˜์Šค๋ฅผ ์šฐ๋“œ๊ณจ๋ ˜ ํด๋ž˜์Šค์—์„œ ์ƒ์†๋ฐ›์Œ

- ์šฐ๋“œ๊ณจ๋ ˜ ํด๋ž˜์Šค์—์„œ ์ถ”์ƒ๋ฉ”์„œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋“œํ•ด์„œ ์žฌ์ •์˜ํ•  ์ˆ˜ ์žˆ์Œ

 

 

 

- ๋ฉ”์„œ๋“œ์— sealed ํ‚ค์›Œ๋“œ๋ฅผ ์“ธ ๊ฒฝ์šฐ ์ž์‹ ํด๋ž˜์Šค์—์„œ ์˜ค๋ฒ„๋ผ์ด๋“œ๊ฐ€ ๋ถˆ๊ฐ€ํ•จ

 

 

 

- ํด๋ž˜์Šค์—๋„ ์“ธ ์ˆ˜ ์žˆ์Œ

- ํ•ด๋‹น ๋ฉ”์„œ๋“œ๋ฅผ ์ƒ์†๋ฐ›์ง€ ๋ชปํ•˜๋„๋ก ํ•จ

 

 

 

๐ŸŸฆ Virtual

๐ŸŸง Document

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/virtual

 

virtual - C# Reference

virtual (C# Reference) In this article --> The virtual keyword is used to modify a method, property, indexer, or event declaration and allow for it to be overridden in a derived class. For example, this method can be overridden by any class that inherits i

docs.microsoft.com

- Method, Property๋ฅผ overrideํ•  ์ˆ˜ ์žˆ๊ฒŒํ•˜๋Š” ํ‚ค์›Œ๋“œ

- virtual์€ ๊ตฌํ˜„์„ ์•ˆํ•ด๋„ ๋จ

 

 

 

๐ŸŸง Method

- override๋ฅผ ํ•˜์ง€ ์•Š์•„๋„ ์˜ค๋ฅ˜๊ฐ€ ๋‚˜์ง€ ์•Š์Œ

 

 

 

class Food
{
    string name;
    public virtual void Show() { }
}

class Salad : Food
{
    public override void Show()
    {
    }
}

- ์ธํ„ฐํŽ˜์ด์Šค, ์ถ”์ƒํด๋ž˜์Šค์ฒ˜๋Ÿผ overrideํ•˜๋ฉด ๋จ

 

 

 

๐ŸŸง Property

class Food
{
    float calorie;
    public virtual float Calorie { get; set; }
}

class Salad : Food
{
    public override float Calorie { get => base.Calorie; set => base.Calorie = value; }
}

 

 

 

 

๐ŸŸง ์ถ”์ƒํด๋ž˜์Šค์™€ ๊ฐ™์ด ์“ฐ๋Š” ๊ฒฝ์šฐ

public abstract class Monster : MonoBehaviour
{
    // ๊ตฌํ˜„๋ถ€
    public int hp = 100;
    public void Jump()
    {
        Debug.Log("jump!");
    }

    // virtual
    public virtual void Attack() { }

    // ์ถ”์ƒ๋ฉ”์„œ๋“œ
    public abstract void Move();
}

- ์œ„์™€ ๊ฐ™์ด ์ถ”์ƒ ํด๋ž˜์Šค ์•ˆ์— virtual ํ‚ค์›Œ๋“œ๋ฅผ ์“ธ ์ˆ˜ ์žˆ์Œ

๋ฐ˜์‘ํ˜•

'๐ŸŽฎunity > C#' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[C#] Nullable Type  (0) 2022.11.14
[C#] ref & out  (0) 2022.10.26
C# ์ฃผ์„์— ๋Œ€ํ•œ XML ํƒœ๊ทธ  (0) 2021.11.01