venus najad

venus najad

  • 1.3k
  • 414
  • 28.1k

polymorphism on abstract class

Mar 27 2019 7:46 AM
hello... i have an issue and need help... i will calculate two circuits with two resistors, parallel or serie... for this i have created an abstract class KretsDel, which has two protected fields(resistors), and abstract property, Total and a constructor.
 
further i have created two classes Parallel and Serie, which contains the propertyTotal which returns the result of resistors connections.
 
further i have created a class Krets with a field List<KretsDel> and two methoder for calculation of serie or parallel coupling for each element in List<KretsDel>... but i get errors...
 
the following is my codes:
  1. class Program  
  2. {  
  3. static void Main(string[] args)  
  4. {  
  5. Krets k = new Krets();  
  6. }  
  7. }  
  8. abstract class KretsDel  
  9. {  
  10. protected float R1, R2;  
  11. public abstract float Total  
  12. {  
  13. get;  
  14. set;  
  15. }  
  16. public KretsDel(int r1,int r2)  
  17. {  
  18. this.R1 = r1;  
  19. this.R2 = r2;  
  20. }  
  21. }  
  22. class Parallel : KretsDel  
  23. {  
  24. override public float Total  
  25. {  
  26. set  
  27. {  
  28. this.R1 = value;  
  29. this.R2 = value;  
  30. }  
  31. get { return parallel(this.R1, this.R2); }  
  32. }  
  33. public float parallel(float r1, float r2)  
  34. {  
  35. float R = r1 *r2 / (r1 + r2);  
  36. return R;  
  37. }  
  38. }  
  39. class Serie : KretsDel  
  40. {  
  41. override public float Total  
  42. {  
  43. set  
  44. {  
  45. this.R1 = value;  
  46. this.R2 = value;  
  47. }  
  48. get { return serie(this.R1, this.R2); }  
  49. }  
  50. public float serie(float r1, float r2)  
  51. {  
  52. float R =r1 + r2;  
  53. return R;  
  54. }  
  55. }  
  56. public class Krets  
  57. {  
  58. static Parallel parallel = new Parallel();  
  59. static Serie serie = new Serie();  
  60. List<KretsDel> lkd = new List<KretsDel>() { parallel, serie };  
  61. foreach(var k in lkd)  
  62. k.Total;  
  63. public float ser(float r1, float r2)  
  64. {  
  65. float R = r1 + r2;  
  66. return R;  
  67. }  
  68. public float para(float r1, float r2)  
  69. {  
  70. float R = r1 * r2 / (r1 + r2);  
  71. return R;  
  72. }  
  73. }  
appreciate any help... kind regards

Answers (5)