초기 커밋.
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,113 @@
|
||||
// 베이스 클래스
|
||||
public class Animal
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public int Age { get; set; }
|
||||
}
|
||||
|
||||
// 파생클래스
|
||||
public class Dog : Animal
|
||||
{
|
||||
public void HowOld()
|
||||
{
|
||||
// 베이스 클래스의 Age 속성 사용
|
||||
Console.WriteLine("나이: {0}", this.Age);
|
||||
}
|
||||
}
|
||||
|
||||
public class Bird : Animal
|
||||
{
|
||||
public void Fly()
|
||||
{
|
||||
Console.WriteLine("{0}가 날다", this.Name);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class VirBase
|
||||
{
|
||||
public virtual void Say()
|
||||
{
|
||||
Console.WriteLine("Hello");
|
||||
}
|
||||
}
|
||||
|
||||
public class SpeakerA : VirBase
|
||||
{
|
||||
public override void Say()
|
||||
{
|
||||
Console.WriteLine("World");
|
||||
}
|
||||
}
|
||||
|
||||
public abstract class PureBase
|
||||
{
|
||||
// abstract C#키워드
|
||||
public abstract int GetFirst();
|
||||
public abstract int GetNext();
|
||||
}
|
||||
|
||||
public class DerivedA : PureBase
|
||||
{
|
||||
private int no = 1;
|
||||
|
||||
// override C#키워드
|
||||
public override int GetFirst()
|
||||
{
|
||||
return no;
|
||||
}
|
||||
|
||||
public override int GetNext()
|
||||
{
|
||||
return ++no;
|
||||
}
|
||||
}
|
||||
|
||||
public class MyBase
|
||||
{
|
||||
public string Name { get; set; }
|
||||
protected int Age { get; set; }
|
||||
}
|
||||
|
||||
public class MyDerived : MyBase
|
||||
{
|
||||
public void Run()
|
||||
{
|
||||
// 파생클래스이므로 Age 사용 가능
|
||||
Console.WriteLine("나이: {0}", this.Age);
|
||||
}
|
||||
}
|
||||
|
||||
internal class Program
|
||||
{
|
||||
private static void Main(string[] args)
|
||||
{
|
||||
var dog = new Dog();
|
||||
dog.Name = "멍멍이";
|
||||
dog.Age = 10;
|
||||
var bird = new Bird();
|
||||
bird.Name = "삐약이";
|
||||
|
||||
dog.HowOld();
|
||||
bird.Fly();
|
||||
|
||||
Animal[] animals = { dog, bird };
|
||||
foreach (var animal in animals)
|
||||
{
|
||||
Console.WriteLine($"{animal.Name}");
|
||||
}
|
||||
|
||||
}
|
||||
public void Test(object obj)
|
||||
{
|
||||
// as 연산자
|
||||
MyBase a = obj as MyBase;
|
||||
|
||||
// is 연산자
|
||||
bool ok = obj is MyBase; //true
|
||||
|
||||
// Explicit Casting
|
||||
MyBase b = (MyBase)obj;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user