-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChapter15.cs
More file actions
115 lines (96 loc) · 3.13 KB
/
Chapter15.cs
File metadata and controls
115 lines (96 loc) · 3.13 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System;
namespace Chapter15{
interface IInfo{
string GetName();
string GetAge();
}
class CA : IInfo{
public string name;
public int Age;
public string GetName() {return name;}
public string GetAge() {return Age.ToString();}
}
class CB : IInfo{
public string First;
public string Last;
public double PersonAge;
public string GetName(){return First + " " + Last;}
public string GetAge(){return PersonAge.ToString();}
}
class MyClass : IComparable
{
public int TheValue;
public int CompareTo(object obj)
{
MyClass mc = (MyClass)obj;
if(this.TheValue < mc.TheValue) return -1;
if(this.TheValue > mc.TheValue) return 1;
return 0;
}
}
class Program{
static void PrintInfo(IInfo item){
Console.WriteLine("Name: {0}, Age: {1}", item.GetName(), item.GetAge());
}
static void PrintOut(string s, MyClass[] mc)
{
Console.Write(s);
foreach(var m in mc)
Console.Write("{0} ", m.TheValue);
Console.WriteLine();
}
interface ILiveBirth
{
string BabyCalled();
}
class Animal{}
class Cat : Animal, ILiveBirth
{
public string BabyCalled()
{
return "kitten";
}
}
class Dog : Animal, ILiveBirth
{
string ILiveBirth.BabyCalled()
{
return "puppy";
}
}
class Bird : Animal{}
static void MainTest(){
Console.WriteLine("Alex is cool!");
Console.WriteLine("******************************************");
CA a = new CA(){name = "John Doe", Age = 35};
CB b = new CB(){First = "Jane", Last = "Doe", PersonAge = 33};
PrintInfo(a);
PrintInfo(b);
Console.WriteLine("*******************************************");
var MyInt = new []{20,4,16,9,2};
MyClass[] mcArr = new MyClass[5];
for(int i = 0; i < 5; ++i)
{
mcArr[i] = new MyClass();
mcArr[i].TheValue = MyInt[i];
}
PrintOut("Initial Order: ", mcArr);
Array.Sort(mcArr);
PrintOut("Sorted Order: ", mcArr);
/*
如果一个类实现了多个接口,并且其中一些接口有相同签名和返回类型的成员,那么类可以实现单个成员
来满足所有包含重复成员的接口
*/
Console.WriteLine("********************************************");
Animal[] animalArray = new Animal[3];
animalArray[0] = new Cat();
animalArray[1] = new Bird();
animalArray[2] = new Dog();
foreach(Animal m in animalArray){
ILiveBirth n = m as ILiveBirth;
if(n != null)
Console.WriteLine("Baby is called: {0}", n.BabyCalled());
}
}
}
}