我有一个抽象类:
public abstract class RootProcessor<T> {
Class<T> clazz;
}
Run Code Online (Sandbox Code Playgroud)
我需要填写ClassT clazz;孩子RootProcessor- 每个孩子都有自己的孩子T
我发现只有一个解决方案,但它需要编译器参数 -Xlint:unchecked
public RootProcessor(){
this.clazz = (Class<T>) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}
Run Code Online (Sandbox Code Playgroud)
这是最好的解决方案吗?没有我们可以这样做-Xlint:unchecked吗?
我试图理解Perl的背景,我绊倒了一块石头;
鉴于代码;
#!/usr/bin/perl
my $b = (33,22,11);
print "$b\n";
my $b = () = (33,22,11);
print "$b\n";
my @b = (33,22,11);
print "@b\n";
my @b = () = (33,22,11);
print "@b\n";
Run Code Online (Sandbox Code Playgroud)
结果是(最后一行是空白的);
11
3
33 22 11
<>
Run Code Online (Sandbox Code Playgroud)
由于第二个打印返回列表的长度,我假设某个地方生成了一个数组,因为标量上下文中的数组计算它的长度.但第4版似乎相信这一假设.我本来期待看到'33 22 11'印刷但没有得到任何东西.这里发生了什么事?
我正在阅读Java Generics常见问题解答,并遇到&hellip各种情况:
public interface Action<E extends Exception> {
void run() throws E ;
}
public final class Executor {
public static <E extends Exception>
void execute(Action<E> action) throws E {
…
action.run();
…
}
}
public final class Test {
private static class TestAction implements Action<java.io.FileNotFoundException> {
public void run() throws java.io.FileNotFoundException {
…
throw new java.io.FileNotFoundException() ;
…
}
public static void main(String[] args) {
try {
Executor.execute(new TestAction()) ;
} catch (java.io.FileNotFoundException f) { …Run Code Online (Sandbox Code Playgroud) 当我做
ArrayList<Integer> arr = new ArrayList<Integer>(10);
arr.set(0, 1);
Run Code Online (Sandbox Code Playgroud)
Java给了我
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.set(Unknown Source)
at HelloWorld.main(HelloWorld.java:13)
Run Code Online (Sandbox Code Playgroud)
有没有一种简单的方法可以预先保留ArrayList的大小,然后立即使用索引,就像数组一样?
即,在
class A {
public String s;
}
Run Code Online (Sandbox Code Playgroud)
和
A a1 = new A();
a1.s = "bla";
A a2 = new A();
a2.s = a1.s;
a1 = null;
Run Code Online (Sandbox Code Playgroud)
将a1被垃圾收集或是a1.s允许收集它的参考(我宁愿做一个深层复制a2.s = new String(a1.s))?
非常感谢提前!
我在另一篇文章的答案中看到了这段代码:为什么我会使用Perl匿名子程序而不是命名的子程序?,但无法确切地知道发生了什么,所以我想自己运行它.
sub outer
{
my $a = 123;
sub inner
{
print $a, "\n"; #line 15 (for your reference, all other comments are the OP's)
}
# At this point, $a is 123, so this call should always print 123, right?
inner();
$a = 456;
}
outer(); # prints 123
outer(); # prints 456! Surprise!
Run Code Online (Sandbox Code Playgroud)
在上面的例子中,我收到一个警告:"变量$ a不会在第15行保持共享.显然,这就是输出"意外"的原因,但我仍然不明白这里发生了什么.
sub outer2
{
my $a = 123;
my $inner = sub
{
print $a, "\n";
};
# At this point, $a …Run Code Online (Sandbox Code Playgroud) 所以,我正在尝试使用的非常简单的代码在这里:http://wiki.python.org/moin/UdpCommunication
(也在这里):发送:
import socket
UDP_IP = "127.0.0.1"
UDP_PORT = 5005
MESSAGE = "Hello, World!"
print "UDP target IP:", UDP_IP
print "UDP target port:", UDP_PORT
print "message:", MESSAGE
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))
Run Code Online (Sandbox Code Playgroud)
接收:
import socket
UDP_IP = "127.0.0.1"
UDP_PORT = 5005
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
while True:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
print "received message:", data
Run Code Online (Sandbox Code Playgroud)
当我在计算机上运行这两个应用程序时,代码工作正常.我把发送代码放在我的笔记本电脑上:
UDP_IP="IP address for …Run Code Online (Sandbox Code Playgroud) 我刚刚完成Project Euler问题9(警告剧透):
A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
a^2 + b^2 = c^2
For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2.
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.
Run Code Online (Sandbox Code Playgroud)
这是我的解决方案:
public static int specPyth(int num)
{
for (int a = 1; a < num; a++)
for (int b = 2; b …Run Code Online (Sandbox Code Playgroud) 我有一个庞大的字符串(一个SQL查询..),我在notepad2中写了.我正在使用Java,我只是想知道是否有一种快速简便的方法来添加到每行的末尾?例如:
String query = ""
+ " select column1
+ " , column2
+ " , column3
+ " from table
);
//want to add \n" at the end of each line
Run Code Online (Sandbox Code Playgroud) 我明白,如果代码的结构使得它被编程为接口,那么模拟它是微不足道的; 但是,我正在使用一个我无法改变的代码库(这不是我的代码库),事实并非如此.
这个代码库是高度互联的,没有任何东西被编程到接口,只有结构,所以没有依赖注入.
结构本身只包含其他结构,所以我也不能这样做.我不相信我可以对方法做任何事情,并且存在的少数函数不是变量,所以我不知道将它们交换掉.继承在golang中不是一件事,所以这也是不行的.
在像python这样的脚本语言中,我们可以在运行时修改对象,也就是猴子补丁.在golang中我能做些什么吗?试图找出一些方法来测试/基准测试而不触及底层代码.