我需要一种方法来实现一个可以接受可变数量的泛型参数的类。基本上,我需要一种方法来组合以下类:
class IncidentReporter1<A> {
public void reportIncident(A a) {
}
}
class IncidentReporter2<A, B> {
public void reportIncident(A a, B b) {
}
}
class IncidentReporter3<A, B, C> {
public void reportIncident(A a, B b, C c) {
}
}
class IncidentReporter4<A, B, C, D> {
public void reportIncident(A a, B b, C c, D d) {
}
}
Run Code Online (Sandbox Code Playgroud)
只进一个IncidentReporter班。我知道我可以Class[]在运行时接收并使用它,但我想知道在 java 中是否有更好的本地方法来做到这一点。
我在Windows 10上运行Eclipse Neon,并且可以肯定的是,我始终能够使用两根手指使用笔记本电脑的触摸板向上,向下,向左和向右滚动。更新到Eclipse Neon之后,我无法使用触摸板水平滚动,而不得不手动向左和向右拖动滚动条,这很烦人。对于如何解决这个问题,有任何的建议吗?
谢谢。
我正在制作一个使用管道输入/输出流(周围有一个对象输入/输出流包装器)的java应用程序。我在发送数据时遇到了相当大的延迟,并认为这是因为我最初的数据大小所致。但是,以下演示显示了在不同线程上通信时管道输入/输出流中的本机延迟。
public class Main{
public static long millis = 0;
public static void main(String[] args) throws Exception {
JFrame frame = new JFrame();
JButton button = new JButton("Press me");
frame.getContentPane().add(button);
frame.setSize(new Dimension(500, 500));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
PipedInputStream pis = new PipedInputStream();
PipedOutputStream pos = new PipedOutputStream(pis);
button.addActionListener(e -> {
try {
pos.write((int) (Math.random() * 1000));
//records time the packet was sent
millis = System.currentTimeMillis();
} catch (IOException e1) {
e1.printStackTrace();
}
});
while (true) {
System.out
.println("recieved: " + pis.read() + …Run Code Online (Sandbox Code Playgroud) 我试图覆盖C ++中的虚函数。在我重写该函数之后,它实际上并未覆盖它,因此使该类成为抽象的。下面的代码将使您对问题有很好的了解。
正如您在下面看到的,该代码对于像int这样的非指针模板也能正常工作,但使用int指针失败。
我以为也许是因为引用指针存在问题,所以我在实现Derived2的过程中取出了&,但并没有解决。
template<class T>
class Base {
virtual void doSomething(const T& t) = 0;
};
class Derived1: public Base<int>{
void doSomething(const int& t) {
} // works perfectly
};
class Derived2: public Base<int*>{
void doSomething(const int*& t) {
}
// apparently parent class function doSomething is still unimplemented, making Derived2 abstract???
};
int main(){
Derived1 d1;
Derived2 d2; // does not compile, "variable type 'Derived2' is an abstract class"
}
Run Code Online (Sandbox Code Playgroud) java ×2
c++ ×1
class ×1
eclipse ×1
eclipse-neon ×1
generics ×1
inheritance ×1
inputstream ×1
outputstream ×1
overriding ×1
polymorphism ×1
scroll ×1
templates ×1
touchpad ×1
windows-10 ×1