Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 重写方法以将不同的分隔符集传递给扫描仪_Java_Java.util.scanner - Fatal编程技术网

Java 重写方法以将不同的分隔符集传递给扫描仪

Java 重写方法以将不同的分隔符集传递给扫描仪,java,java.util.scanner,Java,Java.util.scanner,关于超车的几个问题 我正在从另一个类插入一个方法openRead,该方法将被重写,以便scanner类使用所提供的分隔符模式,该模式被引用。我需要使用scanner类useDelmiter方法 来自另一个类的方法 public boolean openRead() throws FileNotFoundException { sc = new Scanner(new File(fileName)); if (fileName != null) { re

关于超车的几个问题

我正在从另一个类插入一个方法
openRead
,该方法将被重写,以便scanner类使用所提供的分隔符模式,该模式被引用。我需要使用scanner类useDelmiter方法

来自另一个类的方法

public boolean openRead() throws FileNotFoundException
 {
   sc = new Scanner(new File(fileName));

    if (fileName != null)
    {
         return true;
    }
    else
    {
        return false;
    }

}
分隔符

protected final String DELIMITERS = "[\\s[^'a-zA-Z]]";

我不知道如何使用常量分隔符来克服这个问题。

所以您只想覆盖的版本对
扫描仪使用不同的分隔符模式?我建议看一下,因为它记录了如何使用不同的分隔符模式

public boolean openRead() throws FileNotFoundException
{
    boolean result = super.openRead();
    sc.useDelimiter(DELIMITERS);
    return result;
}
编辑

或者,您可能只是不知道Java中的重写是什么,为此,您应该在中阅读更多内容

但基本上,如果你有一些课程:

public class ScannerUserParent
{
    protected Scanner sc;
    private String filename = null;

    // all that other stuff like constructors...

    public boolean openRead() throws FileNotFoundException
    {
        sc = new Scanner(new File(fileName));

        if (fileName != null)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}
然后您将该类划分为子类(或
扩展
):

然而,还有其他一些事情可能会阻止你这样做。例如,如果
sc
成员是
private
作用域,则子类不能以我所示的方式直接使用它

在我的示例中,
sc
使用
protected
访问,因此类及其子类可以使用它

如果有一个
private
Scanner
,并且假设父级有一个
getScanner()
方法返回
Scanner
,则可以执行以下操作:

    public boolean openRead() throws FileNotFoundException
    {
        boolean result = super.openRead(); // we are calling the parent's openRead() method to set up the Scanner sc object
        Scanner sc = getScanner();
        sc.useDelimiter(DELIMITERS);
        return result;
    }

三个月和四个之前的问题,恕我直言,你现在真的,真的应该正确地格式化代码,而不是让其他人帮你清理。在“提问”框的右侧有一个标记为“如何格式化”的链接。值得一读的是,它在一个简单易读的列表中准确地告诉你该做什么。扩展版本在这里:您的标题和问题似乎不匹配,或者我可能只是误解了您的提问。sc成员是私人成员,因此我理解我需要使用get方法?@user445714-是,如果它是
private
,那么您可以选择使用getter方法来访问该对象。是否可以在布尔结果和sc.usedimiter之间使用上述openRead方法和getter?@user445714-检查我答复底部的最新代码段。我的理解是,如果父端没有getter,那么如果它是私有的,子端将无法看到它?孩子会继承它,但看不见?
    public boolean openRead() throws FileNotFoundException
    {
        boolean result = super.openRead(); // we are calling the parent's openRead() method to set up the Scanner sc object
        Scanner sc = getScanner();
        sc.useDelimiter(DELIMITERS);
        return result;
    }