JAVA全集-05-高级类特征2

原创 2017年01月03日 17:58:47




复习:




package banking6.domain;

public class Bank {
	private Customer[] customers; // 用于存放客户
	private int numberOfCustomers;// 记录客户的个数

	private Bank() {
		customers = new Customer[5];

	}
	private static Bank bank=new Bank();
	public static Bank getBanking(){
		return bank;
	}

	// 添加一个Customer到数组中
	public void addCustomer(String f, String l) {
		customers[numberOfCustomers] = new Customer(f, l);
		numberOfCustomers++;
	}

	// 获取Customer的个数
	public int getNumOfCustomers() {
		return numberOfCustomers;
	}

	// 返回指定索引位置的Customer
	public Customer getCustomer(int index) {
		return customers[index];
	}
}



package banking6.reports;

import java.text.NumberFormat;

import banking6.domain.*;


public class CustomerReport {
	public void generateReport(){
		NumberFormat currency_format = NumberFormat.getCurrencyInstance();
   Bank bank=Bank.getBanking();
   Customer customer;
		
		// Generate a report
		System.out.println("\t\t\tCUSTOMERS REPORT");
		System.out.println("\t\t\t================");

		for (int cust_idx = 0; cust_idx < bank.getNumOfCustomers(); cust_idx++) {
			customer = bank.getCustomer(cust_idx);

			System.out.println();
			System.out.println("Customer: " + customer.getLastName() + ", "
					+ customer.getFirstName());

			for (int acct_idx = 0; acct_idx < customer.getNumOfAccounts(); acct_idx++) {
				Account account = customer.getAccount(acct_idx);
				String account_type = "";

				// Determine the account type
				/***
				 * Step 1: Use the instanceof operator to test what type of
				 * account we have and set account_type to an appropriate value,
				 * such as "Savings Account" or "Checking Account".
				 ***/
				if (account instanceof SavingAccount) {
					account_type = "SavingAccount";
				}
				if (account instanceof CheckingAccount) {
					account_type = "CheckingAccount";
				}

				// Print the current balance of the account
				/***
				 * Step 2: Print out the type of account and the balance. Feel
				 * free to use the currency_format formatter to generate a
				 * "currency string" for the balance.
				 ***/
				System.out.println(account_type + "current balance is "
						+ currency_format.format(account.getBalance()));
			}
		}
	}
}


package testbanking;
/*
 * This class creates the program to test the banking classes.
 * It creates a set of customers, with a few accounts each,
 * and generates a report of current account balances.
 */

import banking6.domain.*;
import banking6.reports.CustomerReport;

public class TestBanking6 {

  public static void main(String[] args) {
    Bank     bank = Bank.getBanking();
    Customer customer;
    CustomerReport report = new CustomerReport();

    // Create several customers and their accounts
    bank.addCustomer("Jane", "Simms");
    customer = bank.getCustomer(0);
    customer.addAccount(new SavingAccount(500.00, 0.05));
    customer.addAccount(new CheckingAccount(200.00, 400.00));

    bank.addCustomer("Owen", "Bryant");
    customer = bank.getCustomer(1);
    customer.addAccount(new CheckingAccount(200.00));

    bank.addCustomer("Tim", "Soley");
    customer = bank.getCustomer(2);
    customer.addAccount(new SavingAccount(1500.00, 0.05));
    customer.addAccount(new CheckingAccount(200.00));

    bank.addCustomer("Maria", "Soley");
    customer = bank.getCustomer(3);
    // Maria and Tim have a shared checking account
    customer.addAccount(bank.getCustomer(2).getAccount(1));
    customer.addAccount(new SavingAccount(150.00, 0.05));

    // Generate a report
    report.generateReport();
  }
}


练习:

package test;

public class test {

	public static void main(String[] args){
		new Leaf();
		//new Leaf();
	}
}

class Root{
	static{
		System.out.println("Root的静态初始化块");
	}
	{
		System.out.println("Root的普通初始化块");
	}
	public Root(){
		System.out.println("Root的无参数的构造器");
	}
}
class Mid extends Root{
	static{
		System.out.println("Mid的静态初始化块");
	}
	{
		System.out.println("Mid的普通初始化块");
	}
	public Mid(){
		System.out.println("Mid的无参数的构造器");
	}
	public Mid(String msg){
		//通过this调用同一类中重载的构造器
		this();
		System.out.println("Mid的带参数构造器,其参数值:"
			+ msg);
	}
}
class Leaf extends Mid{
	static{
		System.out.println("Leaf的静态初始化块");
	}
	{
		System.out.println("Leaf的普通初始化块");
	}	
	public Leaf(){
		//通过super调用父类中有一个字符串参数的构造器
		super("SB");
		System.out.println("执行Leaf的构造器");
	}
}












package com.jisu.licheng;

//工厂方法设计模式
public class FactoryMethod {
  public static void main(String[] args) {
	IWorkFactory i=new StudentWorkFactory();
	i.getWork().doWork();
	
}
}

interface Work {
	void doWork();
}

class StudrntWork implements Work {

	@Override
	public void doWork() {

		System.out.println("学生写作业");
	}

}

class TeacherWork implements Work {

	@Override
	public void doWork() {
		System.out.println("老师改作业");

	}
}

interface IWorkFactory {

	Work getWork();
}

class StudentWorkFactory implements IWorkFactory{

	@Override
	public Work getWork() {
		
		return new StudrntWork();
	}
	
}

class TeacherWorkFactory implements IWorkFactory{

	@Override
	public Work getWork() {
		
		return new TeacherWork();
	}
	
}






package com.jisu.licheng;

//代理模式
public class ProxyPattern {

	 Object obj=new ProxyObject();
	
	// obj.action();
}

interface Object {
	void action();
}

// 代理类
class ProxyObject implements Object {

	Object obj;

	 public ProxyObject() {
		 System.out.println("代理类创建成功");
			obj = new ObjectImpl();
	}
	
	@Override
	public void action() {
		System.out.println("代理类开始执行");
		obj.action();
		System.out.println("代理类执行结束");
	}

}

// 被代理类
class ObjectImpl implements Object {

	@Override
	public void action() {
		System.out.println("被代理类开始执行");
		System.out.println("具体操作");
		System.out.println("被代理类执行完毕");

	}

}


interface USB{		// 
	public void start() ;
	public void stop() ;	
}
class Computer{
	public static void show(USB usb){	
		usb.start() ;
		System.out.println("=========== USB 设备工作 ========") ;
		usb.stop() ;
	}
};
class Flash implements USB{
	public void start(){	// 重写方法
		System.out.println("U盘开始工作。") ;
	}
	public void stop(){		// 重写方法
		System.out.println("U盘停止工作。") ;
	}
};
class Print implements USB{
	public void start(){	// 重写方法
		System.out.println("打印机开始工作。") ;
	}
	public void stop(){		// 重写方法
		System.out.println("打印机停止工作。") ;
	}
};
public class InterfaceDemo{
	public static void main(String args[]){
		Computer.show(new Flash()) ;
		Computer.show(new Print()) ;

		c.show(new USB(){
			public void start(){
				System.out.println("移动硬盘开始运行");
			}
			public void stop(){
				System.out.println("移动硬盘停止运行");
			}
		});
	}
};











面向对象总结:







相关文章推荐

JAVA全集-04-高级类特征1

执行顺序由上至下

java学习之路 之 高级类特性1-面向对象特征之三:多态

面向对象特征之三:多态 多态性,是面向对象中最重要的概念,在java中有两种体现: 方法的重载(overload)和重写(overwrite)。 对象的多态性   ——可以直接应用在抽象类和接口...

JAVA高级05--网络编程--要点总结

JAVA高级05_网络编程 1.IP地址和Port(端口号) 本地回路的IP地址:127.0.0.1,特殊IP地址,不经过网卡。没有和网卡绑定。 Port两个字节,不同的应用程序使用不同的端...

Java2核心技术卷II高级特征分卷1

  • 2012年09月15日 10:24
  • 49MB
  • 下载

JAVA高级视频02_IO输入与输出 05 FileInputStream和FileOutPutStream

FileInputStream和FileOutputStream分别用来创建磁盘文件的输入流和输出流,构造函数来指定文件的路径和文件名。这里的In和out也是相对应用程序来说的,FileInputSt...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:JAVA全集-05-高级类特征2
举报原因:
原因补充:

(最多只允许输入30个字)