-----------------------------------------------------------------------------------------------------------------------------------
Animal Class
-----------------------------------------------------------------------------------------------------------------------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Study_014_2
{
class Animal
{
protected string name;
public Animal(string name)
{
this.name = name;
Console.WriteLine("Animal 클래스 생성자 호출");
}
public virtual void Bark()
{
Console.WriteLine("흐엉엉");
}
public string GerName()
{
return this.name;
}
}
}
|
-----------------------------------------------------------------------------------------------------------------------------------
Dog Class
-----------------------------------------------------------------------------------------------------------------------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Study_014_2
{
class Dog : Animal
{
public Dog(string name) : base(name)
{
this.name = name;
}
public override void Bark()
{
Console.WriteLine("멍멍");
}
}
}
|
-----------------------------------------------------------------------------------------------------------------------------------
Cat Class
-----------------------------------------------------------------------------------------------------------------------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Study_014_2
{
class Cat : Animal
{
public Cat(string name) : base(name)
{
this.name = name;
Console.WriteLine(this.name + "이 생성되었습니다.");
//파생클래스에서 this.를 사용하고 base.을 안쓰는이유 :
//파생클래스에서 같은 이름을 가진 변수가 재정의 될수 있기때문에
//그렇게 되면 부모클래스의 name과 파생클래스의
}
public override void Bark()
{
Console.WriteLine("양옹");
}
}
}
|
-----------------------------------------------------------------------------------------------------------------------------------
App Class
-----------------------------------------------------------------------------------------------------------------------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Study_014_2
{
class App
{
public App()
{
Cat cat = new Cat("또리");
Dog dog = new Dog("뽀삐");
string catName = cat.GerName();
string dogName = dog.GerName();
}
}
}
|
-----------------------------------------------------------------------------------------------------------------------------------
상속 && 파생 클래스를 배웠다.
'C# > 수업내용' 카테고리의 다른 글
2020-04-22 업적시스템 만들기(파일입출력) (0) | 2020.04.22 |
---|---|
2020-04-16 수업내용 Source (0) | 2020.04.16 |
2020-04-14 수업내용 (클래스,메서드) (0) | 2020.04.14 |
2020-04-13 수업내용 (0) | 2020.04.13 |
2020-04-09 수업내용 Source (0) | 2020.04.09 |