μΈν°νμ΄μ€λ₯Ό μμλ°λ κ²κ³Ό ν΄λμ€λ₯Ό μμλ°λ κ²μ μ°¨μ΄μ
μΈν°νμ΄μ€λ₯Ό μμλ°λ κ² (Interface Inheritance)
μΈν°νμ΄μ€λ λ©μλ μκ·Έλμ²(μ΄λ¦, 맀κ°λ³μ, λ°ν κ°)λ§ μ μνλ€.
ν΄λμ€κ° μΈν°νμ΄μ€λ₯Ό μμλ°μΌλ©΄ ν΄λΉ ν΄λμ€λ μΈν°νμ΄μ€κ° μ μν λ©μλλ€μ λͺ¨λ ꡬνν΄μΌ νλ€. (νλλΌλ ꡬνλμ§ μμΌλ©΄ μ»΄νμΌ μλ¬κ° λλ€.)
κ·Έλ¦¬κ³ λ€μ€μμμ΄ κ°λ₯νλ€. (μΌν(,)λ‘ μ΄μ΄μ°λ©΄ λλ€.)
interface IShape
{
double CalculateArea();
}
class Circle : IShape
{
double radius;
public Circle(double r)
{
radius = r;
}
public double CalculateArea()
{
return Math.PI * radius * radius;
}
}
class Square : IShape
{
double side;
public Square(double s)
{
side = s;
}
public double CalculateArea()
{
return side * side;
}
}
class Program
{
static void Main()
{
IShape circle = new Circle(5);
IShape square = new Square(4);
Console.WriteLine($"Circle Area: {circle.CalculateArea()}");
Console.WriteLine($"Square Area: {square.CalculateArea()}");
}
}
ν΄λμ€λ₯Ό μμλ°λ κ² (Class Inheritance)
ν΄λμ€ μμμ κΈ°μ‘΄ ν΄λμ€μ λͺ¨λ λ©€λ²(νλ, λ©μλ)μ λμμ μμλ°λ κ²μ΄λ€.
μμλ ν΄λμ€λ κΈ°μ‘΄ ν΄λμ€μ λͺ¨λ κΈ°λ₯κ³Ό λμμ κ·Έλλ‘ μ¬μ©ν μ μκ³ , μ€λ²λΌμ΄λ©μ ν΅ν΄ λμμ λ€λ₯΄κ² ν μλ μλ€.
μΈν°νμ΄μ€μ λ¬λ¦¬ ν΄λμ€λ λ€μ€μμμ΄ λΆκ°λ₯νλ€. κΈ°λ³Έμ μΌλ‘ νλμ ν΄λμ€λ§ μμλ°μ μ μλ€.
using System;
class Vehicle
{
public void StartEngine()
{
Console.WriteLine("Engine started");
}
public void StopEngine()
{
Console.WriteLine("Engine stopped");
}
}
class Car : Vehicle
{
public void Drive()
{
Console.WriteLine("Car is driving");
}
}
class Program
{
static void Main()
{
Car myCar = new Car();
myCar.StartEngine();
myCar.Drive();
myCar.StopEngine();
}
}
μΈν°νμ΄μ€λ₯Ό μμνλ μΈν°νμ΄μ€λ₯Ό λ§λλ μ΄μ
- κΈ°μ‘΄μ μΈν°νμ΄μ€λ₯Ό μ¬μ¬μ©νλ©΄μ νμ₯ν μ μλ€.
- κΈ°μ‘΄ μμ€ μ½λμ μν₯μ μ£Όμ§ μκ³ μλ‘μ΄ κΈ°λ₯μ μΆκ°ν λ, μΈν°νμ΄μ€λ₯Ό μ§μ μμ νλ©΄ μ΄ μΈν°νμ΄μ€λ₯Ό μμνλ ν΄λμ€λ€ μ λΆμ μ»΄νμΌ μ€λ₯κ° μκΈ΄λ€. μ΄ λ μΈν°νμ΄μ€λ₯Ό μμνλ μΈν°νμ΄μ€λ₯Ό μ¬μ©νλ©΄ μ’λ€.
using System;
interface IShape
{
void Draw();
}
interface IResizableShape : IShape
{
void Resize(int width, int height);
}
class Rectangle : IResizableShape
{
public void Draw()
{
Console.WriteLine("Drawing a rectangle");
}
public void Resize(int width, int height)
{
Console.WriteLine($"Resizing rectangle to {width}x{height}");
}
}
class Circle : IShape
{
public void Draw()
{
Console.WriteLine("Drawing a circle");
}
}
class Program
{
static void Main()
{
IResizableShape rectangle = new Rectangle();
rectangle.Draw();
rectangle.Resize(10, 5);
IShape circle = new Circle();
circle.Draw();
}
}
μ μ½λλ₯Ό μ€λͺ νμλ©΄
- IShape μΈν°νμ΄μ€: Draw λ©μλ μ μ
- IResizableShape μΈν°νμ΄μ€: IShape μΈν°νμ΄μ€λ₯Ό μμ(Draw λ©μλ) + Resize λ©μλ μ μ
- Rectangle ν΄λμ€: IResizableShape μΈν°νμ΄μ€μ Draw λ©μλ, Resize λ©μλ ꡬν
- Circle ν΄λμ€: IShape μΈν°νμ΄μ€μ Draw λ©μλ ꡬν
μΈν°νμ΄μ€μ κΈ°λ³Έ ꡬν λ©μλ
μΈν°νμ΄μ€λ λ³Έλ λ©μλλ₯Ό μ μνκΈ°λ§ νμ§, ꡬννμ§λ μμλ€. κ·Έλ¦¬κ³ μΈν°νμ΄μ€λ₯Ό μμλ°μ ν΄λμ€μμλ μ μλ λ©μλλ₯Ό λͺ¨λ ꡬνν΄μΌνλ€.
νμ§λ§ μΈν°νμ΄μ€μμ ꡬνμ νκ² λλ©΄, ν΄λμμμλ μ΄ λ©μλλ€μ΄ μ΄λ―Έ ꡬνμ΄ λμ΄μκΈ° λλ¬Έμ λ°λμ ꡬνμ ν΄μΌνλ λ¬Έμ κ° μμ΄μ§λ€. κ·Έλ κΈ° λλ¬Έμ μ νμ ꡬνμ΄ κ°λ₯ν΄μ§λ€.
using System;
public interface ICalculator
{
int Add(int a, int b);
// κΈ°λ³Έ ꡬν λ©μλ
// μ΄ λ©μλλ₯Ό μ€λ²λΌμ΄λνμ§ μλ ν΄λμ€λ μ΄ κΈ°λ³Έ ꡬνμ μ¬μ©νκ² λ¨
int Subtract(int a, int b)
{
return a - b;
}
}
public class BasicCalculator : ICalculator
{
public int Add(int a, int b)
{
return a + b;
}
// Subtract λ©μλλ₯Ό μ€λ²λΌμ΄λνμ§ μμλ λ¨
}
public class AdvancedCalculator : ICalculator
{
public int Add(int a, int b)
{
return a + b;
}
// κΈ°λ³Έ ꡬν λ©μλλ₯Ό μ€λ²λΌμ΄λνμ¬ κ³ μ ν λμμ μ μν¨
public int Subtract(int a, int b)
{
return Math.Abs(a - b);
}
}
class Program
{
static void Main()
{
ICalculator basicCalc = new BasicCalculator();
ICalculator advancedCalc = new AdvancedCalculator();
Console.WriteLine("Basic Calculator:");
Console.WriteLine("Add: " + basicCalc.Add(5, 3));
Console.WriteLine("Subtract: " + basicCalc.Subtract(10, 4));
Console.WriteLine("\nAdvanced Calculator:");
Console.WriteLine("Add: " + advancedCalc.Add(8, 2));
Console.WriteLine("Subtract: " + advancedCalc.Subtract(4, 10));
}
}
μΆμ ν΄λμ€
μΆμ ν΄λμ€λ μΈν°νμ΄μ€μ²λΌ ꡬνλΆκ° μλ λ©μλλ₯Ό κ°μ§κ³ μλ€. μ΄λ κ² κ΅¬νλΆκ° μλ λ©μλλ₯Ό μΆμλ©μλλΌκ³ νλ€. μΆμν΄λμ€λ λ§μ°¬κ°μ§λ‘ μμλ°μΌλ©΄ μ΄ μΆμ λ©μλλ€μ μ λΆ κ΅¬νμ ν΄μΌνλ€.
μΈν°νμ΄μ€μμ μ°¨μ΄μ μ΄λΌλ©΄, μΆμ ν΄λμ€λ ꡬνλΆκ° μλ μΌλ° λ©μλλ ν¬ν¨ν μ μλ€. μ¦, μΆμ ν΄λμ€λ νλ μ΄μμ μΆμ λ©μλλ₯Ό ν¬ν¨νκΈ°λ§ νλ©΄ λλ€. κ·Έλ¦¬κ³ ν΄λμ€μ΄κΈ° λλ¬Έμ λ€μ€μμμ΄ μ λλ€.
*μΆμ λ©μλκ° μλ μΆμ νλ‘νΌν°λ₯Ό νλλ§ ν¬ν¨νκ³ μμ΄λ λλ€.
using System;
abstract class Shape
{
public abstract double CalculateArea(); // μΆμ λ©μλ
}
class Circle : Shape
{
private double radius;
public Circle(double r)
{
radius = r;
}
public override double CalculateArea()
{
return Math.PI * radius * radius;
}
}
class Rectangle : Shape
{
private double width;
private double height;
public Rectangle(double w, double h)
{
width = w;
height = h;
}
public override double CalculateArea()
{
return width * height;
}
}
class Program
{
static void Main()
{
Circle circle = new Circle(5);
Rectangle rectangle = new Rectangle(4, 6);
Console.WriteLine("Circle Area: " + circle.CalculateArea());
Console.WriteLine("Rectangle Area: " + rectangle.CalculateArea());
}
}
'π₯οΈ > C#' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[C#] νλ‘νΌν° (0) | 2023.08.18 |
---|---|
[μ΄κ²μ΄ C#μ΄λ€] Chapter08: μ°μ΅λ¬Έμ (0) | 2023.08.18 |
[μ΄κ²μ΄ C#μ΄λ€] Chapter07: μ°μ΅λ¬Έμ (0) | 2023.08.17 |
[C#] κΉμλ³΅μ¬ μμ볡μ¬, readonly, ꡬ쑰체, νν (0) | 2023.08.17 |
[C#] λΆν ν΄λμ€, νμ₯ λ©μλ (0) | 2023.08.17 |