Scala constructor, logic and inheritance
i have a question about scala and logic in constructor. Lets say i have a following code:
class A(val x:Int) {...whatever...}
cla开发者_如何转开发ss B(val y:String) extends A(IntValueDerivedFrom_y)
Now, how would i derive some value from y and passed it to constructor of class A? I hope it's understandable what i ask about.
Thanks for the answers!
Not sure I understand. You may do
class B(val y: String) extends A(f(y))
f(y)
stands for any expression where y
appears. For instance, Integer.parseInt(y)
This is close to java code
class B extends A {
public B(String y) {
super(Integer.parseInt(y));
}
}
Is that what you wanted?
精彩评论