您的位置:首页 > 编程语言 > Java开发

Java开发常用知识

2016-12-08 15:19 246 查看

getClass()和super.getClass()

getClass().getName() 和super.getClass().getName()的结果是不是一样呢?答案是一样的。

super并没有代表超类的一个引用的能力,只是代表调用父类的方法而已。所以,在子类的方法中,不能这样用System.out.println(super);也不能使用super.super.method()。

super.getClass()只是表示调用父类的方法而已。getClass方法来自Object类,它返回对象在运行时的类型。

如果想获得父类的Class,请用getClass().getSuperclass().getName()

具体请看源码注释:

/**
* Returns the unique instance of {@link Class} that represents this
* object's class. Note that {@code getClass()} is a special case in that it
* actually returns {@code Class<? extends Foo>} where {@code Foo} is the
* erasure of the type of the expression {@code getClass()} was called upon.
* <p>
* As an example, the following code actually compiles, although one might
* think it shouldn't:
* <p>
* <pre>{@code
*   List<Integer> l = new ArrayList<Integer>();
*   Class<? extends List> c = l.getClass();}</pre>
*
* @return this object's {@code Class} instance.
*/
public final Class<?> getClass() {
return shadow$_klass_;
}


/**
* Returns the {@code Class} object which represents the superclass of the
* class represented by this {@code Class}. If this {@code Class} represents
* the {@code Object} class, a primitive type, an interface or void then the
* method returns {@code null}. If this {@code Class} represents an array
* class then the {@code Object} class is returned.
*/
public Class<? super T> getSuperclass() {
// For interfaces superClass is Object (which agrees with the JNI spec)
// but not with the expected behavior here.
if (isInterface()) {
return null;
} else {
return superClass;
}
}


使用FileChannel copy文件

/**
*
* copy file
*
* @param src
*            source file
* @param dest
*            target file
* @throws IOException
*/
public static void copyFile(File src, File dest) throws IOException {
FileChannel inChannel = null;
FileChannel outChannel = null;
try {
if (!dest.exists()) {
dest.createNewFile();
}
inChannel = new FileInputStream(src).getChannel();
outChannel = new FileOutputStream(dest).getChannel();
inChannel.transferTo(0, inChannel.size(), outChannel);
} finally {
if (inChannel != null) {
inChannel.close();
}
if (outChannel != null) {
outChannel.close();
}
}
}


删除文件

/**
* delete file
*
* @param file
*            file
* @return true if delete success
*/
public static boolean deleteFile(File file) {
if (!file.exists()) {
return true;
}
if (file.isDirectory()) {
File[] files = file.listFiles();
for (File f : files) {
deleteFile(f);
}
}
return file.delete();
}


获取文件MD5值

/**
* get md5
*
* @param file
* @return
*/
public static String getFileMD5(File file) {
if (!file.isFile()) {
return null;
}
MessageDigest digest = null;
FileInputStream in = null;
byte buffer[] = new byte[8192];
int len;
try {
digest = MessageDigest.getInstance("MD5");
in = new FileInputStream(file);
while ((len = in.read(buffer)) != -1) {
digest.update(buffer, 0, len);
}
} catch (Exception e) {
Log.e(TAG, "getFileMD5", e);
return null;
} finally {
try {
if (in != null)
in.close();
} catch (IOException e) {
Log.e(TAG, "getFileMD5", e);
}
}
BigInteger bigInt = new BigInteger(digest.digest());
return bigInt.toString();
}


Java中
<T>
<? super T>
<? extends T>
的区别

<T>
这个很好理解,只能是类型T的对象

<? super T>
指的是 T 是上限,传进来的对象必须是T或者是T的父类

<? extends T>
指的是T是下限,传进来的对象必须是T或者是T的子类

Java 对异常的处理

在Java中除了
RuntimeException
及其任何子类,其他异常类都被 Java 的异常强制处理机制强制异常处理。

关于那些被强制异常处理的代码块,必须进行异常处理,否则编译器会提示“Unhandled exception type Exception”错误警告。

Java中用于处理异常的方式

自行处理:可能引发异常的语句封入在try内,而处理异常的相应语句则封入catch块内

回避异常:在方法声明中包含throws子句,通知潜在调用者,如果发生了异常,必须由调用者处理。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java