sendratechange.java

来自「Java 入门书的源码」· Java 代码 · 共 50 行

JAVA
50
字号
//Copyright (c) 1998, Arthur Gittleman
//This example is provided WITHOUT ANY WARRANTY either expressed or implied.

/* Any object that implements the RateChangeListener 
 * interface can be notified about interest rate 
 * changes.  We notify investor and business objects 
 * which implement RateChangeListener to take action 
 * when the rate change occurs.
 */

interface RateChangeListener {
  public void rateRaised(double amount);
  public void rateLowered(double amount);
}
class Investor implements RateChangeListener {
  public void rateRaised(double amount) {
    System.out.println("   Investor sells stocks");
  }
  public void rateLowered(double amount) {
    System.out.println("   Investor buys stocks");
  }
  public void countMoney() {
    System.out.println("Counting money");
  }
}
class Business implements RateChangeListener {
  public void rateRaised(double amount) {
    System.out.println("   Business reduces debt");
  }
  public void rateLowered(double amount) {
    System.out.println("   Business takes a loan");
  }
  public void doPayroll() {
    System.out.println("Doing payroll");
  }
}
public class SendRateChange {
  public static void main(String [] args) {
    RateChangeListener investor = new Investor();
    RateChangeListener business = new Business();
    System.out.println("Raising interest rates");
    investor.rateRaised(.50);
    business.rateRaised(.50);
    System.out.println("Lowering interest rates");
    investor.rateLowered(.25);
    business.rateLowered(.25);
  }
}
               

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?