浏览量 4602
2014/01/02 02:10
//: initialization/OrderOfInitialization.java
// Demonstrates initialization order.
import static net.mindview.util.Print.*;
// When the constructor is called to create a
// Window object, you'll see a message:
class Window {
Window(int marker) { print("Window(" + marker + ")"); }
}
class House {
Window w1 = new Window(1); // Before constructor
House() {
// Show that we're in the constructor:
print("House()");
w3 = new Window(33); // Reinitialize w3
}
Window w2 = new Window(2); // After constructor
void f() { print("f()"); }
Window w3 = new Window(3); // At end
}
public class OrderOfInitialization {
public static void main(String[] args) {
House h = new House();
h.f(); // Shows that construction is done
}
} /* Output:
Window(1)
Window(2)
Window(3)
House()
Window(33)
f()
*///:~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//: initialization/StaticInitialization.java
// Specifying initial values in a class definition.
import static net.mindview.util.Print.*;
class Bowl {
Bowl(int marker) {
print("Bowl(" + marker + ")");
}
void f1(int marker) {
print("f1(" + marker + ")");
}
}
class Table {
static Bowl bowl1 = new Bowl(1);
Table() {
print("Table()");
bowl2.f1(1);
}
void f2(int marker) {
print("f2(" + marker + ")");
}
static Bowl bowl2 = new Bowl(2);
}
class Cupboard {
Bowl bowl3 = new Bowl(3);
static Bowl bowl4 = new Bowl(4);
Cupboard() {
print("Cupboard()");
bowl4.f1(2);
}
void f3(int marker) {
print("f3(" + marker + ")");
}
static Bowl bowl5 = new Bowl(5);
}
public class StaticInitialization {
public static void main(String[] args) {
print("Creating new Cupboard() in main");
new Cupboard();
print("Creating new Cupboard() in main");
new Cupboard();
table.f2(1);
cupboard.f3(1);
}
static Table table = new Table();
static Cupboard cupboard = new Cupboard();
} /* Output:
Bowl(1)
Bowl(2)
Table()
f1(1)
Bowl(4)
Bowl(5)
Bowl(3)
Cupboard()
f1(2)
Creating new Cupboard() in main
Bowl(3)
Cupboard()
f1(2)
Creating new Cupboard() in main
Bowl(3)
Cupboard()
f1(2)
f2(1)
f3(1)
*///:~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
package javahaonan;
import static javahaonan.Print.*;
class Bowl {
Bowl( int marker){
print("Bowl("+ marker + ")");
}
void f1(int marker){
print("f1("+ marker + ") ");
}
}
class Table{
static Bowl bowl1 = new Bowl(1);
Table(){
print("Table()");
bowl2.f1(1);
}
void f2(int marker) {
print("f2("+ marker+ ")");
}
static Bowl bowl2 = new Bowl(2);
}
class Cupboard{
Bowl bowl3 = new Bowl(3);
static Bowl bowl4 = new Bowl(4);
Cupboard(){
print("Cupboard()");
bowl4.f1(2);
}
void f3(int marker) {
print("f3("+marker+ ")");
}
static Bowl bowl5 = new Bowl(5);
}
public class StaticInitialization {
public static void main(String[] args){
print("Creating new Cupboard() in main");
new Cupboard();
print("Creating new Cupboard() in main");
new Cupboard();
table.f2(1);
cupboard.f3(1);
}
static Table table = new Table();
static Cupboard cupboard = new Cupboard();
}
//*Bowl(1)
Bowl(2)
Table()
f1(1)
Bowl(4)
Bowl(5)
Bowl(3)
Cupboard()
f1(2)
Creating new Cupboard() in main
Bowl(3)
Cupboard()
f1(2)
Creating new Cupboard() in main
Bowl(3)
Cupboard()
f1(2)
f2(1)
f3(1)*/
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
package javahaonan;
import static javahaonan.Print.*;
class Rock{
Rock (){
printnb("Rock ");
}
}
public class SimpleConstructor {
public static void main(String[] args){
for(int i=0;i<8;i++)
new Rock();
}
}
//Rock Rock Rock Rock Rock Rock Rock Rock
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
package javahaonan;
class Rock2{
Rock2( int i){
System. out.print("Rock " + i + " " );
}
}
public class SimpleConstructor2 {
public static void main(String[] args) {
for(int i=1;i<3;i++)
new Rock2(i);
}
}
//Rock 1 Rock 2
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
package javat;
import static javat.Print.*;
class Cup{
Cup( int marker){
print("Cup("+ marker+ ")");
}
void f( int marker){
print("f(" + marker + ")");
}
}
class Cups{
static Cup cup1 ;
static Cup cup2 ;
static{
cup1 = new Cup(1);
cup2 = new Cup(2);
}
Cups(){
print("Cups()");
}
}
public class ExplicitStatic {
public static void main(String[] args) {
Cups. cup2.f(99);
print("Inside main()");
Cups. cup1.f(99);
Cups c=new Cups();
}
}
/*
Cup(1)
Cup(2)
f(99)
Inside main()
f(99)
Cups()
*/
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
package javat;
import static javat.Print.*;
class Mug{
Mug( int marker){
print("Mug("+ marker+ ")");
}
void f(int marker){
print("f("+marker+ ")");
}
}
public class Mugs {
Mug mug1;
Mug mug2;
{
mug1= new Mug(1);
mug2= new Mug(2);
print("mug1 & mug2 initialized" );
}
Mugs(){
print( "Mugs()");
}
Mugs(int i){
print("Mugs(int)" );
}
public static void main(String[] args){
print ( "Inside main()");
new Mugs();
print( "new Mugs() completed");
new Mugs(1);
print( "new Mugs(1) completed");
}
}/* output:
Inside main()
Mug(1)
Mug(2)
mug1 & mug2 initialized
Mugs()
new Mugs() completed
Mug(1)
Mug(2)
mug1 & mug2 initialized
Mugs(int)
new Mugs(1) completed
*/
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
package javat;
import static javat.Print.*;
public class ArraysOfPrimitives {
public static void main(String[] args){
int[] a1={1,2,3,4,5};
int[] a2;
a2=a1;
for (int i=0;i<a2.length;i++)
a2[i]=a2[i]+1;
for (int i=0;i<a1.length;i++)
print("a1[" +i+"]=" +a1[i]);
}
}
/*
a1[0]=2
a1[1]=3
a1[2]=4
a1[3]=5
a1[4]=6
*/
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
package javat;
import java.util.*;
import static javat.Print.*;
public class ArrayNew {
public static void main(String[] args) {
// TODO 自动生成的方法存根
int[] a;
Random rand=new Random(47);
a=new int[rand.nextInt(20)];
print( "length of a="+ a.length );
print(Arrays. toString(a));
}
}
/*
length of a=18
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
*/
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
package javat;
import java.util.*;
import static javat.Print.*;
public class ArrayClassObj {
public static void main(String[] args) {
Random rand= new Random();
Integer[] a= new Integer[rand.nextInt(20)];
print( "length of a="+a.length );
for(int i=0;i<a.length;i++)
a[i]=rand.nextInt(500);
print(Arrays. toString(a));
}
}
/*
length of a=16
[132, 424, 10, 411, 429, 305, 448, 476, 455, 340, 326, 120, 399, 157, 294, 394]
*/
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
package javat;
import java.util.*;
public class ArrayInit {
public static void main(String[] args){
Integer[] a = {
new Integer(1),
new Integer(2),
3,
};
Integer[] b= new Integer[]{
new Integer(1),
new Integer(2),
3
};
System. out.println(Arrays.toString(a));
System. out.println(Arrays.toString(b));
}
}
/*
[1, 2, 3]
[1, 2, 3]
*/
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
package javat;
public class DynamicArray {
public static void main(String[] args) {
Other. main(new String[]{ "wangzi","de de" ,"dfsa" });
}
}
class Other{
public static void main(String[] args) {
for(String s : args)
System. out.print(s+ " " );
}
}
//可以在方法内部调用。
//wangzi de de dfsa
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
上一篇 搜索 下一篇