Advertisement
Hiteshw11

Sum Of Integers Using list In Generic Class

Nov 26th, 2024
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 0.67 KB | Source Code | 0 0
  1.  
  2. /* Create a generic base class called `Storage<T>` with a method `store` that takes a parameter of type `T`. Then, create a derived class called `IntegerStorage` that restricts `T` to integers and adds a method `sum` that returns the sum of all stored integers. */
  3.  
  4.  
  5.  
  6. void main() {
  7.   var a= store(3);
  8.  
  9.   print(a.storage());
  10.   var b = IntegerStorage();
  11.   b.store(5);
  12.  print( b.store(5));
  13. }
  14.  
  15.  
  16. class store<T>
  17. {
  18. T data1;
  19.  store(this.data1);
  20.  
  21. storage()
  22. {
  23. return "$data1";
  24. }
  25.  
  26.  
  27. }
  28.  
  29.  
  30. class IntegerStorage extends store
  31. {
  32. List<int> abc=[];
  33. int sum =0;
  34.  
  35. IntegerStorage():super(0);
  36.  
  37. store(int data2)
  38. {
  39. abc.add(data2);
  40. sum=sum+data2;
  41. return sum;
  42. }
  43.  
  44. }
Tags: dart
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement