├── .gitignore ├── 递归死类.java ├── 四则运算类.java ├── 问好类.java ├── 特殊字符类.java ├── 随机数生成器类.java ├── 大人类.java ├── 小孩类.java ├── 原始人测试类.java ├── 听不懂例外.java ├── 聊天机器人 ├── 聊天接口.java ├── 聊天服务类.java ├── 聊天客户端类.java └── 聊天机器人类.java ├── 条件语句类.java ├── 识字类.java ├── 字符串方法类.java ├── 投资回报类.java ├── 根号类.java ├── 识字循环类.java ├── 投资回报循环类.java ├── 人测试类.java ├── 世界类.java ├── 变量演示类.java ├── 小九九类.java ├── 人类.java ├── 排队类.java ├── 排队买票类.java ├── README.md └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | *.komodoproject -------------------------------------------------------------------------------- /递归死类.java: -------------------------------------------------------------------------------- 1 | class 递归死类 { 2 | public static void main (String[] 参数) { 3 | main(参数); 4 | } 5 | } -------------------------------------------------------------------------------- /四则运算类.java: -------------------------------------------------------------------------------- 1 | class 四则运算类 { 2 | public static void main (String[] 参数) { 3 | System.out.println(5+(2-1)*6/3); 4 | } 5 | } -------------------------------------------------------------------------------- /问好类.java: -------------------------------------------------------------------------------- 1 | class 问好类 { 2 | public static void main (String[] 参数) { 3 | // 要让它做的事 4 | System.out.println("吃了么"); 5 | } 6 | } -------------------------------------------------------------------------------- /特殊字符类.java: -------------------------------------------------------------------------------- 1 | class 特殊字符类 { 2 | public static void main (String[] 参数) { 3 | System.out.println("边检员低头看着证件说\t\"这么久没回了啊?\".\n百感交集,咧着嘴回了一句\t\"是啊,还没完呢\""); 4 | } 5 | } -------------------------------------------------------------------------------- /随机数生成器类.java: -------------------------------------------------------------------------------- 1 | import java.util.Random; 2 | 3 | 4 | 5 | class 随机数生成器类 { 6 | public static void main (String[] 参数) { 7 | Random 生成器 = new Random(); 8 | System.out.println("我想到的数字是:" + 生成器.nextInt()); 9 | } 10 | } -------------------------------------------------------------------------------- /大人类.java: -------------------------------------------------------------------------------- 1 | public class 大人类 extends 人类 { 2 | String 责任 = "扶老携幼"; 3 | 4 | public 大人类(String 姓名, int 年龄) { 5 | super(姓名, 年龄); 6 | } 7 | 8 | public void 生活() { 9 | System.out.println("我必须" + 责任); 10 | } 11 | } -------------------------------------------------------------------------------- /小孩类.java: -------------------------------------------------------------------------------- 1 | public class 小孩类 extends 人类 { 2 | String 想做的事 = "大人的事"; 3 | 4 | public 小孩类(String 姓名, int 年龄) { 5 | super(姓名, 年龄); 6 | } 7 | 8 | public void 长大() { 9 | System.out.println("我要做" + 想做的事); 10 | } 11 | } -------------------------------------------------------------------------------- /原始人测试类.java: -------------------------------------------------------------------------------- 1 | // 是的这个类名是故意这么起的 2 | 3 | class 原始人测试类 { 4 | public static void main(String[] 参数) throws 听不懂例外 { 5 | 人类 无名 = new 人类("", -1); 6 | System.out.println(无名.回答("秘密哦")); 7 | System.out.println(无名.回答("?")); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /听不懂例外.java: -------------------------------------------------------------------------------- 1 | public class 听不懂例外 extends Exception { 2 | 3 | private String 听见的; 4 | 5 | public 听不懂例外(String 听见的) { 6 | this.听见的 = 听见的; 7 | } 8 | 9 | @Override 10 | public String getMessage() { 11 | return "听不懂这个: " + 听见的; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /聊天机器人/聊天接口.java: -------------------------------------------------------------------------------- 1 | package 聊天机器人; 2 | 3 | import javax.jws.WebMethod; 4 | import javax.jws.WebService; 5 | import javax.jws.soap.SOAPBinding; 6 | 7 | @WebService 8 | @SOAPBinding(style = SOAPBinding.Style.RPC) 9 | public interface 聊天接口 { 10 | @WebMethod String 回答(String 听到的); 11 | } -------------------------------------------------------------------------------- /聊天机器人/聊天服务类.java: -------------------------------------------------------------------------------- 1 | package 聊天机器人; 2 | 3 | import javax.xml.ws.Endpoint; 4 | 5 | public class 聊天服务类 { 6 | public static void main(String[] arguments) { 7 | String url = "http://127.0.0.1:5335/service"; 8 | Endpoint.publish(url, new 聊天机器人类()); 9 | System.out.println("服务启动在:" + url); 10 | } 11 | } -------------------------------------------------------------------------------- /条件语句类.java: -------------------------------------------------------------------------------- 1 | class 条件语句类 { 2 | public static void main (String[] 参数) { 3 | int 年龄 = 21; 4 | if (年龄 < 5) { 5 | System.out.println("这是哪家闺女啊?爸妈在哪儿呢?"); 6 | } else if (年龄 < 20) { 7 | System.out.println("没到法定婚龄! 等几年再结婚吧"); 8 | } else { 9 | System.out.println("妹妹,真想嫁也拦不住你.要不再考虑一天?"); 10 | } 11 | } 12 | } -------------------------------------------------------------------------------- /识字类.java: -------------------------------------------------------------------------------- 1 | class 识字类 { 2 | public static void main (String[] 参数) { 3 | int 识字量 = 1; 4 | int 每年增倍数 = 2; 5 | 6 | // 第一年 7 | 识字量 = (1 + 每年增倍数) * 识字量; 8 | 9 | // 第二年 10 | 识字量 = (1 + 每年增倍数) * 识字量; 11 | 12 | // 第三年 13 | 识字量 = (1 + 每年增倍数) * 识字量; 14 | System.out.println("三年后认识" + 识字量 + "个字咯"); 15 | } 16 | } -------------------------------------------------------------------------------- /字符串方法类.java: -------------------------------------------------------------------------------- 1 | class 字符串方法类 { 2 | public static void main (String[] 参数) { 3 | String 字符串 = "去是go"; 4 | String 搜索字符串 = "go"; 5 | System.out.println("\"" + 字符串 + "\"的长度:" + 字符串.length()); 6 | System.out.println(搜索字符串 + "在\"" + 字符串 + "\"的位置是:" + 字符串.indexOf(搜索字符串)); 7 | System.out.println("\"" + 字符串 + "\"的第一个字符是:" + 字符串.charAt(0)); 8 | } 9 | } -------------------------------------------------------------------------------- /投资回报类.java: -------------------------------------------------------------------------------- 1 | class 投资回报类 { 2 | public static void main (String[] 参数) { 3 | float 账户余额 = 1000f; 4 | float 回报率 = 0.08f; 5 | 6 | // 第一年 7 | 账户余额 = (1 + 回报率) * 账户余额; 8 | 9 | // 第二年 10 | 账户余额 = (1 + 回报率) * 账户余额; 11 | 12 | // 第三年 13 | 账户余额 = (1 + 回报率) * 账户余额; 14 | System.out.println("三年后变成" + 账户余额 + "元"); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /根号类.java: -------------------------------------------------------------------------------- 1 | class 根号类 { 2 | public static void main (String[] 参数) { 3 | int 数 = 0; 4 | try { 5 | 数 = Integer.parseInt(参数[0]); 6 | System.out.println(数 + "的平方根是" + Math.sqrt(数)); 7 | } catch (NumberFormatException e) { 8 | System.out.println(参数[0] + "看着不像整数"); 9 | return; 10 | } finally { 11 | System.out.println("彩蛋"); 12 | } 13 | } 14 | } -------------------------------------------------------------------------------- /识字循环类.java: -------------------------------------------------------------------------------- 1 | class 识字循环类 { 2 | public static void main (String[] 参数) { 3 | int 识字量 = 1; 4 | int 每年翻倍数 = 2; 5 | int 年限 = 10; 6 | int 年份 = 0; 7 | 8 | while (年份 < 年限) { 9 | if (识字量 > 3000) { 10 | break; 11 | } 12 | 识字量 = (1 + 每年翻倍数) * 识字量; 13 | 年份 = 年份 + 1; 14 | } 15 | System.out.println(年份 + "年后认识" + 识字量 + "个字"); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /投资回报循环类.java: -------------------------------------------------------------------------------- 1 | class 投资回报循环类 { 2 | public static void main (String[] 参数) { 3 | float 账户余额 = 1000f; 4 | float 回报率 = 0.08f; 5 | int 年限 = 20; 6 | int 年份 = 0; 7 | 8 | while (年份 < 年限) { 9 | if (账户余额 > 2000) { 10 | break; 11 | } 12 | 账户余额 = (1 + 回报率) * 账户余额; 13 | 年份 = 年份 + 1; 14 | } 15 | System.out.println(年份 + "年后变成" + 账户余额 + "元"); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /人测试类.java: -------------------------------------------------------------------------------- 1 | class 人测试类 { 2 | public static void main (String[] 参数) throws 听不懂例外 { 3 | 人类 无名 = new 人类("", -1); 4 | 应该相等("我记住了", 无名.回答("秘密哦"), "秘密应该记住"); 5 | 应该相等("你猜? 答案长度是3", 无名.回答("?"), "遇到问题就让猜秘密"); 6 | System.out.println("测试通过"); 7 | } 8 | 9 | private static void 应该相等(Object 期望值, Object 实际值, String 反馈信息) { 10 | assert 实际值.equals(期望值) : 反馈信息 + ", 但是实际值是: \"" +实际值 + "\"而期望值是: \"" + 期望值 + "\""; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /世界类.java: -------------------------------------------------------------------------------- 1 | class 世界类 { 2 | public static void main(String[] 参数) { 3 | 大人类 大白 = new 大人类("大白", 30); 4 | 小孩类 小白 = new 小孩类("小白", 5); 5 | 6 | 大白.自我介绍(); 7 | 大白.生活(); 8 | 9 | 小白.自我介绍(); 10 | 小白.长大(); 11 | try { 12 | System.out.println(小白.回答("这是咱俩的秘密")); 13 | System.out.println(小白.回答("你住哪里啊?")); 14 | System.out.println(小白.回答("%#^$#@$!")); 15 | } catch (听不懂例外 e) { 16 | System.out.println(e.getMessage()); 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /聊天机器人/聊天客户端类.java: -------------------------------------------------------------------------------- 1 | package 聊天机器人; 2 | 3 | import java.net.URL; 4 | 5 | import javax.xml.namespace.QName; 6 | import javax.xml.ws.Service; 7 | 8 | public class 聊天客户端类 { 9 | public static void main(String[] 参数) throws Exception { 10 | Service 服务 = Service.create(new URL("http://127.0.0.1:5335/service?wsdl"), 11 | new QName("http://聊天机器人/", "聊天机器人类Service")); 12 | 聊天接口 聊天接口 = 服务.getPort(聊天接口.class); 13 | 14 | // 待续: 参数检查 15 | System.out.println(聊天接口.回答(参数[0])); 16 | } 17 | } -------------------------------------------------------------------------------- /变量演示类.java: -------------------------------------------------------------------------------- 1 | class 变量演示类 { 2 | public static void main (String[] 参数) { 3 | boolean 年纪尚幼 = true; // true或false,真或假 4 | char 姓 = '好'; // 单个字符 5 | byte 年龄 = 27; // 字节: -128到127, 即-2^7到(2^7-1) 6 | short 认识人数 = 1234; // 短整数: -32768到32767, 即-2^15到(2^15-1) 7 | int 不认识人数 = 1234567890; // 整数: -2^31到(2^31-1) 8 | long 星球数 = 123456789000000000L; // 长整数: -2^63到(2^63-1) 9 | float 身高 = 1100000000f; // 单精度浮点数: 2^-126到(2-2^-23)*2^127 10 | double 体重 = 999999999999.9; // 双精度浮点数: 2^-1074到(2-2^-52)*2^1023 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /聊天机器人/聊天机器人类.java: -------------------------------------------------------------------------------- 1 | package 聊天机器人; 2 | 3 | import javax.jws.WebService; 4 | 5 | @WebService(endpointInterface = "聊天机器人.聊天接口") 6 | public class 聊天机器人类 implements 聊天接口 { 7 | private String 小秘密 = ""; 8 | 9 | @Override 10 | public String 回答(String 听到的) { 11 | System.out.println("我听到: " + 听到的); 12 | if (听到的.contains("?")) { 13 | return "你猜? 答案长度是" + 小秘密.length(); 14 | } else if (听到的.contains("秘密")) { 15 | 小秘密 = 听到的; 16 | return "我记住了"; 17 | } else { 18 | return "..."; 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /小九九类.java: -------------------------------------------------------------------------------- 1 | import java.util.HashMap; 2 | 3 | /** 4 | * 运行示例 5 | * $ java 小九九 6 4 6 | * 6乘4得24 7 | */ 8 | class 小九九类 { 9 | private static final HashMap 乘法表 = new HashMap<>(); 10 | 11 | // 构建整个乘法表, 包括反序, 比如2_8和8_2都有 12 | static { 13 | for (int 数一 = 1; 数一 < 10; 数一++) { 14 | for (int 数二 = 1; 数二 < 10; 数二++) { 15 | 乘法表.put(数一 + "" + 数二, 数一 * 数二); 16 | } 17 | } 18 | } 19 | 20 | public static void main(String[] 参数) { 21 | String 数一 = 参数[0]; 22 | String 数二 = 参数[1]; 23 | System.out.println(数一 + "乘" + 数二 + "得" + 乘法表.get(数一 + 数二)); 24 | } 25 | } -------------------------------------------------------------------------------- /人类.java: -------------------------------------------------------------------------------- 1 | public class 人类 { 2 | String 姓名 = "无名氏"; 3 | int 年龄 = 0; 4 | float 身高 = 0.0f; 5 | private String 小秘密 = ""; 6 | 7 | public 人类(String 姓名, int 年龄) { 8 | this.姓名 = 姓名; 9 | this.年龄 = 年龄; 10 | } 11 | 12 | public void 自我介绍() { 13 | System.out.println("我叫" + 姓名 + ", 今年" + 年龄 + "岁"); 14 | } 15 | 16 | public String 回答(String 听到的) throws 听不懂例外 { 17 | if (听到的.contains("%")) { 18 | throw new 听不懂例外(听到的); 19 | } else if (听到的.contains("?")) { 20 | return "你猜? 答案长度是" + 小秘密.length(); 21 | } else if (听到的.contains("秘密")) { 22 | 小秘密 = 听到的; 23 | return "我记住了"; 24 | } else { 25 | return "..."; 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /排队类.java: -------------------------------------------------------------------------------- 1 | class 排队类 { 2 | public static void main(String[] 参数) { 3 | 人类[] 一队 = { 4 | new 人类("小明", 14), 5 | new 人类("小红", 5), 6 | new 人类("大黄", 12), 7 | new 人类("阿牛", 9) 8 | }; 9 | 10 | 人类[] 二队 = new 人类[10]; 11 | 二队[0] = new 人类("阿狗", 11); 12 | 二队[1] = new 人类("阿猫", 10); 13 | // 2空着 14 | 二队[3] = new 人类("阿猪", 9); 15 | 16 | java.util.Arrays.sort(一队, new java.util.Comparator<人类>() { 17 | @Override 18 | public int compare(人类 甲, 人类 乙) { 19 | return 甲.年龄 - 乙.年龄; 20 | } 21 | }); 22 | 23 | for (int 序号 = 0; 序号 < 一队.length; 序号++) { 24 | if (一队[序号] != null) { 25 | 一队[序号].自我介绍(); 26 | } 27 | } 28 | } 29 | } -------------------------------------------------------------------------------- /排队买票类.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | 3 | class 排队买票类 { 4 | public static void main(String[] 参数) { 5 | 6 | // 创建时, 不用指定长度. 7 | // <人>的意思是它的内容是"人". 后面为什么只要<>呢? 因为内容已经限定为"人"了. 8 | ArrayList<人类> 队列 = new ArrayList<>(); 9 | 10 | 人类 小明 = new 人类("小明", 14); 11 | 人类 小红 = new 人类("小红", 5); 12 | 人类 大黄 = new 人类("大黄", 12); 13 | 人类 阿牛 = new 人类("阿牛", 9); 14 | 15 | // 三人先后排上了队 16 | 队列.add(小明); 17 | 队列.add(小红); 18 | 队列.add(大黄); 19 | 20 | System.out.println("小红在位置:" + 队列.indexOf(小红)); 21 | 22 | // 小明走了 23 | 队列.remove(小明); 24 | 25 | System.out.println("小明走后, 小红在位置:" + 队列.indexOf(小红)); 26 | 27 | // 阿牛插队, 到了阿牛前面, 小红的后面 28 | 队列.add(1, 阿牛); 29 | 30 | // 现在队是怎么排的? 31 | for(人类 谁 : 队列) { 32 | 谁.自我介绍(); 33 | } 34 | } 35 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## 共享协议 2 | 本作使用[署名-非商业使用-禁止演绎](https://creativecommons.org/licenses/by-nc-nd/4.0/)协议共享。 3 | 4 | # Java编程一天入门 v0.0.1 alpha 5 | 6 | ## 前言 7 | 8 | Java入门代码用中文写(举例如下)更能被新手理解. 由于至今没有看到类似教程, 在此抛砖引玉. 欢迎指正/批评/意见/建议. 9 | 10 | ```java 11 | public class 小孩类 extends 人类 { 12 | String 想做的事 = "大人的事"; 13 | 14 | public 小孩类(String 姓名, int 年龄) { 15 | super(姓名, 年龄); 16 | } 17 | 18 | public void 长大() { 19 | System.out.println("我要做" + 想做的事); 20 | } 21 | } 22 | ``` 23 | 24 | 编程语言的语法是最机械的, 在阅读过程中, 请尽量关注于程序做了些什么, 而一些语法细节可以暂时忽略. 入门之后, 在接下去的写和读代码过程中, 语法自然会熟练起来. 25 | 26 | 每一讲建议时间30分钟左右. 如果卡住(比如超过一小时), 欢迎在代码库[发问](https://github.com/program-in-chinese/java_in_hours_chn/issues). 目的是让总时间控制在8小时左右, 让"一天入门"更符合实际. 27 | 28 | ## 目录 29 | 一 [准备编程](#%E4%B8%80-%E5%87%86%E5%A4%87%E7%BC%96%E7%A8%8B) 30 | 31 | 二 [问个好吧](#%E4%BA%8C-%E9%97%AE%E4%B8%AA%E5%A5%BD%E5%90%A7) 32 | 33 | 三 [Java的现状](#%E4%B8%89-java%E7%9A%84%E7%8E%B0%E7%8A%B6) 34 | 35 | 四 [用Java算术](#%E5%9B%9B-%E7%94%A8java%E7%AE%97%E6%9C%AF) 36 | 37 | 五 [变量-在程序中保存修改信息](#%E4%BA%94-%E5%8F%98%E9%87%8F-%E5%9C%A8%E7%A8%8B%E5%BA%8F%E4%B8%AD%E4%BF%9D%E5%AD%98%E4%BF%AE%E6%94%B9%E4%BF%A1%E6%81%AF) 38 | 39 | 六 [文字](#%E5%85%AD-%E6%96%87%E5%AD%97) 40 | 41 | 七 [如果...就...不然...](#%E4%B8%83-%E5%A6%82%E6%9E%9C%E5%B0%B1%E4%B8%8D%E7%84%B6) 42 | 43 | 八 [直到...一直...](#%E5%85%AB-%E7%9B%B4%E5%88%B0%E4%B8%80%E7%9B%B4) 44 | 45 | 九 [造个人](#%E4%B9%9D-%E9%80%A0%E4%B8%AA%E4%BA%BA) 46 | 47 | 十 [让它更像人](#%E5%8D%81-%E8%AE%A9%E5%AE%83%E6%9B%B4%E5%83%8F%E4%BA%BA) 48 | 49 | 十一 [数据排排站-数组](#%E5%8D%81%E4%B8%80-%E6%95%B0%E6%8D%AE%E6%8E%92%E6%8E%92%E7%AB%99-%E6%95%B0%E7%BB%84) 50 | 51 | 十二 [更多结构](#%E5%8D%81%E4%BA%8C-%E6%9B%B4%E5%A4%9A%E7%BB%93%E6%9E%84) 52 | 53 | 十三 [活久见](#%E5%8D%81%E4%B8%89-%E6%B4%BB%E4%B9%85%E8%A7%81) 54 | 55 | 十四 [为人民服务](#%E5%8D%81%E5%9B%9B-%E4%B8%BA%E4%BA%BA%E6%B0%91%E6%9C%8D%E5%8A%A1) 56 | 57 | 十五 [自我肯定 - 测试](#%E5%8D%81%E4%BA%94-%E8%87%AA%E6%88%91%E8%82%AF%E5%AE%9A---%E6%B5%8B%E8%AF%95) 58 | 59 | 零 [没有规矩, 不成方圆 - 代码风格](#%E9%9B%B6-%E6%B2%A1%E6%9C%89%E8%A7%84%E7%9F%A9-%E4%B8%8D%E6%88%90%E6%96%B9%E5%9C%86---%E4%BB%A3%E7%A0%81%E9%A3%8E%E6%A0%BC) 60 | 61 | ## 一 准备编程 62 | 63 | 编程就是让计算机做你想让它做的事. 64 | 65 | 编程语言是工具,就像画笔,应该拿上手找块空白就可以用. 66 | 67 | 为了编写第一个Java程序,必需一个Java开发套件(本文代码测试用的是Oracle JDK 8),以及一个写程序的文本编辑工具. 68 | 本文的代码足够简单,集成开发环境的用处不大,任何文本编辑器都可以(推荐工具待定.写本文时用的是Komodo Editor免费版). 69 | 70 | 安装JDK后, 打开命令行窗口,运行javac和java,不报错"command not found",即为成功,可以继续. (待续:常见问题与解决) 71 | 72 | 扩展资料: 解释器与编译器的区别, JDK(Oracle JDK, OpenJDK), IDE(Eclipse, NetBeans, IntelliJ等等) 73 | 74 | ## 二 问个好吧 75 | 76 | 新建文本文件,命名为"问好.java".输入最简单的一个Java程序: 77 | 78 | ```java 79 | class 问好 { 80 | public static void main (String[] 参数) { 81 | // 待续: 要让它做的事 82 | } 83 | } 84 | ``` 85 | 86 | 这个程序定义了一个类(class),名叫"问好",文件名一般与类名相同. 这个类就是一个程序. 里面的main是程序入口. 注意所有的大括号都需要配对. 双斜杠"//"之后的是注释,是为读代码的人方便理解写的,不影响编译运行. 87 | "参数"很扎眼吧,不用急,第四讲就知道它做什么了. 88 | 89 | 这个程序可以编译运行(见"手把手"部分),但运行后没有任何输出.因为这个程序是个空架子,没有任何可以看到的运行结果.下面就让它做点事. 90 | 91 | ```java 92 | class 问好 { 93 | public static void main (String[] 参数) { 94 | // 要让它做的事 95 | System.out.println("吃了么"); 96 | } 97 | } 98 | ``` 99 | 100 | 加上的这行代码将打印一行字,内容是"吃了么". 101 | 102 | 试试编译运行,将看到命令行下输出: 103 | ``` 104 | 吃了么 105 | ``` 106 | 107 | 试试改字符串的内容,再编译运行.恭喜! 你已经可以写出无数个不同的Java程序了. 108 | 再试试加一行相同的代码,输出结果变了吗? 恭喜! 你已经可以写出无限长的Java程序了. 109 | 110 | ### 手把手: 111 | 在命令行下编译和运行 112 | #### 编译: 113 | 在程序文件的目录下,运行下面的命令 114 | ``` 115 | $ javac 问好.java 116 | ``` 117 | 此命令将程序文件编译生成.class文件,在这个目录下多了一个"问好.class"文件 118 | 119 | 注: 在Windows下, 如果报错"unmappable character for encoding GBK", 请加编码参数: 120 | ``` 121 | $ javac -encoding utf8 问好.java 122 | ``` 123 | #### 运行 124 | 下面的命令寻找并运行叫"问好"的类: 125 | ``` 126 | $ java 问好 127 | ``` 128 | 129 | ## 三 Java的现状 130 | 在更进一步之前,最好了解现在Java都用来做什么. 131 | 132 | 优点: 133 | - Oracle JDK是开源的, 另有一个社区维护的版本OpenJDK也是. 134 | - 程序员用户群很大, 能碰到的问题基本上都被前人趟过雷了. 135 | - 可以用的成熟的经过时间检验的库很多. 136 | 137 | 用途: 138 | - 很大一部分网络服务 139 | - 大多数安卓手机应用 140 | - 少量游戏和桌面应用 141 | - 一些企业内部用Java Applet做可以嵌入网页的在线工具. Chrome浏览器已不支持Java Applet,原因之一是安全性 142 | 143 | 扩展资料: Apache Maven, Java Applet 144 | 145 | ## 四 用Java算术 146 | 147 | 新建文件"四则运算.java" 148 | ```java 149 | class 四则运算 { 150 | public static void main (String[] 参数) { 151 | System.out.println(1+2); 152 | } 153 | } 154 | ``` 155 | 编译运行后,果然输出3. 再试试其他四则运算吧,加减乘除运算符分别是+-*/. 156 | 还有括号也可以用. 注: 如果算式中所有的数都是整数,那么每步运算都会取整 157 | 158 | 恭喜! 你已经可以用Java程序完成数学运算了. 159 | 160 | 那么其他的运算呢? 新建文件"根号.java" 161 | ```java 162 | class 根号 { 163 | public static void main (String[] 参数) { 164 | System.out.println(Math.sqrt(4)); 165 | } 166 | } 167 | ``` 168 | 看起来告诉程序的值是4,编译运行后, 果然如愿打印出了2.0. Math.sqrt是Java中开根号的方法. 169 | 应该不用啰嗦了,试试把4改成其他的数,看看结果如何? 170 | 171 | 现在,你可能已经觉得程序的"回答"太"精简"和生硬了,那么人性化一些吧,下面开始只列出main方法内的代码 172 | ```java 173 | System.out.println("4的平方根是" + Math.sqrt(4)); 174 | ``` 175 | 输出听起来顺耳些了,但如果想要把4改成其他数,需要改程序的两个地方,这种麻烦可要不得! 可以把4先存到一个变量里,然后在两处引用同一个变量: 176 | ```java 177 | int 数 = 4; 178 | System.out.println(数 + "的平方根是" + Math.sqrt(数)); 179 | ``` 180 | 这样只要改一处了.不过,为了改输入值,还是要改程序,再编译再运行,这种麻烦可要不得! "参数"终于派上用场了. 181 | ```java 182 | int 数 = Integer.parseInt(参数[0]); 183 | System.out.println(数 + "的平方根是" + Math.sqrt(数)); 184 | ``` 185 | "参数[0]"是"参数"数组的第一个值. Integer.parseInt是Java把字符串转换成整数的方法. 186 | 现在代码里没有了输入值,该怎样告诉程序需要给什么数开根号呢? 187 | 在运行程序时,命令后加上一个"参数": 188 | ``` 189 | $ java 根号 4 190 | ``` 191 | 如果忘了在运行时加参数, 这个程序会打印一个异常报告: java.lang.ArrayIndexOutOfBoundsException. 192 | 意思是:数组是空的,却要取第一个值,没辙. 193 | 194 | 试试多加几个参数吧, 参数[1]是"参数"数组第二个值,以此类推. 195 | 恭喜! 你的程序不用修改代码就可以接受不同的外部输入了. 196 | 197 | Math是Java自带标准库中的数学类,包含很多有用的方法.详细请查阅JDK文档. 198 | 199 | 标准库有很多有用的类. 比如随机数, 用在很多聊天机器人上. 200 | 新建"随机数生成器.java": 201 | ```java 202 | class 随机数生成器 { 203 | public static void main (String[] 参数) { 204 | java.util.Random 生成器 = new java.util.Random(); 205 | System.out.println("我想到的数字是:" + 生成器.nextInt()); 206 | } 207 | } 208 | ``` 209 | java.util.Random是随机数类的全路径, java.util是它所在的包. 没有全路径Java就找不到这个类了. 210 | 为什么Math和Integer没有这样的前缀呢? 因为他们在java.lang包里,是"亲生"的,不用包名Java也能找到这些类. 211 | 212 | "生成器"是随机数类的一个"个体". 用new关键词来产生. 一个现实的比方: "人"是一个类型, 你我都是同样类型的不同个体. 213 | nextInt是产生一个随机数的方法. 为什么Math.sqrt和Integer.parseInt不用new出一个个体呢? 因为它们和main方法一样, 都是静态(static)的. 214 | 215 | 这样重复类的全名看着真累, 下面用import来开头导入这个类路径, 之后就不用再重复了: 216 | ```java 217 | import java.util.Random; 218 | 219 | class 随机数生成器 { 220 | public static void main (String[] 参数) { 221 | Random 生成器 = new Random(); 222 | System.out.println("我想到的数字是:" + 生成器.nextInt()); 223 | } 224 | } 225 | ``` 226 | 227 | 扩展资料: 数组, 异常, 方法, JDK文档 228 | 229 | ## 五 变量-在程序中保存修改信息 230 | 231 | 在上一讲的"根号"类中,用了一个整数(int)变量来保存输入值. "参数"是一个字符串(String)数组. 232 | Java中还有其他几种基本变量: boolean, char, byte, short, long, float, double 233 | ```java 234 | boolean 年纪尚幼 = true; // true或false,真或假 235 | char 姓 = '好'; // 单个字符 236 | byte 年龄 = 27; // 字节: -128到127, 即-2^7到(2^7-1) 237 | short 认识人数 = 1234; // 短整数: -32768到32767, 即-2^15到(2^15-1) 238 | int 不认识人数 = 1234567890; // 整数: -2^31到(2^31-1) 239 | long 星球数 = 123456789000000000L; // 长整数: -2^63到(2^63-1) 240 | float 身高 = 1100000000f; // 单精度浮点数: 2^-126到(2-2^-23)*2^127 241 | double 体重 = 999999999999.9; // 双精度浮点数: 2^-1074到(2-2^-52)*2^1023 242 | ``` 243 | 它们的范围逐渐增大,可以根据需要选择. 长整数后如果不加'L'(大写的l),会被默认为整数值. 244 | 245 | 上一讲的四则运算类中,已经尝试了4种运算符. 变量运算的结果可以赋给自己,或者另一个变量. 246 | 247 | 举个例子, 如2岁的时候认识一个字,每年增加两倍, 3年之后会变成多少.下面是一个很直白的计算方法: 248 | ```java 249 | class 识字类 { 250 | public static void main (String[] 参数) { 251 | int 识字量 = 1; 252 | int 每年增倍数 = 2; 253 | 254 | // 第一年 255 | 识字量 = (1 + 每年增倍数) * 识字量; 256 | 257 | // 第二年 258 | 识字量 = (1 + 每年增倍数) * 识字量; 259 | 260 | // 第三年 261 | 识字量 = (1 + 每年增倍数) * 识字量; 262 | System.out.println("三年后认识" + 识字量 + "个字咯"); 263 | } 264 | } 265 | ``` 266 | 你的感觉没错, 它看起来就很累赘, 而且如果要算10年后呢? 267 | 268 | 恭喜! 你已经有了判断代码优劣的直觉. 至于改进方法,留个悬念吧. 269 | 270 | ## 六 文字 271 | 272 | 之前的程序都用文字的形式"回答"结果. 就像现实世界一样, 文字是最经典基本的人机交流方式. 为此Java提供了很多文本处理的方法. 273 | 274 | 第一讲中的"吃了么"是一个字符串(String). 它由三个字符(char)组成: '吃','了','么'. 275 | 注意在定义变量时字符用单引号,而字符串用双引号. 就像上一讲的浮点数后的f和长整数后的L一样, 这些都是Java的"传统". 考虑到Java诞生在上世纪90年代初,就配合一下吧. 276 | 277 | 可能已经注意到String开头是大写的,没错,和其他基本变量类型不同,它是一个类. 278 | 279 | 第一讲中也许已经试过了多个System.out.println,每个会打出一行. 如果不想另起一行, 用System.out.print就行. 280 | 281 | 既然用双引号包起来的就是字符串,那么如果想在字符串里显示双引号,该怎么办呢? 这需要加一个反斜杠: \\" 282 | 283 | 那么反斜杠又是个特殊符号了, 如果要显示它, 就需要再加一个: \\\\ 284 | 类似的还有\t(制表符), \n(换行)等等. 如果将来有一个想不出怎么显示的东西, 再找本工具书看看Java特殊字符部分吧. 下面的程序演示一些: 285 | ```java 286 | class 特殊字符 { 287 | public static void main (String[] 参数) { 288 | System.out.println("边检员看了看证件,头没抬地说\t\"这么久没回了啊?\".\n百感交集,咧着嘴回了一句\t\"是啊,还没完呢\""); 289 | } 290 | } 291 | ``` 292 | 前几讲已经用过加号连接多个字符串,以及其他类型的变量. 只要是基本变量,都可以这样和字符串用加号连接,产生一个新的字符串. 293 | 294 | 字符串有不少常用方法,比如获取长度,搜索子字符串,变换英文大小写等等.下面演示他们的用法: 295 | ```java 296 | class 字符串方法 { 297 | public static void main (String[] 参数) { 298 | String 字符串 = "去是go"; 299 | String 搜索字符串 = "go"; 300 | System.out.println("\"" + 字符串 + "\"的长度:" + 字符串.length()); 301 | System.out.println(搜索字符串 + "在\"" + 字符串 + "\"的位置是:" + 字符串.indexOf(搜索字符串)); 302 | System.out.println("\"" + 字符串 + "\"的第一个字符是:" + 字符串.charAt(0)); 303 | } 304 | } 305 | ``` 306 | 扩展资料: 类型转换 307 | 308 | ## 七 如果...就...不然... 309 | 代码说了算: 310 | ```java 311 | if (年龄 < 20) { 312 | System.out.println("没到法定婚龄! 等几年再结婚吧"); 313 | } else { 314 | System.out.println("妹妹,真想嫁也拦不住你.要不再考虑一天?"); 315 | } 316 | ``` 317 | if就是"如果",后面跟的是条件, 紧接着的{}在条件满足时执行; else就是"不然",紧接着的{}在之前的条件不满足时执行. 318 | 没错, {}里当然可以有多行代码. 然后在if里套if试试? 319 | 320 | Java支持所有数学中的大小比较符号: < > >= <= 321 | 322 | 另外, 因为单个=被用于变量赋值, 判断"等于"就用了双等号: == 不等于呢? != 323 | 324 | 如果有并列的多个条件,可以串起来这样写: 325 | ```java 326 | if (年龄 < 5) { 327 | System.out.println("这是哪家闺女啊?爸妈在哪儿呢?"); 328 | } else if (年龄 < 20) { 329 | System.out.println("没到法定婚龄! 等几年再结婚吧"); 330 | } else { 331 | System.out.println("妹妹,真想嫁也拦不住你.要不再考虑一天?"); 332 | } 333 | ``` 334 | 如果把 <5 和 <20的顺序倒过来: 335 | ```java 336 | if (年龄 < 20) { 337 | System.out.println("没到法定婚龄! 等几年再结婚吧"); 338 | } else if (年龄 < 5) { 339 | System.out.println("这是哪家闺女啊?爸妈在哪儿呢?"); 340 | } else { 341 | System.out.println("妹妹,真想嫁也拦不住你.要不再考虑一天?"); 342 | } 343 | ``` 344 | 即使是3岁的小朋友也满足<20的条件, 因此会执行输出"没到法定婚龄! 等几年再结婚吧". 345 | 是的,计算机执行程序就是这么老(si)实(ban), 执行第一个被满足的条件之后的{}内代码, 而且无视后面所有else的条件判断. 346 | 347 | 注意: 不同于数值的比较方法, 字符串的"等于"判断有自己的方法equals, 比大小用compareTo: 348 | ```java 349 | if ("辛苦".equals("不辛苦")) { 350 | System.out.println("辛不辛苦无所谓"); 351 | } else if ("辛苦".compareTo("不辛苦") > 0){ 352 | System.out.println("辛苦点好"); 353 | } else { 354 | System.out.println("不辛苦好"); 355 | } 356 | ``` 357 | 你猜上面程序输出的是什么呢? 358 | 359 | 扩展资料: &&, ||, switch, ?:, 字符串比较 360 | 361 | ## 八 直到...一直... 362 | 记得算识字量的程序么? 如果要算10年, 难道必须重复10行`识字量 = (1 + 每年翻倍数) * 识字量;`吗? 用脚趾想也不可能吧. 363 | 364 | 在写代码之前, 不妨先构思一下该怎么算. 这里多了一个输入值: 年限. 照原来的思路应该是: 每过一年增加一次识字量, 直到过了10年. 这样就需要记着过了多少年. 之前提到, 一个变量用来"记"变化的值最合适: 365 | ```java 366 | for (int 年份 = 0; 年份 < 年限; 年份 = 年份 + 1) { 367 | 识字量 = (1 + 每年翻倍数) * 识字量; 368 | } 369 | ``` 370 | 上面的代码反映了我们的思路: 371 | - 1 变量"年份"初始值是0 372 | - 2 如果它小于10, 说明没到10年, 那么就运行{}的内容, 增一次识字量; 如果到了10年, 就结束循环 373 | - 3 "年份"加一, 继续执行第2步 374 | 375 | 同样的循环用while的格式来写是这样: 376 | ```java 377 | int 年份 = 0; 378 | while (年份 < 年限) { 379 | 识字量 = (1 + 每年翻倍数) * 识字量; 380 | 年份 = 年份 + 1; 381 | } 382 | ``` 383 | 看起来for循环更紧凑, 也更不容易写错. while循环里,如果忘写了"年份 = 年份 + 1;",可就有趣了,因为年份没有增加, 循环中止条件一直不能满足(0永远小于年限), 代码运行停不下来,俗称"死循环". 而for循环里因为定了"(初始化; 循环条件; 累加或递减执行语句)"的格式, 少了一项会很扎眼. 384 | 385 | 如果想要提前结束循环,可以用break. 想知道过几年能认识中文的常用三千字的话: 386 | ```java 387 | int 年份 = 0; 388 | while (年份 < 年限) { 389 | if (识字量 > 3000) { 390 | break; 391 | } 392 | 识字量 = (1 + 每年翻倍数) * 识字量; 393 | 年份 = 年份 + 1; 394 | } 395 | System.out.println(年份 + "年后认识" + 识字量 + "个字"); 396 | ``` 397 | break执行后,它所在的循环就被打断,程序从循环之后开始执行. 398 | 399 | 如果想要循环继续执行,但是跳过循环内的部分代码,可以用continue. 一个牵强的例子,如果从第三年才开始认识新字(比如在国外呆了三年): 400 | ```java 401 | for (int 年份 = 0; 年份 < 年限; 年份 = 年份 + 1) { 402 | if (年份 < 3) { 403 | continue; 404 | } 405 | 识字量 = (1 + 每年翻倍数) * 识字量; 406 | } 407 | ``` 408 | 注: 有更简短的实现方法, 这个例子只为了演示continue的用处. 409 | 恭喜! 至此控制流介绍完了. 410 | 411 | 扩展资料: 变量初始值, 作用域, ++, --, do...while, 递归(例子见:递归死) 412 | 413 | ## 九 造个人 414 | 415 | 我们都是人类,每个人都是一个个体,大多数人有共有的属性和行为,同时也存在个体之间的差异. 416 | 下面就来在程序里定义一个"人"类: 417 | ```java 418 | public class 人 { 419 | } 420 | ``` 421 | 这样的"人"还什么都做不了. 我们出生后都有姓名,那么它也应该有: 422 | ```java 423 | public class 人 { 424 | String 姓名 = "无名氏"; 425 | 426 | public void 自我介绍() { 427 | System.out.println("我叫" + 姓名); 428 | } 429 | } 430 | ``` 431 | 这个类具有了"姓名"属性, "自我介绍"方法引用了这个属性并输出加工后的回答. 432 | class前的public表示"人"可以在其他类里使用. 比如这个"世界"类里, "我"是"人"类的一个个体: 433 | ```java 434 | class 世界 { 435 | public static void main(String[] 参数) { 436 | 人 我 = new 人(); 437 | 我.自我介绍(); 438 | } 439 | } 440 | ``` 441 | 不过,应该有个像样的名字,而不是默认的"无名氏". 需要在自我介绍之前,先定名字: 442 | ```java 443 | 我.姓名 = "小白"; 444 | ``` 445 | 编译运行"世界"后,可以看到输出. 446 | 447 | 这个世界好像太单调了,人有不同分类,大人,小孩等等,他们做不同的事.新建"大人"类: 448 | ```java 449 | public class 大人 extends 人 { 450 | String 责任 = "扶老携幼"; 451 | 452 | public void 生活() { 453 | System.out.println("我必须" + 责任); 454 | } 455 | } 456 | ``` 457 | 再新建"小孩"类: 458 | ```java 459 | public class 小孩 extends 人 { 460 | String 想做的事 = "大人的事"; 461 | 462 | public void 长大() { 463 | System.out.println("我要做" + 想做的事); 464 | } 465 | } 466 | ``` 467 | 现在的世界要喧闹一些了: 468 | ```java 469 | class 世界 { 470 | public static void main(String[] 参数) { 471 | 大人 大白 = new 大人(); 472 | 小孩 小白 = new 小孩(); 473 | 474 | 大白.姓名 = "大白"; 475 | 大白.自我介绍(); 476 | 大白.生活(); 477 | 478 | 小白.姓名 = "小白"; 479 | 小白.自我介绍(); 480 | 小白.长大(); 481 | } 482 | } 483 | ``` 484 | "大人"和"小孩"都是"人"的扩展类(俗称"子类"), 他们也可以有自己的"子类",比如"婴儿"可以是"小孩"的子类. 485 | 486 | 扩展资料: 接口(interface) 487 | 488 | ## 十 让它更像人 489 | 490 | 一个人还有很多属性: 491 | ```java 492 | public class 人 { 493 | String 姓名 = "无名氏"; 494 | int 年龄 = 0; 495 | float 身高 = 0.0f; 496 | private String 小秘密 = ""; 497 | 498 | public void 自我介绍() { 499 | System.out.println("我叫" + 姓名 + ", 今年" + 年龄 + "岁"); 500 | } 501 | } 502 | ``` 503 | 谁的小秘密都不可以直接让别人知道. private就限制了这个变量只能给个体内部使用, 任何其他类里,都不能直接获取这个值. 下面这个程序在编译会报错: 504 | ```java 505 | class 世界 { 506 | public static void main(String[] 参数) { 507 | 人 我 = new 人(); 508 | System.out.println(我.小秘密); 509 | } 510 | } 511 | ``` 512 | 既然这个小秘密只能由自己引用和修改, 一般有公开方法可以让其他类间接接触这个变量: 513 | ```java 514 | public String 回答(String 听到的) { 515 | if (听到的.contains("?")) { 516 | return "你猜? 答案长度是" + 小秘密.length(); 517 | } else if (听到的.contains("秘密")) { 518 | 小秘密 = 听到的; 519 | return "我记住了"; 520 | } else { 521 | return "..."; 522 | } 523 | } 524 | ``` 525 | 根据"听到的"内容, 如果里面包含问号, 就提示秘密的字符串长度, 让猜秘密. 如果包含"秘密"两个字,就把它存在"小秘密"变量里. 再不然,就...了. 526 | 527 | 这个方法返回(return)一个字符串. 可以在"世界"类里打印出每个回答. 528 | 529 | 在创建个体的时候, 之前都是new xxxx(), 没有传入任何参数. 因此如果在创建后对属性初始化就需要这样做: 530 | ```java 531 | 大人 大白 = new 大人(); 532 | 大白.姓名 = "大白"; 533 | 大白.年龄 = 30; 534 | ``` 535 | 另一种比较简洁的方法是, 在"大人"类里定义一个带参数的创建方法: 536 | ```java 537 | public 大人(String 姓名, int 年龄) { 538 | this.姓名 = 姓名; 539 | this.年龄 = 年龄; 540 | } 541 | ``` 542 | 然后在创建"大人"个体时,就可以这样: 543 | ```java 544 | 大人 大白 = new 大人("大白", 30); 545 | ``` 546 | 同样可以在"小孩"类里定义一个类似的创建方法. 黏贴复制的很愉快吧? 不过每当这样愉快的时候,就需要警惕一下,因为重复的代码往往意味着设计问题,而且很可能增加今后代码维护的难度. 一个不成文的经验是, 重复代码越少越好. 547 | 548 | 一个思路是, 大人和小孩都是"人",那么这个内容相同的创建方法理应由"人"来定义. 然后"大人"和"小孩"只要引用它就可以了. 549 | 具体实现请参考super关键词. 550 | 551 | 扩展资料: private/protected/public, static, final, set/get 552 | 553 | ## 十一 数据排排站-数组 554 | 555 | 第四讲里, 已经用过了"参数"这个字符串数组. 下面我们用数组给人排队: 556 | ```java 557 | class 排队 { 558 | public static void main(String[] 参数) { 559 | 人[] 一队 = { 560 | new 人("小明",14), 561 | new 人("小红", 5), 562 | new 人("大黄", 12), 563 | new 人("阿牛", 9) 564 | }; 565 | 566 | for (int 序号 = 0; 序号 < 一队.length; 序号++) { 567 | 一队[序号].自我介绍(); 568 | } 569 | } 570 | } 571 | ``` 572 | "人"是单个的人, 多加一对方括号"人[]"就成了一队人(再加一对[]呢?). "一队"是个长度为4的数组. 它的length属性就是它的长度. 在它初始化时, 长度就已经确定了,而且之后不能修改. 之前我们用过"参数[0]"获得"参数"数组的第一个值. 同样在这里可以用从0到(一队.length-1)的变量"序号"来获取数组里的每个值. 573 | 574 | 注意: 数组的序号是从0开始的, 这是比Java年纪还大的一个老传统, 再配合一下吧. 575 | 576 | 数组还有另一种初始化方法: 577 | ```java 578 | 人[] 二队 = new 人[10]; 579 | 二队[0] = new 人("阿狗", 11); 580 | 二队[1] = new 人("阿猫", 10); 581 | // 2空着 582 | 二队[3] = new 人("阿猪", 9); 583 | ``` 584 | 很直白, 一开始是初始化一个长度是10的数组, 之后就是往数组中的指定位置放个体. 585 | 586 | 如果对二队按照一队的方式来"报数", 会报错NullPointerException,因为位置2还空着. 这时需要加个"不为空"的判断条件: 587 | ```java 588 | for (int 序号 = 0; 序号 < 二队.length; 序号++) { 589 | if (二队[序号] != null) { 590 | 二队[序号].自我介绍(); 591 | } 592 | } 593 | ``` 594 | 排了队, 下面就试试按照某个属性排序. 比如要按年龄对一队排序. 大略的思路可能是: 比较相邻的两人年纪,谁小就排在前面. 下面是Java对应这种思路的一种程序: 595 | ```java 596 | java.util.Arrays.sort(一队, new java.util.Comparator<人>() { 597 | @Override 598 | public int compare(人 甲, 人 乙) { 599 | return 甲.年龄 - 乙.年龄; 600 | } 601 | }); 602 | ``` 603 | Arrays和Comparator都是java.util包里的类. 如果嫌这样不美观可以在程序前import这两个类. 604 | 605 | 如果想要排个方阵呢? 只要再加一对[]就可以了: 606 | ```java 607 | 人[][] 方阵 = new 方阵[10][15]; 608 | 方阵[0][0] = new 人("阿狗",3); 609 | 方阵[2][4] = new 人("阿猫",4); 610 | ``` 611 | 612 | 扩展资料: 排序算法, 泛型 613 | 614 | ## 十二 更多结构 615 | 616 | 数组已经可以做很多事了, 但它的局限在于长度是不可变的. 上一讲的一队里只能"换人",不能加进第五个人了, 而且,即使把位子空出来,"队伍"的长度也还是四. 617 | 618 | 当然有变通的办法, 比如: 上来就建个够大的,在90%的问题里,可能一百万就够大了,反正内存现在都是几个G的. 或者灵活点, 满了的时候就新建一个更大的数组, 把原本的数据"挪"到新数组里. 很显然, 这两种做法都有前人实践了. 后者明显是更普遍适用的, 于是这套做法在Java 1.2版里(现在有1.9了)就催生了标准库的java.util.ArrayList类. 619 | 620 | 注: 知道这个由来不是因为我是考古专业,也不是信口胡诌. 记得之前说过JDK是开源的吗? Java标准库当然也是JDK的一部分, 有兴趣的可以看看ArrayList.add方法的实现. 621 | 622 | "排队买票.java"演示了它的基本操作. 如果注释还不够清楚的话, 请在代码库开issue质疑. 623 | 624 | 另一种数据存取方式是根据一类数据,查找另一类数据. 比如说, 九九乘法表, 就是从两个数找对应的结果. 四六二十四可以表示成"46"->24, 二八十六:"28"->16 625 | 626 | 这种从一种值查询另一种值的情况, 可以考虑哈希表. 示例在"小九九.java". 627 | 628 | 当然根据思路不同, 一个问题可以用各种不同的结构解决, 比如乘法表用方阵(数组)也合适. 629 | 630 | 算法和数据结构是程序员面试很喜欢的问题, 因为它很接近数学和建模. 不过实际工作中, 真正需要实现新算法或者改进现有算法的编程工作比例很有限. 即使厌恶数学, 编程也可以做很多很多不需要小学以上数学知识和方法的事情. 当然, 难免有一天会有书到用时方恨少的感觉. 用画画作比方, 二岁的小孩也可以, 毕其一生追求极致也有. 631 | 632 | 祝涂鸦愉快. 633 | 634 | 扩展资料: 内存空间占用, 队列(queue), 栈(stack), 树(tree), 图(graph) 635 | 636 | ## 十三 活久见 637 | 638 | 第四讲里, 用到了Integer.parseInt方法. 如果参数输错, 比如"啊呜",运行后会打印出一堆诡异的东西, 其中有NumberFormatException, 也提到"啊呜". 不用懂英文也能猜到这个输入有点问题了. 639 | 640 | 这个NumberFormatException属于Exception, 俗语叫"异常", 但直译是"例外", 感觉后者的字面意思明确些. 比如这个把字符串转换成整数的方法, "啊呜"对它来说就是个束手无策的例外情况, 拿到手里既然处理不了就会把它"丢"出来. 丢出来就得"接住", 不然像前面那样就砸了. 下面的try ... catch 就是"试试看...接住(例外情况)...": 641 | ```java 642 | int 数 = 0; 643 | try { 644 | 数 = Integer.parseInt(参数[0]); 645 | System.out.println(数 + "的平方根是" + Math.sqrt(数)); 646 | } catch (NumberFormatException e) { 647 | System.out.println(参数[0] + "看着不像整数"); 648 | return; 649 | } finally { 650 | System.out.println("彩蛋"); 651 | } 652 | ``` 653 | 最后的finally里面可以写无论例外情况还是正常情况都需要运行的内容. "这和不用finally,直接放在后面有啥区别?" 654 | 655 | 试试不用finally包着运行一个例外情况就知道了. catch里虽然有return, 但程序退出之前还是运行了"彩蛋". 656 | 657 | 在"人类.回答"方法里丢了一种新建的Exception"听不懂例外". 具体请看"人类","听不懂例外",以及"世界类"中如何接住这个例外并处理的. 658 | 659 | 扩展资料: Error, (Un)checkedException 660 | 661 | ## 十四 为人民服务 662 | 663 | 在"聊天机器人"目录中,是一个很简单的聊天机器人和客户端的实现. 机器人回答和第十讲相同. 尚欠细节说明. 664 | 665 | 编译运行服务器: 666 | ``` 667 | $ javac 聊天机器人/聊天服务类.java 668 | $ java 聊天机器人/聊天服务类 669 | 服务启动在:http://127.0.0.1:5335/service 670 | ``` 671 | 运行客户端: 672 | ``` 673 | $ javac 聊天机器人/聊天客户端类.java 674 | $ java 聊天机器人/聊天客户端类 秘密哦 675 | 我记住了 676 | $ java 聊天机器人/聊天客户端类 你是谁? 677 | 你猜? 答案长度是3 678 | ``` 679 | 680 | 扩展资料: JAX-WS, SOAP, REST 681 | 682 | ## 十五 自我肯定 - 测试 683 | 684 | 至今为止, 所有程序的正确性都是靠运行之后看打印输出来判断的. 如果要测试"人类.回答"方法, 该怎么做呢? 685 | 686 | 一个很自然的想法是, 看看这个方法对不同输入值返回的结果吧, 我们驾轻就熟的System.out.println出马, 请看代码"原始人测试类". 687 | 688 | 这样可以比较一目了然地看到返回结果, 然后脑子里想想, 好像是没错, 测试通过咯. 但是, 想象一下, 如果每次改动这个方法, 就要运行一次测试, 就需要动脑子想想期望的返回值该是多少, 然后盯着屏幕比较每个字符串. 这种麻烦怎么能够重复第二次呢?? 689 | 690 | 为了省得在脑子里记得期望的返回值, 把它们记在程序里就好. 实现在"人测试类"里, 而且通过使用assert语句, 使测试更简洁了些. 691 | 692 | 注: 为使assert语句有效, 运行时需要加命令行参数-enableassertions, 不然就会被无视: 693 | ``` 694 | $ java -enableassertions 人测试类 695 | ``` 696 | 697 | "应该相等"这个方法可以普遍适用于其他需要测试等值的地方. 可以想见,这么常用的方法必然早就有人贡献了库, 省得大家日后造轮子了. JUnit就是其中一个. 它提供了一系列辅助方法, 使测试更加方便. 698 | 699 | 扩展资料: JUnit, TDD, Mock/Stub 700 | 701 | ## 零 没有规矩, 不成方圆 - 代码风格 702 | 703 | 即使只看一眼, 有个印象就好. 写了一段时间代码之后重温这里也许会有另外的感触. 704 | 705 | 到现在为止, 代码的写法好像没有什么讲究. 是吗? 回顾一下(注: 凡是规矩必有例外): 706 | 707 | 1 源码文件 708 | - 文件名和类名相同 709 | - UTF-8编码格式 710 | - 特殊字符: 所有留白(缩进,分隔),都是空格组成的,而不是tab 711 | 712 | 2 源码结构 713 | - 顺序是: 包声明, 导入语句, 唯一一个类声明 714 | - 导入语句用全路径, 避免使用通配符*. (好像没提过: import java.util.*就是导入所有java.util包下的类. 这样一眼看不出到底导入了哪些类) 715 | - 导入语句的排序. 按照路径名的字符串大小排序, 小的在前面 (比如: "java.util.HashMap"<"java.util.Map"<"java.util.胡诌类") 716 | - 写类文件的时候, 至少, 不能每次新加内容都写在文件最后. 一种规则是,把同类型的语句和代码块放在一起. 比如: 常量在一起,变量在一起,方法在一起,等等. 还有其他"维度"的类型, 比如公有/私有: 公有方法在一起, 私有方法在一起, 等等. 717 | 718 | 3 语句格式 719 | - {} 就算可以省略, 也写上 (不然更容易犯低级错误, 信不信由你) 720 | - {前不换行,之后换行; }前换行, 之后如果不跟else或者;的话就换行 721 | - 一层缩进是两个空格 722 | - 一行最多一条语句 723 | - 一行语句超过100(?)个字符就分行 (英文代码中超过100读着就太长了,中文代码在表达同样意思时会用更少字符,所以也许100不再恰当了) 724 | - 如何分行呢? 看着顺眼就好 (超出了"入门"的范畴, 改天再详说) 725 | - 空行可以让代码分块显眼. 之前提到的不同类型的语句就可以用空行隔开,比如变量和方法之间. 另外两个方法声明之间也是. 726 | - 空格也可以让代码读起来更容易, 嗯, 再次省去100+字. 727 | - 本地变量(在方法中声明的变量)不到用时不声明 (好处之一是重构起来更方便) 728 | - 数组类型的变量声明时,[]不放在变量名之后 ("人类[] 一队" 和 "人类 一队[]"的语法都允许,但前者看起来更明显是数组) 729 | - switch中, 每个case占单独一行, 如果某个case块有语句, 并且不是以break结束, 需要加上显眼的注释, 表示这是故意而为之的, 比如: // 继续 (等到哪天在switch的一个case忘了break,可能就更能明白用意了) 730 | - 除非case已经包含了所有情况, switch末的default尽量加上, 即使不用处理. (总是想到例外情况,是编程的好习惯) 731 | - Modifier排序: public protected private abstract default static final transient volatile synchronized native strictfp 732 | - 长整型用大写L作后缀,以免小写的l和1太相似 733 | 734 | 4 命名 735 | - 包名用小写英文或中文, 不用下划线 736 | - 类名尽量用名词或名词短语. 用"类"结尾, 便于和一般变量名区别. 测试类用"测试类"结尾. (示例代码的类已经重命名) 737 | - 接口(interface)名用"接口"结尾 738 | - 方法命名尽量用动词开头 739 | - 常量命名以"常量_"作前缀, 注意只用在"真"常量上, 就是初始化后不能被改变的量. 740 | - 类型变量 (待续) 741 | 742 | 上述代码风格摘自Google Java英文代码风格指南, 加入了一点中文代码相关内容. 删去的列了一些在下面(不完整). 请自行取舍: 743 | - 包和导入语句都是单行 (是的,可以分行写) 744 | - 一个变量一个声明 (int 数一, 数二; 语法允许) 745 | - 英文变量/方法/类名采用骆驼命名法 746 | 747 | 这么多的规则, 如果没有辅助会增加很多额外负担. 好在现在的集成开发环境(IDE)基本都有根据模板进行代码自动格式化的功能. 善其事, 利其器. 请自行选择一个好用的Java集成开发环境. 748 | 749 | 江湖再见. 750 | 751 | ### (全文完) 752 | 753 | 待定: 754 | 755 | 分身有术-多线程 756 | 757 | 文字到图形 758 | 759 | 读写文件 760 | 761 | 内部类, closure, lambda... 762 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | {one line to give the program's name and a brief idea of what it does.} 635 | Copyright (C) {year} {name of author} 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | {project} Copyright (C) {year} {fullname} 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------