Created
October 9, 2011 10:43
-
-
Save hodzanassredin/1273540 to your computer and use it in GitHub Desktop.
c# trait mixins via intefaces and extension methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using Traits; | |
namespace TraitsMixin | |
{ | |
//trait Similarity | |
public interface TraitSimilarity { | |
Boolean isSimilar(object x); | |
} | |
public static class TraitSimilarityProvide | |
{ | |
public static Boolean isNotSimilar(this TraitSimilarity trait, object x) | |
{ | |
return !trait.isSimilar(x); | |
} | |
} | |
//end trait Similarity | |
//trait Circle | |
public interface TraitCircle | |
{ | |
int Radius(); | |
TraitCircle Rotate(int x); | |
} | |
public static class TraitCircleProvide | |
{ | |
public static int Diameter(this TraitCircle trait) | |
{ | |
return trait.Radius() * 2; | |
} | |
public static TraitCircle intRevRotate(this TraitCircle trait, int x) | |
{ | |
return trait.Rotate(-x); | |
} | |
} | |
//end trait Circle | |
public class Test : TraitSimilarity, TraitCircle | |
{ | |
public Test(int radius) | |
{ | |
_radius = radius; | |
} | |
public int _radius; | |
public bool isSimilar(object x) | |
{ | |
if (x is Test) return true; | |
else return false; | |
} | |
public int Radius() | |
{ | |
return _radius; | |
} | |
public TraitCircle Rotate(int x) | |
{ | |
return new Test(_radius); | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var test = new Test(5); | |
test.isSimilar(test); | |
test.isNotSimilar(test); | |
test.Radius(); | |
test.Rotate(5); | |
test.intRevRotate(5); | |
test.Diameter(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment