Unit 3 Dot Net Interface
Unit 3 Dot Net Interface
// Triangle.cs
using System;
namespace CustomInterface {
public class Triangle : Shape, IPoint {
public Triangle() { }
public Triangle(string name) : base(name) { }
public override void Draw() {
Console.WriteLine("Drawing {0} the Triangle", PetName); }
public byte Points {
get {
return 3;
}}}}
// Hexagon.cs using System;
namespace CustomInterface {
public class Hexagon : Shape, IPoint {
public Hexagon() { }
public Hexagon(string name) : base(name) { } public
override void Draw() { Console.WriteLine("Drawing
{0} the Hexagon", PetName); }
public byte Points {
get {
return 6; } } } }
// Shape.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CustomInterface {
public abstract class Shape {
protected string shapeName;
public Shape() {
shapeName = "NoName"; }
public Shape(string s) {
shapeName = s;
}
public virtual void Draw() {
Console.WriteLine("Shape.Draw()"); }
public string PetName {
get { return shapeName; }
set { shapeName = value; } }
static void Main(string[] args) {
}}
Now we have a set of types that support IPoint interface. Next question is how we
interact with the new interface. Let's invoke the methods of interface directly from
the object level. First, look at the following Main() method:
static void Main(string[] args)
{
Hexagon hex = new Hexagon();
Console.WriteLine("Points: {0}", hex.Points);
Console.ReadLine(); }