-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinterface.cs
68 lines (53 loc) · 1.1 KB
/
interface.cs
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
using System;
public interface IShape
{
double GetArea();
}
public interface IDrawable
{
void Draw();
}
public struct Point
{
public int X { get; }
public int Y { get; }
public Point(int x, int y)
{
X = x;
Y = y;
}
}
public class Square : IShape
{
public double Side { get; set; }
public Square(double side) => Side = side;
public double GetArea() => Side * Side;
}
// Multiple interface implementations
public class Circle : IShape, IDrawable
{
public Circle(Point center, double radius)
{
Center = center;
Radius = radius;
}
public Point Center { get; set; }
public double Radius { get; set; }
public double GetArea() => Math.PI * Radius * Radius;
public void Draw() => Console.WriteLine("Drawing Circle");
}
internal class Program
{
private static void PrintArea(IShape shape)
{
Console.WriteLine(shape.GetArea());
}
private static void Main(string[] args)
{
var square = new Square(4);
PrintArea(square); // 16
var circle = new Circle(new Point(3, 6), 7);
PrintArea(circle); // 153.93804002589985
Console.ReadKey();
}
}