package
DAP;
class
test5{
public
static
void
main(String[] args){
final
StringBuffer a
=
new
StringBuffer(
"
adf
"
);
final
StringBuffer b
=
new
StringBuffer(
"
efg
"
);
//
System.out.println(a=b);
a.append(
"
def
"
);
System.out.println(a.toString());
//
String c="abc";
//
String d="abc";
String c
=
new
String(
"
abc
"
);
String d
=
new
String(
"
abc
"
);
System.out.println(c
==
d);
//
true,false
}
}
//
引用本身的不变:
final
StringBuffer a
=
new
StringBuffer(
"
immutable
"
);
final
StringBuffer b
=
new
StringBuffer(
"
not immutable
"
);
a
=
b;
//
编译期错误
引用指向的对象不变:
final
StringBuffer a
=
new
StringBuffer(
"
immutable
"
);
a.append(
"
broken!
"
);
//
编译通过
public
class
Singleton {
private
Singleton(){}
//
在自己内部定义自己一个实例,是不是很奇怪?
//
注意这是private 只供内部调用
private
static
Singleton instance
=
new
Singleton();
//
这里提供了一个供外部访问本class的静态方法,可以直接访问
public
static
Singleton getInstance() {
return
instance;
}
}
class
st{
private
int
a
=
3
;
static
private
int
b
=
4
;
//
这个也行
static
public
int
c
=
5
;
public
static
void
main(String [] args){
//
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
package
DAP;
class
test2{
test2(
int
s){a
=
s;};
private
int
a
=
2
;
public
int
geta(){
return
a;}
public
static
void
main(String[] args){
final
int
g
=
7
;
//
if anonyclass can access variable g ,final is necessary!
System.out.println((
new
test2(
3
){
int
toint(){
return
g;}}).toint());
}
}
package
DAP;
import
java.awt.BorderLayout;
import
javax.swing.JFrame;
import
javax.swing.JScrollPane;
import
javax.swing.JTextArea;
import
javax.swing.event.CaretEvent;
import
javax.swing.event.CaretListener;
public
class
MainClass {
public
static
void
main(String args[])
throws
Exception {
JFrame frame
=
new
JFrame(
"
Caret Example
"
);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextArea textArea
=
new
JTextArea();
JScrollPane scrollPane
=
new
JScrollPane(textArea);
frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
CaretListener listener
=
new
CaretListener() {
public
void
caretUpdate(CaretEvent caretEvent) {
System.out.println(
"
dot:
"
+
caretEvent.getDot());
System.out.println(
"
mark
"
+
caretEvent.getMark());
}
};
textArea.addCaretListener(listener);
frame.setSize(
250
,
150
);
frame.setVisible(
true
);
}
}
package
DAP;
abstract
class
content{String content
=
"
C
"
;
abstract
int
value();}
abstract
class
destination{String des
=
"
D
"
;
abstract
int
value();}
class
contents{
private
int
n
=
3
;
A a;B b;
class
A
extends
content{
int
n
=
new
contents().n
*
1
;
int
value(){
return
n;};};
//
outerclass.this
class
B
extends
destination{
int
aa
=
2
;
int
value(){
return
aa;};};
//
inner classes cant have static declarations
//
contents(){a=new A();b=new B();};
//
otherwise inner classes is static
public
content getc(){
return
new
A();};
public
destination getd(){
return
new
B();};
public
content localanonyclass(){
return
new
content(){
int
a
=
9
;
int
value(){
return
a;}};}
//
content is a interface
}
class
test{
public
static
void
main(String [] args){
contents c
=
new
contents();
System.out.println(c.getClass().getName()
==
"
DAP.contents
"
);
//
gute methode fuer ..
content a
=
c.localanonyclass();
destination b
=
c.getd();
//
System.out.println("content=" + a.value() + " destination=" + b.value());
System.out.println(a.value());
}
}