Define generic classes

Source: Internet
Author: User

 

... It's inheritance, it's interface implementation, and I'm so tired and sweaty.

Code:
  1. Public abstract class animal
  2. {
  3. Protected string name;
  4. Public string name
  5. {
  6. Get
  7. {
  8. Return name;
  9. }
  10. Set
  11. {
  12. Name = value;
  13. }
  14. }
  15. Public animal ()
  16. {
  17. Name = "the animal with no name ";
  18. }
  19. Public animal (string newname)
  20. {
  21. Name = newname;
  22. }
  23. Public void feed ()
  24. {
  25. Console. writeline ("{0} has been fed.", name );
  26. }
  27. Public abstract void makeanoise ();
  28. }
  29. Public class chicken: Animal
  30. {
  31. Public void layegg ()
  32. {
  33. Console. writeline ("{0} has laid an egg.", name );
  34. }
  35. Public chicken (string newname)
  36. : Base (newname)
  37. {
  38. }
  39. Public override void makeanoise ()
  40. {
  41. Console. writeline ("{0} Says 'cluck! '", Name );
  42. }
  43. }
  44. Public class cow: Animal
  45. {
  46. Public void milk ()
  47. {
  48. Console. writeline ("{0} has been milked.", name );
  49. }
  50. Public cow (string newname)
  51. : Base (newname)
  52. {
  53. }
  54. Public override void makeanoise ()
  55. {
  56. Console. writeline ("{0} syas 'moo'.", name );
  57. }
  58. }
  59. Public class supercow: cow
  60. {
  61. Public void fly ()
  62. {
  63. Console. writeline ("{0} is flying! ", Name );
  64. }
  65. Public supercow (string newname)
  66. : Base (newname)
  67. {
  68. }
  69. Public override void makeanoise ()
  70. {
  71. Console. writeline ("{0} says 'Here I come to save the day! '", Name );
  72. }
  73. }
  74. Public class farm <t>: ienumerable <t>
  75. Where T: Animal
  76. {
  77. Private list <t> animals = new list <t> ();
  78. // Read-only attribute
  79. Public list <t> animals
  80. {
  81. Get {return animals ;}
  82. }
  83. // Implement ienumerable <t>. getenumerator ()
  84. Public ienumerator <t> ienumerable <t>. getenumerator ()
  85. {
  86. Return animals. getenumerator ();
  87. }
  88. // Implement ienumerable. getenumerator ()
  89. Ienumerator ienumerable. getenumerator ()
  90. {
  91. Return animals. getenumerator ();
  92. }
  93. Public void makenoises ()
  94. {
  95. Foreach (T animal in animals)
  96. {
  97. // Due to constraints
  98. Animal. makeanoise ();
  99. }
  100. }
  101. /// <Summary>
  102. /// Extract items that are cow or inherited from cow
  103. /// </Summary>
  104. /// <Returns> </returns>
  105. Public farm <cow> getcows ()
  106. {
  107. Farm <cow> cowfarm = new farm <cow> ();
  108. Foreach (T animal in animals)
  109. {
  110. If (animal is cow)
  111. {
  112. Cowfarm. Animals. Add (animal as cow );
  113. }
  114. }
  115. Return cowfarm;
  116. }
  117. Public void feedtheanimals ()
  118. {
  119. Foreach (T animal in animals)
  120. {
  121. Animal. Feed ();
  122. }
  123. }
  124. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.