-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAufgabe6
91 lines (68 loc) · 1.9 KB
/
Aufgabe6
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
using System;
using System.Collections.Generic;
namespace Aufgabe6
{
//Singleton
public class Person
{
public Person(String Name,int Age ){
this.Name=Name;
this.Age=Age;
this.Id = IDGenerator.GetInstance().GibMirNeId();
Program.personen.Add(this);
}
public string Name;
public int Age;
private int Id;
public override string ToString()
{
return "Name:" + Name + ", Age: " + Age + ", " + "Id: " + Id;
}
}
/*
class GlobalVariables
{
// public static int letzteID = 1;
public static IDGenerator DerIdMacher = new IDGenerator();
}
*/
public class IDGenerator
{
private IDGenerator()
{
letzteID = 1;
}
private static IDGenerator _instance;
public static IDGenerator GetInstance()
{
if (_instance == null)
_instance = new IDGenerator();
return _instance;
}
private int letzteID;
public int GibMirNeId()
{
return letzteID++;
}
}
class Program
{
public static List<Person> personen = new List<Person>();
public static void Main(string[] args)
{
// Eine Stelle, an der Personen angelegt werden
new Person("Horst",4 );
new Person("Horst", 45);
new Person("Walter", 33);
new Person( "Karl-Heinz", 22);
// Eine ANDERE Stelle, an der Personen angelegt werden
new Person("Brunhilde",56);
new Person("Maria",75);
new Person("Kunigunde", 22);
new Person("Tusnelda",12);
foreach (var person in personen)
Console.WriteLine(person);
Console.WriteLine("Hello World!");
}
}
}