Java编程思想_示例代码_第05章_初始化和清理_Initialization&Cleanup

ExerciseCode_01

package initialization;

//: initialization/SimpleConstructor.java
//  Demonstration of a simple constructor.

class Rock {
	
	Rock() { // This is the constructor
		System.out.print("Rock ");
	}
	
}

public class SimpleConstructor {
	
	public static void main(String[] args) {
		for(int i = 0; i < 10; i++)
			new Rock();
	}
	
} /* Output:
Rock Rock Rock Rock Rock Rock Rock Rock Rock Rock
*///:~

ExerciseCode_02

package initialization;

//: initialization/SimpleConstructor2.java
//  Constructors can have arguments.

class Rock2 {
	
  Rock2(int i) {
    System.out.print("Rock " + i + " ");
  }
  
}

public class SimpleConstructor2 {
	
	public static void main(String[] args) {
		for(int i = 0; i < 8; i++)
			new Rock2(i);
	}
	
} /* Output:
Rock 0 Rock 1 Rock 2 Rock 3 Rock 4 Rock 5 Rock 6 Rock 7
*///:~

ExerciseCode_03

package initialization;

//: initialization/Overloading.java
//  Demonstration of both constructor and ordinary method overloading 
//构造函数和普通方法重载的演示

import static net.mindview.util.Print.*;

class Tree {
	
	int height;
	
	Tree() {
		print("Planting a seedling");
		height = 0;
	}
	
	Tree(int initialHeight) {
		height = initialHeight;
		print("Creating new Tree that is " + height + " feet tall");
	}	
	
	void info() {
		print("Tree is " + height + " feet tall");
	}
	
	void info(String s) {
		print(s + ": Tree is " + height + " feet tall");
	}
	
}

public class Overloading {
	
	public static void main(String[] args) {
		
		for(int i = 0; i < 5; i++) {
			Tree t = new Tree(i);
			t.info();
			t.info("overloaded method");
		}
		// Overloaded constructor:
		new Tree();
  }	
	
} /* Output:
Creating new Tree that is 0 feet tall
Tree is 0 feet tall
overloaded method: Tree is 0 feet tall
Creating new Tree that is 1 feet tall
Tree is 1 feet tall
overloaded method: Tree is 1 feet tall
Creating new Tree that is 2 feet tall
Tree is 2 feet tall
overloaded method: Tree is 2 feet tall
Creating new Tree that is 3 feet tall
Tree is 3 feet tall
overloaded method: Tree is 3 feet tall
Creating new Tree that is 4 feet tall
Tree is 4 feet tall
overloaded method: Tree is 4 feet tall
Planting a seedling
*///:~

ExerciseCode_04

package initialization;

//: initialization/OverloadingOrder.java
//  Overloading based on the order of the arguments. 
//	基于参数顺序重载

import static net.mindview.util.Print.*;

public class OverloadingOrder {
	
	static void f(String s, int i) {
		print("String: " + s + ", int: " + i);
	}
	
	static void f(int i, String s) {
		print("int: " + i + ", String: " + s);
	}
	
	public static void main(String[] args) {
		f("String first", 11);
		f(99, "Int first");
	}
	
} /* Output:
String: String first, int: 11
int: 99, String: Int first
*///:~

ExerciseCode_05

package initialization;

//: initialization/PrimitiveOverloading.java

import static net.mindview.util.Print.*;

public class PrimitiveOverloading {

	void f1(char x) { 
		printnb("f1(char) ");
	}
	
	void f1(byte x){
		printnb("f1(byte) ");
	}
	
	void f1(short x) { 
		printnb("f1(short) "); 
	}
	
	void f1(int x) { 
		printnb("f1(int) "); 
	}
 
	void f1(long x) {
		printnb("f1(long) "); 
	}
	
	void f1(float x) { 
		printnb("f1(float) ");
	}
	
	void f1(double x) {
		printnb("f1(double) "); 
	}
	
	void f2(byte x) { 	
		printnb("f2(byte) "); 
	}
	
	void f2(short x) { 
		printnb("f2(short) ");
	}
	
	void f2(int x) { 
		printnb("f2(int) ");
	}
	
	void f2(long x) {
		printnb("f2(long) ");
	}
	
	void f2(float x) { 
		printnb("f2(float) ");
	}
	
	void f2(double x) { 
		printnb("f2(double) "); 
	}

	void f3(short x) { 
		printnb("f3(short) ");
	}
	
	void f3(int x) { 
		printnb("f3(int) "); 
	}
	
	
	void f3(long x) {
		printnb("f3(long) "); 
	}
	
	void f3(float x) {
		printnb("f3(float) "); 
	}
	
	void f3(double x) { 
		printnb("f3(double) "); 
	}
	
	void f4(int x) { 
		printnb("f4(int) "); 
	}
	
	void f4(long x) { 
		printnb("f4(long) "); 
	}
	
	void f4(float x) { 
		printnb("f4(float) "); 
	}
	
	void f4(double x) {
		printnb("f4(double) ");
	}
		
	void f5(long x) { 
		printnb("f5(long) "); 
	}
	
	void f5(float x) { 
		printnb("f5(float) "); 
	}
	
	void f5(double x) { 
		printnb("f5(double) "); 
	}

	void f6(float x) { 
		printnb("f6(float) ");
	}
	
	void f6(double x) {
		printnb("f6(double) ");
	}

	void f7(double x) { 
		printnb("f7(double) "); 
	}

