8000 Minor changes to Abstract class · EpicSharpCode/StrategyExample@2a00f13 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2a00f13

Browse files
committed
Minor changes to Abstract class
1 parent 4d94f6d commit 2a00f13

File tree

5 files changed

+68
-31
lines changed

5 files changed

+68
-31
lines changed

StrategyExample/People/IPeople.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace StrategyExample.People
8+
{
9+
internal interface IPeople
10+
{
11+
void ShowInfo();
12+
}
13+
}

StrategyExample/People/ManLeo.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace StrategyExample.People
8+
{
9+
internal class ManLeo : PeopleBase
10+
{
11+
public ManLeo()
12+
{
13+
name = "Leo";
14+
birthday = new DateTime(1997, 10, 15);
15+
inventoryCapacity = 10;
16+
17+
InitInventory();
18+
ShowInfo();
19+
}
20+
}
21+
}

StrategyExample/People/PeopleBase.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,23 @@
77

88
namespace StrategyExample.People
99
{
10-
public abstract class PeopleBase
10+
public abstract class PeopleBase : IPeople
1111
{
12-
public string name { get; private set; }
13-
public DateTime birthday { get; private set; }
12+
protected string name;
13+
protected DateTime birthday;
1414

15-
public Inventory inventory;
15+
protected Inventory inventory;
16+
protected int inventoryCapacity;
1617

17-
public PeopleBase(string _name, DateTime _birthday, int _inventoryCapacity)
18-
{
19-
name = _name;
20-
birthday = _birthday;
21-
ShowInfo();
18+
public PeopleBase() { }
2219

23-
inventory = new Inventory(_inventoryCapacity);
20+
public void InitInventory()
21+
{
22+
inventory = new Inventory(inventoryCapacity);
2423
}
2524

2625
public void ShowInfo() => Console.WriteLine($"I am {name}. My birthday is {birthday.ToString("d")}.");
26+
public Inventory GetInventory() => inventory;
27+
public string GetName() => name;
2728
}
2829
}

StrategyExample/Program.cs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,33 @@ internal class Program
1414

1515
public static void Main(string[] args)
1616
{
17-
characters.Add(new PeopleBase("Leo", new DateTime(1997, 10, 15), 10));
17+
characters.Add(new ManLeo());
1818
var man = characters.Last();
1919
OutputEmptyLine();
20-
man.inventory.AddItem(new Tomato());
21-
man.inventory.items.Last().Examine();
22-
man.inventory.items.Last().TryEat();
23-
man.inventory.items.Last().TryRead();
20+
man.GetInventory().AddItem(new Tomato());
21+
man.GetInventory().items.Last().Examine();
22+
man.GetInventory().items.Last().TryEat();
23+
man.GetInventory().items.Last().TryRead();
2424
OutputEmptyLine();
25-
man.inventory.AddItem(new RottenApple());
26-
man.inventory.items.Last().Examine();
27-
man.inventory.items.Last().TryEat();
28-
man.inventory.items.Last().TryRead();
25+
man.GetInventory().AddItem(new RottenApple());
26+
man.GetInventory().items.Last().Examine();
27+
man.GetInventory().items.Last().TryEat();
28+
man.GetInventory().items.Last().TryRead();
2929
OutputEmptyLine();
30-
man.inventory.AddItem(new BurntBook());
31-
man.inventory.items.Last().Examine();
32-
man.inventory.items.Last().TryEat();
33-
man.inventory.items.Last().TryRead();
30+
man.GetInventory().AddItem(new BurntBook());
31+
man.GetInventory().items.Last().Examine();
32+
man.GetInventory().items.Last().TryEat();
33+
man.GetInventory().items.Last().TryRead();
3434
OutputEmptyLine();
35-
man.inventory.AddItem(new TheWhaleBook());
36-
man.inventory.items.Last().Examine();
37-
man.inventory.items.Last().TryEat();
38-
man.inventory.items.Last().TryRead();
35+
man.GetInventory().AddItem(new TheWhaleBook());
36+
man.GetInventory().items.Last().Examine();
37+
man.GetInventory().items.Last().TryEat();
38+
man.GetInventory().items.Last().TryRead();
3939
OutputEmptyLine();
40-
man.inventory.AddItem(new EatableBook());
41-
man.inventory.items.Last().Examine();
42-
man.inventory.items.Last().TryEat();
43-
man.inventory.items.Last().TryRead();
40+
man.GetInventory().AddItem(new EatableBook());
41+
man.GetInventory().items.Last().Examine();
42+
man.GetInventory().items.Last().TryEat();
43+
man.GetInventory().items.Last().TryRead();
4444

4545
Console.ReadLine();
4646
}

StrategyExample/StrategyExample.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656
<Compile Include="Item\Behaviours\Food\NotEatable.cs" />
5757
<Compile Include="People\Inventory.cs" />
5858
<Compile Include="Item\ItemBase.cs" />
59+
<Compile Include="People\IPeople.cs" />
60+
<Compile Include="People\ManLeo.cs" />
5961
<Compile Include="People\PeopleBase.cs" />
6062
<Compile Include="Program.cs" />
6163
<Compile Include="Properties\AssemblyInfo.cs" />

0 commit comments

Comments
 (0)
0