小编Rae*_*ald的帖子

从匿名类访问父类时解决歧义

我最近碰到了这样的事情......

public final class Foo<T>
implements Iterable<T> {

    //...

    public void remove(T t) { /* banana banana banana */ }

    //...

    public Iterator<T> Iterator {
        return new Iterator<T>() {

            //...

            @Override
            public void remove(T t) {
                // here, 'this' references our anonymous class...
                // 'remove' references this method...
                // so how can we access Foo's remove method?           
            }

            //...

        };
    }
}
Run Code Online (Sandbox Code Playgroud)

有什么方法可以做我正在尝试的同时保持这个匿名课程?或者我们必须使用内部类或其他东西?

java anonymous-class

5
推荐指数
1
解决办法
374
查看次数

如果在读取时存在IOException,如何返回500状态代码

说我有以下代码段

public boolean checkListing() {

    try {

        // open the users.txt file
        BufferedReader br = new BufferedReader(new FileReader("users.txt"));

        String line = null;
        while ((line = br.readLine()) != null) {
            String[] values = line.split(" ");
            // only interested for duplicate usernames
            if (username.equals(values[0])) {
                return true;
            }
        }
        br.close();
        return false;
    } catch (IOException e) {
        e.printStackTrace();
        // what to do here
    }

}
Run Code Online (Sandbox Code Playgroud)

如果发生异常,我该如何处理错误?我想知道它发生了,并将500代码返回给用户.

我应该抛出异常并在其他类中捕获它吗?

是否有更优雅的方式来获得反馈?

java exception-handling

5
推荐指数
1
解决办法
873
查看次数

如何以每秒几个固定的速率准确地安排任务

我目前正在使用

 ScheduledFuture<?> schedulerFuture = scheduler.scheduleAtFixedRate(taskMaker, 0 , period, TimeUnit.MICROSECONDS);                
 ScheduledFuture<?> timerFuture = timer.schedule(timeAnalyser, time, TimeUnit.MICROSECONDS);
Run Code Online (Sandbox Code Playgroud)

每秒执行许多任务,在几个线程中也是异步的.问题是,我得到了不准确的信息.例如,当我想在一秒内在第一个线程中执行3个任务,在第二个线程中执行7个任务,在第三个线程中执行5个,有时我会得到4,7,6或3,6,6.这不是处理器在使用更大的数字时无法做到,但我想要每秒执行的任务越多,不准确性就越大.也许是因为timer(停止taskMaker)是在与...完全相同的时间开始的taskMaker

任何改变建议?无论如何,使用SchedulesExecutorService如此高频率执行任务是正确的方法吗?

java multithreading

5
推荐指数
1
解决办法
2333
查看次数

为什么在构造函数之前执行实例初始化块

我知道静态块是在加载类时初始化的,因为类只在程序中加载一次,所以只初始化一次.

每次创建类的实例时都会初始化IIB(实例初始化块),而构造函数则相同:它们在对象创建期间执行.

我不明白为什么在下面的程序IIB中在构造函数之前执行.码-

public class Hello {

    public static void main(String args[]) {
        C obj = new C();
    }
}

class A {

    static {
        System.out.println("Inside static block of A");
    }

    {
        System.out.println("Inside IIB of A");
    }

    A() {
        System.out.println("Inside NO-ARGS constructor of A");
    }
}

class B extends A {

    static {
        System.out.println("Inside static block of B");
    }

    {
        System.out.println("Inside IIB of B");
    }

    B() {
        System.out.println("Inside NO-ARGS constructor of B");
    }
}
Run Code Online (Sandbox Code Playgroud)
class C extends B {