	void testConstVal() {
		printnb("5: ");
		f1(5);f2(5);f3(5);f4(5);f5(5);f6(5);f7(5); 
		print();
	}
	
	void testChar() {
		char x = 'x';
		printnb("char: ");
		f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x); print();
	}
	
	void testByte() {
		byte x = 0;
		printnb("byte: ");
		f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x); print();
	}
	
	void testShort() {
		short x = 0;
		printnb("short: ");
		f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x); print();
	}
	
	void testInt() {
		int x = 0;
		printnb("int: ");
		f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x); print();
	}
	
	void testLong() {
		long x = 0;
		printnb("long: ");
		f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x); print();
	}
	
	void testFloat() {
		float x = 0;
		printnb("float: ");
		f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x); print();
	}
	
	void testDouble() {
		double x = 0;
		printnb("double: ");
		f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x); print();
	}	
	
	public static void main(String[] args) {
		PrimitiveOverloading p = new PrimitiveOverloading();
		p.testConstVal(); 
//5: f1(int) f2(int) f3(int) f4(int) f5(long) f6(float) f7(double)
		p.testChar();     
//char: f1(char) f2(int) f3(int) f4(int) f5(long) f6(float) f7(double)
		p.testByte();     
//byte: f1(byte) f2(byte) f3(short) f4(int) f5(long) f6(float) f7(double)
		p.testShort();    
//short: f1(short) f2(short) f3(short) f4(int) f5(long) f6(float) f7(double)
		p.testInt();      
//int: f1(int) f2(int) f3(int) f4(int) f5(long) f6(float) f7(double)
		p.testLong();     
//long: f1(long) f2(long) f3(long) f4(long) f5(long) f6(float) f7(double)
		p.testFloat();    
//float: f1(float) f2(float) f3(float) f4(float) f5(float) f6(float) f7(double)
		p.testDouble();   
//double: f1(double) f2(double) f3(double) f4(double) f5(double) f6(double)
f7(double)
	}
	
} 

ExerciseCode_06

package initialization;

//: initialization/Demotion.java
// 	Demotion of primitives and overloading  //primitives 基本类型

import static net.mindview.util.Print.*;


public class Demotion {
	  void f1(char x) { print("f1(char)"); }
	  void f1(byte x) { print("f1(byte)"); }
	  void f1(short x) { print("f1(short)"); }
	  void f1(int x) { print("f1(int)"); }
	  void f1(long x) { print("f1(long)"); }
	  void f1(float x) { print("f1(float)"); }
	  void f1(double x) { print("f1(double)"); }

	  void f2(char x) { print("f2(char)"); }
	  void f2(byte x) { print("f2(byte)"); }
	  void f2(short x) { print("f2(short)"); }
	  void f2(int x) { print("f2(int)"); }
	  void f2(long x) { print("f2(long)"); }
	  void f2(float x) { print("f2(float)"); }

	  void f3(char x) { print("f3(char)"); }
	  void f3(byte x) { print("f3(byte)"); }
	  void f3(short x) { print("f3(short)"); }
	  void f3(int x) { print("f3(int)"); }
	  void f3(long x) { print("f3(long)"); }

	  void f4(char x) { print("f4(char)"); }
	  void f4(byte x) { print("f4(byte)"); }
	  void f4(short x) { print("f4(short)"); }
	  void f4(int x) { print("f4(int)"); }
	
	  void f5(char x) { print("f5(char)"); }
	  void f5(byte x) { print("f5(byte)"); }
	  void f5(short x) { print("f5(short)"); }

	  void f6(char x) { print("f6(char)"); }
	  void f6(byte x) { print("f6(byte)"); }
	
	  void f7(char x) { print("f7(char)"); }
	
	  void testDouble() {
		  double x = 0;
		  print("double argument:"); //Output: double argument:
		  f1(x); 					 //Output: f1(double)
		  f2((float)x); 			 //Output: f2(float)
		  f3((long)x); 			     //Output: f3(long)
		  f4((int)x); 			     //Output: f4(int)
		  f5((short)x); 			 //Output: f5(short)
		  f6((byte)x); 			     //Output: f6(byte)
		  f7((char)x);	 			 //Output: f7(char)
	  }
	  
	  public static void main(String[] args) {
		  Demotion p = new Demotion();
		  p.testDouble();
	  }
	  
} /* Output:
double argument:
f1(double)
f2(float)
f3(long)
f4(int)
f5(short)
f6(byte)
f7(char)
*///:~

ExerciseCode_07


ExerciseCode_08

ExerciseCode_09

ExerciseCode_10

ExerciseCode_11

ExerciseCode_12

ExerciseCode_13

ExerciseCode_14

ExerciseCode_15

ExerciseCode_16

ExerciseCode_17

ExerciseCode_18

ExerciseCode_19

ExerciseCode_20

ExerciseCode_21

ExerciseCode_22

ExerciseCode_23

ExerciseCode_24

ExerciseCode_25

ExerciseCode_26

ExerciseCode_27

ExerciseCode_28

ExerciseCode_29

ExerciseCode_30

ExerciseCode_31

ExerciseCode_32

ExerciseCode_33

ExerciseCode_34

ExerciseCode_35

ExerciseCode_36

ExerciseCode_37

ExerciseCode_38

ExerciseCode_39

ExerciseCode_40

ExerciseCode_41

ExerciseCode_42

猜你喜欢

转载自blog.csdn.net/weixin_43002838/article/details/88917990