2005年11月30日

//: c4/P7.java
// another method overloading:
// cn.ljf.cn.tools.* is use to short hand of system.out.println()
// use P.rintln() instead:
import cn.ljf.tools.*;

class Dog2{
 void bark(int i,String s){
  P.rintln(i + " dogs is barking on the " + s);
 }
 void bark(String s, int i){
  P.rintln(i + " dogs is bowling on the " + s);
 }
}
public class P7{
 public static void main(String[] args){
  Dog2 d1 = new Dog2();
  d1.bark(2,"YELLO RIVER");
  d1.bark("STREET",10);
  P.rintln("——————");
  P.rint("Done.");
 }
}

在 java.sun.com 上找过许多次,都没有找到,这次总算找到了,原来它藏在jdk下载页面的下边,例如下面这个地址:http://java.sun.com/j2se/1.4.2/download.html

最下边,倒数第三行黑底白字就是了“J2SE 1.4.2 Documentation”,很醒目,但却一直没有找到,也许是英文太差的缘故吧

下载了下来看,html格式的,虽然不如chm格式的好,毕意是官方的资料嘛。。。

//: c4/P6.java
// Using method overloading
class Dog{
 void bark(int i){
  System.out.println("Then dog was barking for " + i + " minutes!");
 }
 void bark(boolean o){
  if (o)
  System.out.println("The dog is angry,it is bowling!");
  else
  System.out.println("The dog isn’t angry,it looks kind!");
 }
}
public class P6{
 public static void main(String[] args){
  Dog d = new Dog();
  d.bark(3);
  d.bark(false);
  System.out.println("————");
  System.out.println("Done.");
 }
}

文章访问量一直为0,好壮观啊。^_^

希望早点修复,这样写文章才比较有成就感,嘻嘻~~

//: c4/P5.java
// create an array of string and
// print them:
import java.util.*;

public class P5{
 public static void main(String[] args){
 String[] strArray = new String[P5s.pRand(16)];
 System.out.println("Then are " + strArray.length + " elements:");
 System.out.println("——————-");
 for(int i = 0;i < strArray.length; i++){
  P5s s = new P5s();
  strArray[i] = s.createString();
  System.out.println(strArray[i]);
 }
 System.out.println("——————-");
 System.out.println("Done.");
}
}
//: create a random string
class P5s{
 static Random rand = new Random();
 static int pRand(int mod){
  return Math.abs(rand.nextInt()) % mod + 1;
 }
 public String createString(){
  char[] c = new char[pRand(26)];
  for(int i = 0;i < c.length;i++){
   c[i] = (char)(61 + pRand(25));
  }
  String s = "";
  for(int i = 0;i < c.length;i++){
   s += c[i];
  }
  return s;
 }
}

//: c4/P4.java
// create a reference array with class P2
// create some object for array:
import java.util.*;

public class P4{
 static Random rand = new Random();
 static int pRand(int mod){
  return Math.abs(rand.nextInt()) % mod + 1;
 }
 public static void main(String[] args){
  P2[] nbs = new P2[pRand(10)];
  System.out.println("nbs’s length is: " + nbs.length);
  for(int i = 0;i < nbs.length;i++){
   nbs[i] = new P2();
   nbs[i].createNB();
  }
  System.out.println("OK.");
  //System.out.println("nbs’s length is: " + nbs.length);
  //System.out.println("OK.There will no notebook object be created.");
 }
}///:finished P4.java

modify P2.java, add a method that can be use in class P4.

//:c4/P2.java
// use default constructor to create a object
// use constructor with args:
class NoteBook{
 NoteBook(){
  System.out.println("Created a NoteBook.");
 }
 NoteBook(String s){
  System.out.println("Create a " + s + " NoteBook.");
 }
}
public class P2{
 public static void main(String[] args){
  //CreateNB();
  P2 p = new P2();
  p.createNB();
  System.out.println("OK.");
 }
 public void createNB(){
  NoteBook nb1 = new NoteBook();
  NoteBook nb2 = new NoteBook("Sumsung");
 }
}///:finished P2.java

//: c4/P3.java
// create a reference array with class P2
import java.util.*;

public class P3{
 static Random rand = new Random();
 static int pRand(int mod){
  return Math.abs(rand.nextInt()) % mod + 1;
 }
 public static void main(String[] args){
  P2[] nbs = new P2[pRand(10)];
  System.out.println("nbs’s length is: " + nbs.length);
  System.out.println("OK.There will no notebook object be created.");
 }
}

// <<thinking in java>> chapter 4 practise 2
//:c4/P2.java
// use default constructor to create a object
// use constructor with args:
class NoteBook{
 NoteBook(){
  System.out.println("Created a NoteBook.");
 }
 NoteBook(String s){
  System.out.println("Create a " + s + " NoteBook.");
 }
}
public class P2{
 public static void main(String[] args){
  NoteBook nb1 = new NoteBook();
  NoteBook nb2 = new NoteBook("Sumsung");
  System.out.println("OK.");
 }
}

//:<<thinking in java>> chapter 4 practice 1
//:c4/P1.java
// use default constructor to create a object
class NoteBook{
 NoteBook(){
  System.out.println("Created a NoteBook.");
 }
}
public class P1{
 public static void main(String[] args){
  NoteBook nb1 = new NoteBook();
  System.out.println("OK.");
 }
}

我看liuren大佬的首页是有这个功能的,但许多人没有,是不是有选项可以选呢?