1. 程式人生 > >Java程式設計思想第五章習題

Java程式設計思想第五章習題

練習一:驗證未被初始化的引用被初始化成了null

package thinking.java.chapter05;

public class Demo01 {

	public static void main(String[] args) {
		class Person{
			String a;
			Person(){
				System.out.println(a);
			}
	}// TODO Auto-generated method stub
		new Person();
	}
}

練習二:

package thinking.java.chapter05;

public class Demo02 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		class Student{
			String stu1;
			Student(){
				String stu2 = null;
				System.out.println(stu1);
				System.out.println(stu2);
			}
		}
		new Student();
	}

}

練習三:

package thinking.java.chapter05;

public class Demo03 {

	public static void main(String[] args) {
		class A{
			A(){
				System.out.println("呼叫無參構造器");
			}
		}
		A a = new A();
	}
}

練習4:

package thinking.java.chapter05;

public class Demo03 {

	public static void main(String[] args) {
		class A{
			A(){
				System.out.println("呼叫無參構造器");
			}
			A(String a){
				System.out.println("呼叫有參構造器"+a);
			}
		}
		A a = new A();
		A aa = new A("TIFFANY");
	}
}

練習5:

package thinking.java.chapter05;

public class Demo05 {

	public static void main(String[] args) {
		class Dog{
			public void bark(int a){
				System.out.println("barking");
			}
			public void bark(String b) {
				System.out.println("howling");
			}
		}
		Dog d = new Dog();
		d.bark(1);
		d.bark("a");	
	}

}

練習6:

package thinking.java.chapter05;

public class Demo05 {

	public static void main(String[] args) {
		class Dog{
			public void bark(int a,String b){
				System.out.println("barking");
			}
			public void bark(String b,int a) {
				System.out.println("howling");
			}
		}
		Dog d = new Dog();
		d.bark(1,"a");
		d.bark("a",1);	
	}

}

練習8:

package thinking.java.chapter05;

public class Demo6 {

	public static void main(String[] args) {
		class Person{
			int i = 0;
			public int p1(Person per){
				per.p2(p2(this));
				return i;
			}
			public Person p2(Person per) {
				i++;
				return per;
			}
		}
		Person per = new Person();
		System.out.println(per.p1(per));
	}
}

練習9:

package thinking.java.chapter05;

public class Demo09 {
	public static void main(String[] args) {
		class Flower{
			int count = 0;
			 String aa = "rose";//不能在非靜態的類中宣告靜態變數
			Flower(String aa){
				count++;
				System.out.println(count+" "+aa);
			}
			public Flower(String aa,int count) {
				this(aa);
			}
		}
		Flower flower = new Flower("lily",2);
	}

}

練習16:

package thinking.java.chapter05;

public class Demo16 {

	public static void main(String[] args) {
		String[] s = new String[] {
				new String("tiffany"),
				new String("jeff"),
				new String("sarah"),
		};
		for(String a:s) {
			System.out.println(a);
		}
	}

}

練習17:

package thinking.java.chapter05;
class Persan{
	Persan(String name){
		System.out.println("姓名是:"+name);
	}
}
public class Demo17 {

	public static void main(String[] args) {
		Persan[] p = new Persan[] {};
//		並不會列印初始化的訊息,因為壓根沒有建立物件,不會呼叫構造器
	}

}

練習18:

package thinking.java.chapter05;
class Persan{
	Persan(String name){
//		String name = "tiffany";
		System.out.println("姓名是:"+name);
	}
}
public class Demo17 {

	public static void main(String[] args) {
		Persan[] p = new Persan[] {new Persan("tiffany")};//通過建立一個Persan物件來呼叫構造器
//		並不會列印初始化的訊息,因為壓根沒有建立物件,不會呼叫構造器
	}

}

練習21:

package thinking.java.chapter05;
//可以將enum理解成一個類,因為它由自己的方法,所以在外部不能宣告為public
enum Money{
	ONE,TWO,FIVE,TEN,FIVTY,HUNDRED,
}
public class Demo21 {
	public static void main(String[] args) {
			for(Money m:Money.values())
				System.out.println(m+" ordinal "+m.ordinal());
	}
}

練習22:

package thinking.java.chapter05;
//可以將enum理解成一個類,因為它由自己的方法,所以在外部不能宣告為public
enum Money{
	ONE,TWO,FIVE,TEN,FIVTY,HUNDRED,
}
public class Demo21 {
	Money money;
	public Demo21(Money money) {
		this.money = money;
	}
	public void describe() {
			System.out.println("the num is:");
			switch(money) {
			case ONE:
				System.out.println("one yuan");
				break;
			case TWO:
				System.out.println("two yuan");
				break;
			case FIVE:
				System.out.println("five yuan");
				break;
			case TEN:
				System.out.println("ten yuan");
				break;
			case FIVTY:
				System.out.println("fivty yuan");
				break;
			case HUNDRED:
				System.out.println("one hundred yuan");
				break;
			default:
				System.out.println("not found");
			}
			}
	public static void main(String[] args) {
//			for(Money m:Money.values())
//				System.out.println(m+" ordinal "+m.ordinal());
			Demo21 
			one = new Demo21(Money.ONE);
			one.describe();
	}
}