当前课程知识点:Java语言程序设计 > 第五章 面向对象:接口与嵌套类 > 5.7 嵌套类 > 5.7.2 局部类
/*
* Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of Oracle or the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
public class LocalClassExample {
static String regularExpression = "[^0-9]"; //正则表达式,代表“除了0到9的数字字符”
public static void validatePhoneNumber(String phoneNumber1, String phoneNumber2) {
final int numberLength = 10; //假定正确的电话号码包含且仅包含10个数字
// Valid in JDK 8 and later:
// int numberLength = 10;
class PhoneNumber {
String formattedPhoneNumber = null;
PhoneNumber(String phoneNumber){
String currentNumber = phoneNumber.replaceAll(regularExpression, "");//将所有非数字字符替换为空字符
if (currentNumber.length() == numberLength)
formattedPhoneNumber = currentNumber;
else
formattedPhoneNumber = null;
}
public String getNumber() {
return formattedPhoneNumber;
}
// Valid in JDK 8 and later:
// public void printOriginalNumbers() {
// System.out.println("Original numbers are " + phoneNumber1 +
// " and " + phoneNumber2);
// }
}
PhoneNumber myNumber1 = new PhoneNumber(phoneNumber1);
PhoneNumber myNumber2 = new PhoneNumber(phoneNumber2);
// Valid in JDK 8 and later:
// myNumber1.printOriginalNumbers();
if (myNumber1.getNumber() == null)
System.out.println("First number is invalid");
else
System.out.println("First number is " + myNumber1.getNumber());
if (myNumber2.getNumber() == null)
System.out.println("Second number is invalid");
else
System.out.println("Second number is " + myNumber2.getNumber());
}
public static void main(String... args) {
validatePhoneNumber("123-456-7890", "456-7890");
}
}
-考核方式
-1.1 Java应用、历史与基本特点
-1.2 第一个Java程序
-1.3 Java虚拟机、JRE、JDK等
-1.4 课程学习目标与方法
-1.5 实验指导:开发环境安装与配置
-1.6 Java常用类
-第1章测验 绪论
-第1章 编程题
- 第1章 课件
-2.1 Java基本语法概述
-2.2 数据类型与变量
--2.2.3 小练习:表达式正误判断
-2.3 运算符
--2.3.1 运算符1:算数运算符、赋值运算符、比较运算符
-2.4 选择结构
-2.5 循环结构
--思考:循环的使用
-2.6 方法
-2.7 数组与数组列表
-2.8 引用数据类型
-第2章测验 Java基本语法
-3.1 面向对象基本概念
-3.2 封装与模块化
-3.3 对象的初始化
-3.4 static与final
-3.5 使用包管理类
--练一练:在Eclipse中查看String等常用类的代码
-3.6 案例:面向对象之类的设计
-第3章测验 面向对象基础
-4.1 面向对象继承
--练一练:设计一个Triangle三角形类与Shape形状类,并用UML类图进行描述
-4.2 继承与覆盖
-4.3 继承与多态
-4.4 访问控制符
-4.5 final关键字
-4.6 抽象类
-4.7 面向对象设计
--思考:阅读Manager与Employee代码并回答问题
-4.8 实验指导:Eclipse的使用(自动代码生成)
-第4章测验 面向对象(继承与多态)
-5.1 接口基本概念
-5.2 常用接口
--思考:使用Comparable接口改造Shape及其子类
--思考:Comparable接口与Comparator接口的区别
-5.3 使用接口还是继承
-5.4 案例:DAO模式
-5.5 总结:接口
-5.6 Java8接口新特性:默认方法
-5.7 嵌套类
-5.8 Lambda表达式
-5.9 案例:购物车改进
-第5章测验 面向对象(接口与嵌套类)
-6.1 集合概述
-6.2 List接口
-6.3 Iterator迭代器
-6.4 Set接口
-6.5 Map接口
-6.6 泛型
--6.6.1 泛型
-6.7 集合应用
-6.8 集合其他常用方法
-可选:6.9 Collections工具类
-可选:6.10 Java8集合新特性
-第6章测验 集合框架
-7.1 基本概念
-7.2 程序处理机制
-7.3 异常继承层次
-7.4 异常处理:try...catch...finally
--练一练:try...catch...finally综合练习
-7.5 异常处理:throws与throw关键字
-7.6 不要滥用异常
-第7章测验 异常处理
-8.1 概述
-8.2 输入流、输出流基本概念
-8.3 File类
-8.4 字节流
-8.5 字符流
-8.6 缓冲流
-第8章测验 文件与流