I'm a little new to this, so sorry if it may seem a naive question. Let's say I have Java class as follows:
public class A {
private final regex = "folder1/folder2/folder3/.*";
private Pattern pattern;
A(...) {
this.pattern = Pattern.Compile(regex)
}
public func1(String str){
if (...){
Matcher m = pattern.matcher(str)
}
}
}
now I'm trying to extend the class A
with scala so I can override the regex
field and therefore the pattern
field following with it.
class B(...) extends A(...){
}
But I'm not sure how to override the regex to be regex = "folder4/.*"
for class B
CodePudding user response:
You could pass it into a protected constructor:
class A {
private final String regex;
protected A(String regex) {
this.regex = regex;
}
public A() {
this("folder1/folder2/folder3/.*");
}
}
class B extends A {
public B() {
super("folder4/.*");
}
}
Or just declare each as a constant and use an overrideable method to read them:
class A {
private static final String REGEX = "folder1/folder2/folder3/.*";
protected String getRegex() {
return REGEX;
}
}
class B {
private static final String REGEX = "folder4/.*";
@Override
protected String getRegex() {
return REGEX;
}
}