java初始化有什么特点?怎么初始化?初始化的顺序是什么?
实例如下:Test0510.java
class Init {
boolean b;
char c;
short s;
int i;
long l;
float f;
double d;
String str;
Init init;
void f ( ) {
System. out. println ( "boolean b=" + b) ;
System. out. println ( "char c=\"" + c+ "\"" ) ;
System. out. println ( "short s=" + s) ;
System. out. println ( "int i=" + i) ;
System. out. println ( "long l=" + l) ;
System. out. println ( "float f=" + f) ;
System. out. println ( "double d=" + d) ;
System. out. println ( "String str=" + str) ;
System. out. println ( "Init init=" + init) ;
}
}
public class Test0510 {
public static void main ( String[ ] args) {
Init i= new Init ( ) ;
i. f ( ) ;
}
}
public class Test0510 {
int i;
Test0510 ( ) {
System. out. println ( "i= " + i) ;
i= 10 ;
System. out. println ( "i= " + i) ;
}
Test0510 ( int i) {
System. out. println ( "i= " + this . i) ;
this . i= i;
System. out. println ( "i= " + i) ;
}
public static void main ( String[ ] args) {
new Test0510 ( ) ;
new Test0510 ( ) ;
new Test0510 ( 100 ) ;
}
}
class Init {
int i= 10 ;
int j= f ( ) ;
int z= g ( j) ;
int f ( ) { return i; }
int g ( int i) { return i* 10 ; }
}
public class Test0510 {
public static void main ( String[ ] args) {
Init init= new Init ( ) ;
}
}
class Window {
Window ( int i) { System. out. format ( "Window(%d)\n" , i) ; }
}
class House {
Window w1= new Window ( 1 ) ;
House ( ) {
System. out. println ( "House()" ) ;
w3= new Window ( 4 ) ;
}
Window w2= new Window ( 2 ) ;
void f ( ) { System. out. println ( "f()" ) ; }
Window w3= new Window ( 3 ) ;
}
public class Test0510 {
public static void main ( String[ ] args) {
House h= new House ( ) ;
h. f ( ) ;
}
}
class Bowl {
Bowl ( int i) { System. out. format ( "Bowl(%d)\n" , i) ; }
void f1 ( int i) { System. out. format ( "f1(%d)\n" , i) ; }
}
class Table {
static Bowl b1= new Bowl ( 1 ) ;
Table ( ) {
System. out. println ( "Table()" ) ;
b2. f1 ( 3 ) ;
}
void f2 ( int i) { System. out. format ( "f2(%d)\n" , i) ; }
static Bowl b2= new Bowl ( 2 ) ;
}
class Cupboard {
Bowl b3= new Bowl ( 6 ) ;
static Bowl b4= new Bowl ( 4 ) ;
Cupboard ( ) {
System. out. println ( "Cupboard()" ) ;
b4. f1 ( 7 ) ;
}
void f3 ( int i) { System. out. format ( "f3(%d)\n" , i) ; }
static Bowl b5= new Bowl ( 5 ) ;
}
public class Test0510 {
public static void main ( String[ ] args) {
System. out. println ( "Creating new Cupboard() in main" ) ;
new Cupboard ( ) ;
table. f2 ( 8 ) ;
cupboard. f3 ( 9 ) ;
}
static Table table= new Table ( ) ;
static Cupboard cupboard= new Cupboard ( ) ;
}
class Mug {
Mug ( int i) { System. out. format ( "Mug(%d)\n" , i) ; }
void f ( int i) { System. out. format ( "f(%d)\n" , i) ; }
}
class Mugs {
Mug m1, m2;
static Mug m3, m4;
{
m1= new Mug ( 3 ) ;
m2= new Mug ( 4 ) ;
System. out. println ( "m3 & m4" ) ;
}
static {
m3= new Mug ( 1 ) ;
m4= new Mug ( 2 ) ;
System. out. println ( "m1 & m2" ) ;
}
Mugs ( ) { System. out. println ( "Mugs()" ) ; }
Mugs ( int i) { System. out. println ( "Mugs(int)" ) ; }
}
public class Test0510 {
public static void main ( String[ ] args) {
System. out. println ( "main()" ) ;
new Mugs ( ) ;
System. out. println ( "new Mugs() over" ) ;
new Mugs ( 1 ) ;
System. out. println ( "new Mugs(1) over" ) ;
}
}
class Art {
Art ( ) { System. out. println ( "Art()" ) ; }
}
class Drawing extends Art {
Drawing ( ) { System. out. println ( "Drawing()" ) ; }
}
public class Test0510 extends Drawing {
Test0510 ( ) { System. out. println ( "Test0510()" ) ; }
public static void main ( String[ ] args) {
new Test0510 ( ) ;
new Test0510 ( ) ;
}
}
class Insect {
private int i= 9 ;
protected int j;
Insect ( ) {
System. out. format ( "i = %d,j = %d\n" , i, j) ;
j= 39 ;
}
private static int x1= printAll ( "static Insect.x1" ) ;
static int printAll ( String s) {
System. out. println ( s) ;
return 27 ;
}
}
public class Test0510 extends Insect {
private int k= printAll ( "Test0511.k" ) ;
public Test0511 ( ) {
System. out. println ( "k = " + k) ;
System. out. println ( "j = " + j) ;
}
private static int x2= printAll ( "static Test0511.x2" ) ;
public static void main ( String[ ] args) {
System. out. println ( "main()" ) ;
new Test0511 ( ) ;
}
}
class Soap {
private String s;
Soap ( ) {
System. out. println ( "Soap()" ) ;
s= "constructed" ;
}
public String toString ( ) { return s; }
}
public class Test0511 {
private Soap aSoap;
private String s1= "s1" ,
s2, s3, s4;
Test0511 ( ) {
System. out. println ( "Test0511()" ) ;
aSoap= new Soap ( ) ;
s2= "s2" ;
}
{ s3= "s3" ; }
public String toString ( ) {
if ( s4== null) s4= "s4" ;
return String. format ( "s1 = % s, s2 = % s, s3 = % s, s4 = % s\naSoap = %
s\n", s1, s2, s3, s4, aSoap) ;
}
public static void main ( String[ ] args) {
System. out. println ( new Test0511 ( ) ) ;
}
}