我正在尝试测试以下情况:
city.Main)package castle)castle.Guard)和一个包私有类(castle.Princess)。这是我的代码:
Main.java
package city;
import castle.Guard;
public class Main {
public static void main(String[] args) {
Princess princess = Guard.getPrincessStatic();
// Error: Princess cannot be resolved to a type
Guard.getPrincessStatic().sayHi();
// Error: The type Princess is not visible
Guard guard = new Guard();
guard.getPrincess().sayHi();
// Error: The type Princess is not visible
guard.getPrincessMember().sayHi();
// Error: The type Princess is not visible
}
}
Run Code Online (Sandbox Code Playgroud)
Guard.java …
我想使用Guava的TypeToken,然后我从这里下载了Guava Release 14.0 .(我假设14.0比15.0 rc更稳定.)
guava-14.0.1-sources.jar (Source)
http://search.maven.org/remotecontent?filepath=com/google/guava/guava/14.0.1/guava-14.0.1-sources.jar
Run Code Online (Sandbox Code Playgroud)
将下载的内容解压缩guava-14.0.1-sources.jar到一个文件夹中.
然后我在Eclipse中创建了一个Java 1.7项目.
并将Eclipse中的源链接起来Java Build Path -> Source -> Link Source....
注意我没有使用(并且不知道如何使用)任何构建系统,如Maven.
首先我遇到了问题The import javax.annotation.Nullable cannot be resolved.但这已经解决了.我按照这里的主题从这个页面下载jsr305-2.0.1.jar.(我假设2.0.1是最新版本.)
其次我得到了rt.jar access restriction类似的问题这一个.这也可以通过简单地重新添加JRE系统库来解决,就像那里的答案一样.
现在我遇到了这个我无法解决的问题,并且没有来自网络的解决方案.
在包com.google.common.util.concurrent类中ServiceManager,存在一些编译错误.
import javax.inject.Inject; // The import javax.inject cannot be resolved
import javax.inject.Singleton; // The import javax.inject cannot be resolved …Run Code Online (Sandbox Code Playgroud) 考虑班级:
class OnlyIntegerTypeAllowed<T> {
OnlyIntegerTypeAllowed(Class<T> clazz) {
System.out.println(clazz);
if (clazz != Integer.class)
throw new RuntimeException();
}
}
Run Code Online (Sandbox Code Playgroud)
它被设计为只接受Type Arguments Integer.我们if-throw在其构造函数中添加了一个检查.这是检查类型参数的一种非常常见的方法.
但是,可以通过以下方式绕过(黑客入侵,欺骗)此检查:
OnlyIntegerTypeAllowed<Integer> normalWay =
new OnlyIntegerTypeAllowed<Integer>(Integer.class);
OnlyIntegerTypeAllowed<String> hacking =
new OnlyIntegerTypeAllowed<String>((Class<String>) Class.forName(Integer.class.getName()));
Run Code Online (Sandbox Code Playgroud)
无论上述两行有没有编译错误和没有抛出异常!
OMG - 强制执行Type Argument的更好方法吗?
这是android.util.Log源代码.
在最底层(第340行),方法中包括:
public static native int println_native(int bufID,
int priority, String tag, String msg);
Run Code Online (Sandbox Code Playgroud)
我想println_native()或多或少喜欢它println(),只是有int bufID不同的.
但即使我得到了代码println_native(),我仍然缺乏com.android.internal.os.RuntimeInit(第19行import),以模拟android.util.Log旧的Android版本.
这在某种程度上与询问相同:是否存在子类/超类基元类型的任何内容?
(因为((Object) (new String[6])) instanceof Object[]是真的,因为String extends Object.)
例如,的int[],是它唯一的 ((Object) (new int[3])) instanceof int[]是真实的,在Java中的东西?
如果是这样,
((Object) (new int[3])) instanceof int[]
Run Code Online (Sandbox Code Playgroud)
是完全相同的
((Object) (new int[3])).getClass() == int[].class
Run Code Online (Sandbox Code Playgroud)
而我更喜欢后者,因为它应该更快,因为它不必检查每个类型/类继承.
您的位置是:http://developer.android.com/reference/java/io/PrintStream.html#print%28float%29
只有一个功能可以满足所有目的:
public void print (Object o) {
if (o == null) {
// print "null"
} else {
// print o.toString();
}
}
Run Code Online (Sandbox Code Playgroud)
更详细的说明.例如,internal_print(String str)是一个写入打印流的函数.那么唯一需要的功能是:
public void print (Object o) {
if (o == null) {
internal_print( "null" );
} else {
internal_print( o.toString() );
}
}
Run Code Online (Sandbox Code Playgroud)
对于其他float,int,char,long,等超载,我能想象他们只是喜欢:
public void print (float o) {
if (o == null) {
internal_print( "null" );
} else {
internal_print( …Run Code Online (Sandbox Code Playgroud)