code_junkie

Why can't I declare C# methods virtual and static?

Nov 12th, 2011
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. type
  2. TForm1 = class(TForm)
  3. procedure FormShow(Sender: TObject);
  4. end;
  5.  
  6. TTestClass = class
  7. public
  8. class procedure TestMethod(); virtual;
  9. end;
  10.  
  11. TTestDerivedClass = class(TTestClass)
  12. public
  13. class procedure TestMethod(); override;
  14. end;
  15.  
  16. TTestMetaClass = class of TTestClass;
  17.  
  18. var
  19. Form1: TForm1;
  20.  
  21. implementation
  22.  
  23. {$R *.dfm}
  24.  
  25. class procedure TTestClass.TestMethod();
  26. begin
  27. Application.MessageBox('base', 'Message');
  28. end;
  29.  
  30. class procedure TTestDerivedClass.TestMethod();
  31. begin
  32. Application.MessageBox('descendant', 'Message');
  33. end;
  34.  
  35.  
  36. procedure TForm1.FormShow(Sender: TObject);
  37. var
  38. sample: TTestMetaClass;
  39. begin
  40. sample := TTestClass;
  41. sample.TestMethod;
  42. sample := TTestDerivedClass;
  43. sample.TestMethod;
  44. end;
  45.  
  46. class Root {
  47. public static virtual string TestMethod() {return "Root"; }
  48. }
  49. TRootClass = class of TRoot; // Here is the typed type declaration
  50.  
  51. class Derived : Root {
  52. public static overide string TestMethod(){ return "derived"; }
  53. }
  54.  
  55. class Test {
  56. public static string Run(){
  57. TRootClass rc;
  58. rc = Root;
  59. Test(rc);
  60. rc = Derived();
  61. Test(rc);
  62. }
  63. public static Test(TRootClass AClass){
  64. string str = AClass.TestMethod();
  65. Console.WriteLine(str);
  66. }
  67. }
  68.  
  69. class Car
  70. {
  71. public static int TyreCount = 4;
  72. public virtual int GetTyreCount() { return TyreCount; }
  73. }
  74. class Tricar : Car
  75. {
  76. public static new int TyreCount = 3;
  77. public override int GetTyreCount() { return TyreCount; }
  78. }
  79.  
  80. ...
  81.  
  82. Car[] cc = new Car[] { new Tricar(), new Car() };
  83. int t0 = cc[0].GetTyreCount(); // t0 == 3
  84. int t1 = cc[1].GetTyreCount(); // t1 == 4
  85.  
  86. public abstract class HouseDeed : Item
  87. {
  88. public static int m_price = 0;
  89. public abstract int Price { get; }
  90. /* more impl here */
  91. }
  92.  
  93. public class FieldStoneHouseDeed : HouseDeed
  94. {
  95. public static new int m_price = 43800;
  96. public override int Price { get { return m_price; } }
  97. /* more impl here */
  98. }
  99.  
  100. public class SmallTowerDeed : HouseDeed
  101. {
  102. public static new int m_price = 88500;
  103. public override int Price { get { return m_price; } }
  104. /* more impl here */
  105. }
  106.  
  107. public interface ISumable<T>
  108. {
  109. static T Add(T left, T right);
  110. }
  111.  
  112. public T Aggregate<T>(T left, T right) where T : ISumable<T>
  113. {
  114. return T.Add(left, right);
  115. }
  116.  
  117. public static class Static<T> where T : new()
  118. {
  119. public static Value = new T();
  120. }
  121.  
  122. public interface ISumable<T>
  123. {
  124. T Add(T left, T right);
  125. }
  126.  
  127. public T Aggregate<T>(T left, T right) where T : ISumable<T>, new()
  128. {
  129. return Static<T>.Value.Add(left, right);
  130. }
Add Comment
Please, Sign In to add comment