    static …
Run Code Online (Sandbox Code Playgroud)

java constructor initialization

5
推荐指数
1
解决办法
661
查看次数

如何为不实现Iterable的类使用for-each循环

我正在阅读完整参考文献中的集合,然后我遇到了这个陈述

收集界面

Collection接口是构建Collections Framework的基础,因为它必须由定义集合的任何类实现.Collection是一个具有此声明的通用接口:interface Collection<E>.这里,E指定集合将容纳的对象的类型.Collection扩展了Iterable接口.这意味着所有集合都可以通过使用for-each样式for循环来循环.(回想一下,只有实现Iterable的类可以通过for循环).

在最后两行中,写入只有那些实现Iterable接口的类才能通过for循环循环.但是,我猜对象类没有实现可迭代接口,那么我们如何能够在字符串,整数等情况下使用for-each循环.

java collections foreach iterable interface

5
推荐指数
1
解决办法
127
查看次数

为什么私有基类构造函数导致"隐式超级构造函数不可见"

如果构造函数不在Java中继承,为什么我会得到编译错误(隐式超级构造函数A()对于默认构造函数是不可见的.必须定义一个显式构造函数)?

class A {
    private A() {
    }
}

public class B extends A {

}
Run Code Online (Sandbox Code Playgroud)

UPD.我知道super()在隐式B构造函数中调用它.但我不明白为什么它无法访问私有构造函数super().那么,如果我们只有私有构造函数,那么事实上是final什么?

java inheritance constructor private

5
推荐指数
1
解决办法
1112
查看次数

版本1.0.14之后的cucumber-java和cucumber-junit不起作用

我一起使用Cucumber-JVM和Selenium WebDriver.我在eclipse中有一个Maven项目,pom.xml文件的依赖关系如下:

<dependency>
    <groupId>info.cukes</groupId>
    <artifactId>cucumber-java</artifactId>
    <version>1.2.2</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>info.cukes</groupId>
    <artifactId>cucumber-junit</artifactId>
    <version>1.2.2</version>
    <scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)

RunCukesTest.java文件的内容是:

import org.junit.runner.RunWith;
import cucumber.junit.Cucumber;
@RunWith(Cucumber.class)
@Cucumber.Options(format = {"pretty", "html:target/cucumber-htmlreport","json-pretty:target/cucumber-report.json"})
public class RunCukesTest {
}
Run Code Online (Sandbox Code Playgroud)

我在以下代码行中收到错误:

import cucumber.junit.Cucumber;
@RunWith(Cucumber.class)
@Cucumber.Options(format = {"pretty", "html:target/cucumber-htmlreport","json-pretty:target/cucumber-report.json"})
Run Code Online (Sandbox Code Playgroud)

但是,当我使用1.0.14版时,它运行良好.最新版本有什么问题?

eclipse cucumber cucumber-jvm cucumber-junit selenium-webdriver

5
推荐指数
2
解决办法
3万
查看次数

Cassandra (CQL) RoleManager 禁用

我目前对 Cassandra 使用以下配置:

 authenticator: PasswordAuthenticator
 authorizer: org.apache.cassandra.auth.CassandraAuthorizer
Run Code Online (Sandbox Code Playgroud)

我的问题是:如何禁用角色管理以使用正常的 CQL system.permissions

整体禁用角色的原因是因为在 roleManagement 处于活动状态时我无法使用创建新用户:

 user@cqlsh> create user testuser with password '123';
 InvalidRequest: code=2200 [Invalid query] message="org.apache.cassandra.auth.CassandraRoleManager doesn't support PASSWORD"
Run Code Online (Sandbox Code Playgroud)

roles cassandra

5
推荐指数
1
解决办法
8736
查看次数

为什么在compareTo(Object)中有一个强制转换

public int compareTo(Object x) {
    Task other = (Task)x;

    if (other.priority == this.priority) {
        System.out.println("Each task has the same priority level.");
        return 0;
    } else if (other.priority > this.priority) {
        System.out.println(other.priority + "\n" + this.priority);
        return -1;
    } else {
        System.out.println(this.priority + "\n" + other.priority);
        return 1;
    }
}
Run Code Online (Sandbox Code Playgroud)

这就是我为类编程分配的代码.我不确定我为什么要使用Task other = (Task)x;它或者它在做什么.其余的我明白了.如果有人能快速解释这里的实际情况,我会很高兴.谢谢!

java comparable

5
推荐指数
1
解决办法
1087
查看次数

使用正则表达式提取括号中的数字

目前,我正在努力解决使用Java捕获REGEX数字的问题

我试图使用REGEX捕获此字符串值中的数字的字符串是这个..

[TEST][64894]HelloWorld[KGMObilians]
Run Code Online (Sandbox Code Playgroud)

我希望能够找到位于[]中的数字.

这可能用Java吗?我可以通过拆分字符串轻松获取数字,但该公司希望我使用正则表达式,因为它更安全,更快.请帮我.

java regex

5
推荐指数
1
解决办法
111
查看次数