hardcore java 学习笔记2——java in review,hardcore学习笔记
hardcore java 学习笔记2——java in review,hardcore学习笔记
Hardcore Java |
hardcore java 是一本非常好的书,在第一章中,作者就整理了很多知识,下面再罗列一下精华:
java中核心概念:java指针,继承,RTTI是最关键的三个概念。
1. 指针:很多说法认为java没有指针,java更喜欢叫它“引用”,引用就是对象句柄了。但是,这里作者指出,java的“引用”符合计算机科学中对指针的概念,和传统的C/C++相比,java指针没有比较的功能而已。而指针并不一定要有“比较”才能满足定义。
“java has pointers , it just doesn`t have pointer arithmetic . ”
1: public static void someMethod(Vector source) {
2: Vector target = source;
3: target.add("Swing");
4: }
2.继承 用一句话来说就是 everything is class, Object is god .
public static final void main(final String[] args) {
System.out.println(SomeClass.class + " --|> " + SomeClass.class.getSuperclass());
System.out.println(SomeOtherClass.class + " --|> "
+ SomeOtherClass.class.getSuperclass());
}
3.RTTI
A a = null;
A a1 = new A();
B b = new B();
C c = new C();
a = (A)b; // no problem
b = (B)a; // still no problem, casting back to what it was created as.
a = a1; // Same type so no problem
Object d = (Object)c; // no problem becasue of implicit inheritance
c = (C)d; // casting back
This code shows how to create an object of a subclass, cast it to its base class, and then cast back to the subclass .RTTI works in the background to ensure that your casting is always legal; in other words, an object can safely be cast only to its own type, or to a type that it is inherited from .
以下是一些编程的小技巧吧,好的习惯来自细节。
4. 减少if的使用
if (inner != null) {
if (inner.contains(target)) {
// do code.
}
变成
if ((inner != null) && (inner.contains(target))) {
// do code.
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public static int someMethod(final Point p) {
if (p == null) {
return 0;
} else {
return p.x + p.y;
}
}
变成
public static int someElegantMethod(final Point p) {
return (p == null) ? 0 : (p.x + p.y);
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
5. LOOP
break,continue, label
for (int idx = 0; idx < 1000; idx++) {
// ... do some complex code.
if (idx == 555) {
break;
}
// ... processing code
}
break 会跳出当前的for循环
for (int idx = 0; idx < 1000; idx++) {
// ... do some complex code.
if (idx == 555) {
continue;
}
// ... processing code
}
continue 会继续继续for循环,但是,某些情况会被忽略掉。
Label的作用
LINE:
for (int x = 0; x < values[0].length; x++) {
COLUMN:
for (int y = 0; y < values.length; y++) {
if ((values[x][y].x < 0) || (values[x][y].y < 0)) {
continue LINE; // skip the rest of the line;
}
// do something with the value
}
}
6.collection iterator
int[] results = new int[points.size()];
Point element = null;
Iterator iter = points.iterator();
for (int idx = 0; iter.hasNext(); idx++) {
element = (Point)iter.next();
results[idx] = element.x;
}
改成
int[] results = new int[points.size()];
Point element = null;
int idx = 0;
Iterator iter = points.iterator();
while (iter.hasNext()) {
element = (Point)iter.next();
results[idx] = element.x;
idx++;
}
return results;
7.assert 用于调试的利器
for (String key = null; iter.hasNext(); key = (String)iter.next()) {
assert (key != null);
System.out.println(key + "=" + System.getProperty(key));
}
keep in mind that you can use any expression that evaluates to anything other than void as the second expression.
//this will not work
assert(args != null) :String s = "args is not null .";
to assert or not to assert
public class UnchainedConstructors extends JButton {
/**
* Constructor for only the panel name.
*
* @param text Display text for the button.
*/
public UnchainedConstructors(final String text) {
setText(text);
String tooltip = new String("A button to show " + text);
setToolTipText(tooltip);
}
/**
* Constructor for only the tool tip and panel name.
*
* @param text Display text for the button.
* @param tooltip Tool Tip for the panel.
*/
public UnchainedConstructors(final String text, final String tooltip) {
setText(text);
setToolTipText(tooltip);
}
/**
* Constructor for all parameters.
*
* @param text Display text for the button.
* @param tooltip Tool Tip for the panel.
* @param listener Listener to the panel.
*/
public UnchainedConstructors(final String text, final String tooltip,
final ActionListener listener) {
setText(text);
setToolTipText(tooltip);
addActionListener(listener);
}
}你可能在觉得这个代码没有什么问题。条件是假设我们会触发一个按钮,这个按钮会调用三种方法之一,现在假如新添加一个按钮就会变得非常的麻烦,要在三个方法中都修改。所以,要使用chained constructors :
public ChainedConstructors(final String text) {
this(text, new String("A button to show " + text), null);
}
/**
* A chained constructor showing usage of an instance method.
*
* @param color The default color to show.
*/
public ChainedConstructors(final Color color) {
this(color.toString(), "", null);
}
/**
* A chained constructor showing usage of a static method.
*
* @param text Display text for the button.
* @param showDate Whether to show the creation date in the button's tooltip.
*/
public ChainedConstructors(final String text, final boolean showDate) {
this(text, buildDateToolTip(text, showDate), null);
}
相关文章
- 暂无相关文章
用户点评