Advertisement
valtih1978

Factoring constructor into the superclass

Jun 28th, 2014
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.51 KB | None | 0 0
  1.   abstract class AlgorithmState[ResultType](sum: Int) {
  2.     def produce(option: Int)
  3.   }
  4.  
  5.   class IntAlg(sum: Int) extends AlgorithmState[Int] (sum) {
  6.     def produce(option: Int) = new IntAlg(sum - option)
  7.    }
  8.  
  9.   class StringAlg(sum: Int) extends AlgorithmState[Int] (sum) {
  10.     def produce(option: Int) = new StringAlg(sum - option)
  11.    }
  12.  
  13.   // the produce method is identical for Str and Int algorithms. Can I move it into the abstract parent class?
  14.   // How do I instantiate appropriate child objects?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement