-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimalTest1.cs
More file actions
98 lines (95 loc) · 2.32 KB
/
AnimalTest1.cs
File metadata and controls
98 lines (95 loc) · 2.32 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AnimalTest1
{
// 抽象クラス動物
abstract class Animal
{
// 名前
protected string name = "";
// 吠える
public abstract void Bark();
// 動く
public abstract void Move();
// 名前を表示する
public void ShowName()
{
Console.WriteLine("名前:{0}", name);
}
}
// 犬クラス
class Dog : Animal
{
public Dog()
{
name = "犬";
}
// 吠える
public override void Bark()
{
Console.WriteLine("ワンワン");
}
// 動く
public override void Move()
{
Console.WriteLine("歩く");
}
}
// 猿クラス
class Monkey : Animal
{
public Monkey()
{
name = "猿";
}
// 吠える
public override void Bark()
{
Console.WriteLine("キーキー");
}
// 動く
public override void Move()
{
Console.WriteLine("木に登る");
}
}
// 猫クラス
class Cat : Animal
{
public Cat()
{
name = "猫";
}
// 吠える
public override void Bark()
{
Console.WriteLine("ニャーニャー");
}
// 動く
public override void Move()
{
Console.WriteLine("走る");
}
}
class Program
{
static void Main(string[] args)
{
// Animalクラスのオブジェクトの配列を生成
Animal[] animals = new Animal[3];
animals[0] = new Dog(); // 犬クラスのインスタンス生成
animals[1] = new Monkey(); // 猿クラスのインスタンスの生成
animals[2] = new Cat();
for (int i = 0; i < animals.Length; i++)
{
animals[i].ShowName();
animals[i].Move();
animals[i].Bark();
Console.WriteLine("------------");
}
}
}
}