public class Purse
{
public Purse(){...}
public void addNickels(int count){...}
public void addDimes(int count){...}
public void addQuarters(int count){...}
public double getTotal(){...}
public static final double NICKEL_VALUE =0.05;
public static final double DIME_VALUE =0.1;
public static final double QUARTER_VALUE =0.25; ...
}
public class Coin
{
public Coin(double aValue,String aName){...}
public double getValue(){...}
...
}
public class Purse
{
public Purse(){...}
public void add(Coin aCoin){...}
public double getTotal(){...}
...
}
public void transfer(double amount, BankAccount other)
{
balance = balance - amount;
other.balance = other.balance + amount;
}
public void deposit(double amount)
{
if (amount < 0)
System.out.println("Bad value");
. . .
}
void transfer(double amount, double otherBalance)
{
balance = balance - amount;
otherBalance = otherBalance + amount;
}
double savingsBalance = 1000;
harrysChecking.transfer(500, savingsBalance)
/**
Deposits money into this account.
@param amount the amount of money to deposit
(Precondition: amount >= 0)
*/
if (amount < 0)
throw new IllegalArgumentException();
balance = balance + amount;
if (amount < 0)
return; // don't do this
balance = balance + amount;
// no test--that's ok
// if this makes the balance negative,
// it's the caller's fault
balance = balance + amount;
/**
Deposits money into this account.
(Postcondition: getBalance() >= 0)
@param amount the amount of money to deposit
(Precondition: amount >= 0)
*/
|x - y| / max(|x|, |y|) <= εε是個很小的數。 假如 x 或 y 有一為 0, 則另一個的絕對值不大於 ε。
使用方法實現時, 既然所有外顯參數都是數字(基本型式之一), 並不是對物件做運算, 因此設計成 static 方法:
class Numeric { public static boolean approxEqual(double x, double y) { final double EPSILON = 1E-14; if(x == 0) return Math.abs(y) <= EPSILON; if(y == 0) return Math.abs(x) <= EPSILON; return Math.abs(x - y) / Math.max(Math.abs(y), Math.abs(y)) <= EPSILON; } }
if (Numeric.approxEqual(a, b)) . . .
public class BankAccount
{
. . .
private double balance;
private int accountNumber;
private static int lastAccountNumber;
}
public BankAccount()
{
lastAssignedNumber++;
// increment static field
accountNumber = lastAssignedNumber;
// set instance field
}
public class Coin
{
public void draw(Graphics2D g2)
{
String name = "SansSerif"; // local scope
g2.setFont(new Font(name, . . .)); // local name
g2.drawString(this.name, . . .); // field name
}
private String name; // class scope
. . .
}
套件 |
用途 |
例子 |
java.lang |
Language support |
Math |
java.util |
Utilities |
Random |
java.io |
Input and output |
PrintStream |
java.awt |
Abstract Windowing Toolkit |
Color |
java.applet |
Applets |
Applet |
java.net |
Networking |
Socket |
java.sql |
Database Access |
ResultSet |
javax.swing |
Swing user interface |
JButton |
org.omg.CORBA |
Common Object Request Broker Architecture |
ORB |
package com.horstmann.bigjava;
public class Numeric
{
. . .
}
java.awt.Color backgroundColor
= new java.awt.Color(. . .);
import java.awt.Color;
. . .
Color backgroundColor = new Color(. . .);
import java.awt.*;