├── README.md
├── 本书源码说明.docx
├── NetServer
├── WebRoot
│ ├── META-INF
│ │ └── MANIFEST.MF
│ ├── WEB-INF
│ │ └── lib
│ │ │ ├── commons-io-1.4.jar
│ │ │ ├── fastjson-1.2.70.jar
│ │ │ └── commons-fileupload-1.2.1.jar
│ └── upload.jsp
├── 服务端工程的使用说明.docx
├── .idea
│ ├── vcs.xml
│ ├── modules.xml
│ ├── misc.xml
│ └── libraries
│ │ ├── lib1.xml
│ │ └── lib.xml
└── NetServer.iml
├── chapter16
├── lib
│ ├── dom4j-2.1.1.jar
│ ├── log4j-1.2.17.jar
│ ├── fastjson-1.2.70.jar
│ ├── httpcore-4.4.12.jar
│ ├── httpmime-4.5.10.jar
│ └── mchange-commons-java-0.2.16.jar
├── src
│ ├── com
│ │ └── network
│ │ │ ├── im_client
│ │ │ ├── qq_login_bg.png
│ │ │ ├── qq_logo_100.png
│ │ │ ├── qq_logo_30.png
│ │ │ ├── QQListener.java
│ │ │ ├── Friend.java
│ │ │ ├── friend_list.fxml
│ │ │ ├── FriendListMain.java
│ │ │ ├── ChatMain.java
│ │ │ ├── QQLoginMain.java
│ │ │ ├── chat.fxml
│ │ │ └── qq_login.fxml
│ │ │ ├── parser
│ │ │ ├── UserInfo.java
│ │ │ ├── GoodsItem.java
│ │ │ ├── GoodsOrder.java
│ │ │ └── XmlSax.java
│ │ │ ├── socket
│ │ │ ├── TestTcpServer.java
│ │ │ ├── TestTcpClient.java
│ │ │ ├── TestUdpServer.java
│ │ │ └── TestUdpClient.java
│ │ │ ├── ToastUtil.java
│ │ │ ├── im_server
│ │ │ ├── SocketBean.java
│ │ │ └── ChatServer.java
│ │ │ ├── PrintUtils.java
│ │ │ ├── DateUtil.java
│ │ │ └── httptest
│ │ │ └── HttpTesterMain.java
│ └── log4j.properties
├── .idea
│ ├── vcs.xml
│ ├── modules.xml
│ ├── misc.xml
│ └── libraries
│ │ └── lib1.xml
└── chapter16.iml
├── chapter17
├── lib
│ ├── c3p0-0.9.5.4.jar
│ ├── druid-1.1.19.jar
│ └── mysql-connector-java-8.0.16.jar
├── src
│ ├── db.properties
│ └── com
│ │ └── database
│ │ ├── ToastUtil.java
│ │ ├── Utils.java
│ │ ├── autocode
│ │ ├── Student.java
│ │ ├── AutoCodeMain.java
│ │ ├── auto_code.fxml
│ │ ├── Column.java
│ │ └── Teacher.java
│ │ ├── poem
│ │ ├── PoemMain.java
│ │ └── PoemColumn.java
│ │ └── properties
│ │ ├── TestProperties.java
│ │ └── PropertiesUtil.java
├── .idea
│ ├── modules.xml
│ ├── misc.xml
│ └── libraries
│ │ └── lib1.xml
└── chapter17.iml
├── chapter12
├── src
│ └── com
│ │ └── awt
│ │ ├── image
│ │ ├── apple.png
│ │ └── TestImage.java
│ │ ├── window
│ │ ├── TestSimple.java
│ │ ├── TestFrame.java
│ │ └── TestFlow.java
│ │ └── widget
│ │ └── TestTextArea.java
├── .idea
│ ├── modules.xml
│ └── misc.xml
└── chapter12.iml
├── chapter13
├── src
│ └── com
│ │ └── swing
│ │ ├── window
│ │ ├── apple.png
│ │ ├── TestFrame.java
│ │ └── TestPanel.java
│ │ ├── login
│ │ └── MainWindow.java
│ │ └── widget
│ │ ├── TestTextField.java
│ │ ├── TestPasswordField.java
│ │ ├── TestTextArea.java
│ │ └── TestScrollPanel.java
├── .idea
│ ├── modules.xml
│ └── misc.xml
└── chapter13.iml
├── chapter14
├── src
│ └── com
│ │ └── javafx
│ │ ├── scene
│ │ ├── banana.png
│ │ └── TestHello.java
│ │ ├── ToastUtil.java
│ │ ├── mortgage
│ │ ├── Repayment.java
│ │ └── MortgageMain.java
│ │ ├── NumberField.java
│ │ ├── loan
│ │ ├── Main.java
│ │ └── sample.fxml
│ │ ├── fxml
│ │ ├── LoginMain.java
│ │ └── login.fxml
│ │ └── widget
│ │ └── Snack.java
├── .idea
│ ├── modules.xml
│ └── misc.xml
└── chapter14.iml
├── chapter15
├── src
│ └── com
│ │ └── concurrent
│ │ ├── mouse
│ │ ├── hole.png
│ │ ├── mouse.png
│ │ ├── hole_hit.png
│ │ ├── mouse_hit.png
│ │ ├── hit_mouse.fxml
│ │ └── HitMouseMain.java
│ │ ├── PrintUtils.java
│ │ └── stopwatch
│ │ ├── WatchMain.java
│ │ └── watch.fxml
├── .idea
│ ├── modules.xml
│ └── misc.xml
└── chapter15.iml
├── chapter04
├── src
│ └── com
│ │ └── method
│ │ ├── function
│ │ ├── Simple.class
│ │ └── Simple.java
│ │ ├── big
│ │ └── TestInteger.java
│ │ └── pack
│ │ └── PackNumber.java
├── .idea
│ ├── modules.xml
│ └── misc.xml
└── chapter04.iml
├── chapter01
├── src
│ └── com
│ │ └── donghan
│ │ ├── nanjun
│ │ ├── dangyang
│ │ │ ├── Hello.class
│ │ │ ├── Maicheng.java
│ │ │ └── Hello.java
│ │ └── xiangyang
│ │ │ └── Xiangyang.java
│ │ ├── test
│ │ ├── Year.java
│ │ └── Number.java
│ │ ├── nanyang
│ │ └── wanxian
│ │ │ └── Wancheng.java
│ │ ├── huainan
│ │ └── hefei
│ │ │ └── Hefei.java
│ │ └── weijun
│ │ └── yexian
│ │ └── Yecheng.java
├── .idea
│ ├── modules.xml
│ └── misc.xml
└── chapter01.iml
├── chapter07
├── src
│ └── com
│ │ └── object
│ │ ├── inherit
│ │ ├── Swallow.java
│ │ ├── Eagle.java
│ │ ├── DuckProtected.java
│ │ ├── DuckPublic.java
│ │ └── Duck.java
│ │ ├── polymorphic
│ │ ├── Chick.java
│ │ ├── Chicken.java
│ │ ├── Hen.java
│ │ ├── Cock.java
│ │ ├── ChickenFinal.java
│ │ ├── TestChicken.java
│ │ └── TestFinal.java
│ │ ├── encapsulate
│ │ ├── OrangeSimple.java
│ │ └── OrangeMember.java
│ │ ├── account
│ │ ├── TestAccount.java
│ │ ├── DepositAccount.java
│ │ └── CashAccount.java
│ │ └── trade
│ │ ├── DepositAccount.java
│ │ ├── StockAccount.java
│ │ ├── TransactionRecord.java
│ │ ├── CashAccount.java
│ │ └── TestAccount.java
├── .idea
│ ├── modules.xml
│ └── misc.xml
└── chapter07.iml
├── chapter08
├── src
│ └── com
│ │ └── special
│ │ ├── function
│ │ ├── Behavior.java
│ │ ├── Calculator.java
│ │ ├── StringFilter.java
│ │ ├── StringFilter2.java
│ │ ├── Arithmetic.java
│ │ ├── Animal.java
│ │ ├── TestFunctional.java
│ │ ├── StringUtil.java
│ │ └── MathUtil.java
│ │ ├── behavior
│ │ ├── Behavior2.java
│ │ ├── Cock.java
│ │ ├── Hen.java
│ │ ├── SortDescend.java
│ │ ├── Chicken.java
│ │ ├── TestAbstract.java
│ │ ├── Behavior.java
│ │ ├── Frog.java
│ │ ├── Goose.java
│ │ ├── ExpandGoose.java
│ │ ├── Bird.java
│ │ ├── ExpandBehavior.java
│ │ └── TestInterface.java
│ │ ├── inner
│ │ ├── Season.java
│ │ ├── SeasonCn.java
│ │ ├── Tree.java
│ │ ├── TreeNest.java
│ │ ├── TreeInner.java
│ │ ├── TestInner.java
│ │ └── TestStatic.java
│ │ ├── singleton
│ │ ├── Plain.java
│ │ ├── SingletonHungry.java
│ │ ├── SingletonNest.java
│ │ ├── SingletonLazy.java
│ │ └── TestSingleton.java
│ │ └── builder
│ │ ├── UserCommon.java
│ │ ├── TestBuilder.java
│ │ └── User.java
├── .idea
│ ├── modules.xml
│ └── misc.xml
└── chapter08.iml
├── chapter02
├── .idea
│ ├── modules.xml
│ └── misc.xml
├── src
│ └── com
│ │ └── arithmetic
│ │ ├── numerical
│ │ ├── Basic.java
│ │ ├── Prefix.java
│ │ ├── Suffix.java
│ │ └── Convert.java
│ │ ├── math
│ │ ├── Angle.java
│ │ ├── Science.java
│ │ ├── Rand.java
│ │ └── Trunc.java
│ │ ├── Pingfanggen.java
│ │ ├── operator
│ │ ├── Assign.java
│ │ ├── Four.java
│ │ └── Unary.java
│ │ └── Niudundiedai.java
└── chapter02.iml
├── chapter03
├── .idea
│ ├── modules.xml
│ └── misc.xml
├── chapter03.iml
└── src
│ └── com
│ └── control
│ ├── Geligaoli.java
│ ├── array
│ ├── OneDimensional3.java
│ ├── ArrayFill.java
│ ├── ColonErgodic.java
│ ├── OneDimensional2.java
│ ├── OneDimensional.java
│ ├── TwoDimensional2.java
│ ├── TwoDimensional.java
│ ├── TwoDimensional3.java
│ └── ArraySort.java
│ ├── Jitutonglong.java
│ ├── process
│ ├── WhileLoop.java
│ ├── WhileLoop2.java
│ ├── Condition.java
│ ├── Condition3.java
│ ├── ForLoop2.java
│ ├── Multipath.java
│ ├── WhileLoop3.java
│ ├── ForLoop3.java
│ ├── Condition2.java
│ ├── Multipath2.java
│ └── ForLoop.java
│ ├── Feibonaqi.java
│ ├── SunziDingli.java
│ ├── YanghuiSanjiao.java
│ └── logic
│ ├── Relation.java
│ ├── Priority.java
│ ├── ShortCircuit.java
│ └── Bool.java
├── chapter05
├── .idea
│ ├── modules.xml
│ └── misc.xml
├── chapter05.iml
└── src
│ └── com
│ └── string
│ └── character
│ └── TypeChar.java
├── chapter06
├── .idea
│ ├── modules.xml
│ └── misc.xml
└── chapter06.iml
├── chapter09
├── .idea
│ ├── modules.xml
│ └── misc.xml
├── src
│ └── com
│ │ └── collect
│ │ ├── algorithm
│ │ ├── ArraySort.java
│ │ ├── SortInterface.java
│ │ ├── SortBubble.java
│ │ ├── LruMap.java
│ │ ├── FifoList.java
│ │ ├── SortQuick.java
│ │ ├── ArrayFind.java
│ │ └── TestQueue.java
│ │ ├── container
│ │ ├── MobilePhone.java
│ │ ├── MobilePhoneTree.java
│ │ └── MobilePhoneHash.java
│ │ └── generic
│ │ ├── TestClass.java
│ │ └── Apple.java
└── chapter09.iml
├── chapter10
├── .idea
│ ├── modules.xml
│ └── misc.xml
├── src
│ └── com
│ │ └── addition
│ │ ├── annotation
│ │ ├── Calculator.java
│ │ ├── Arithmetic.java
│ │ ├── NotNull.java
│ │ ├── Cock.java
│ │ ├── Hen.java
│ │ ├── Chicken.java
│ │ └── Apple.java
│ │ ├── exception
│ │ ├── ArrayOutOfException.java
│ │ ├── ArrayIsNullException.java
│ │ └── TestError.java
│ │ └── reflect
│ │ ├── Hen.java
│ │ ├── Cock.java
│ │ ├── Chicken.java
│ │ └── TestReflectClass.java
└── chapter10.iml
└── chapter11
├── .idea
├── modules.xml
└── misc.xml
├── chapter11.iml
└── src
└── com
└── io
├── huffman
├── TestHuffman.java
└── HuffmNode.java
└── bio
├── UserInfo.java
└── TestFileStream.java
/README.md:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aqi00/java/HEAD/README.md
--------------------------------------------------------------------------------
/本书源码说明.docx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aqi00/java/HEAD/本书源码说明.docx
--------------------------------------------------------------------------------
/NetServer/WebRoot/META-INF/MANIFEST.MF:
--------------------------------------------------------------------------------
1 | Manifest-Version: 1.0
2 | Class-Path:
3 |
4 |
--------------------------------------------------------------------------------
/NetServer/服务端工程的使用说明.docx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aqi00/java/HEAD/NetServer/服务端工程的使用说明.docx
--------------------------------------------------------------------------------
/chapter16/lib/dom4j-2.1.1.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aqi00/java/HEAD/chapter16/lib/dom4j-2.1.1.jar
--------------------------------------------------------------------------------
/chapter16/lib/log4j-1.2.17.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aqi00/java/HEAD/chapter16/lib/log4j-1.2.17.jar
--------------------------------------------------------------------------------
/chapter17/lib/c3p0-0.9.5.4.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aqi00/java/HEAD/chapter17/lib/c3p0-0.9.5.4.jar
--------------------------------------------------------------------------------
/chapter17/lib/druid-1.1.19.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aqi00/java/HEAD/chapter17/lib/druid-1.1.19.jar
--------------------------------------------------------------------------------
/chapter16/lib/fastjson-1.2.70.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aqi00/java/HEAD/chapter16/lib/fastjson-1.2.70.jar
--------------------------------------------------------------------------------
/chapter16/lib/httpcore-4.4.12.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aqi00/java/HEAD/chapter16/lib/httpcore-4.4.12.jar
--------------------------------------------------------------------------------
/chapter16/lib/httpmime-4.5.10.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aqi00/java/HEAD/chapter16/lib/httpmime-4.5.10.jar
--------------------------------------------------------------------------------
/chapter12/src/com/awt/image/apple.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aqi00/java/HEAD/chapter12/src/com/awt/image/apple.png
--------------------------------------------------------------------------------
/chapter13/src/com/swing/window/apple.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aqi00/java/HEAD/chapter13/src/com/swing/window/apple.png
--------------------------------------------------------------------------------
/chapter14/src/com/javafx/scene/banana.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aqi00/java/HEAD/chapter14/src/com/javafx/scene/banana.png
--------------------------------------------------------------------------------
/chapter15/src/com/concurrent/mouse/hole.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aqi00/java/HEAD/chapter15/src/com/concurrent/mouse/hole.png
--------------------------------------------------------------------------------
/chapter04/src/com/method/function/Simple.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aqi00/java/HEAD/chapter04/src/com/method/function/Simple.class
--------------------------------------------------------------------------------
/chapter15/src/com/concurrent/mouse/mouse.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aqi00/java/HEAD/chapter15/src/com/concurrent/mouse/mouse.png
--------------------------------------------------------------------------------
/chapter16/lib/mchange-commons-java-0.2.16.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aqi00/java/HEAD/chapter16/lib/mchange-commons-java-0.2.16.jar
--------------------------------------------------------------------------------
/chapter17/lib/mysql-connector-java-8.0.16.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aqi00/java/HEAD/chapter17/lib/mysql-connector-java-8.0.16.jar
--------------------------------------------------------------------------------
/NetServer/WebRoot/WEB-INF/lib/commons-io-1.4.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aqi00/java/HEAD/NetServer/WebRoot/WEB-INF/lib/commons-io-1.4.jar
--------------------------------------------------------------------------------
/chapter15/src/com/concurrent/mouse/hole_hit.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aqi00/java/HEAD/chapter15/src/com/concurrent/mouse/hole_hit.png
--------------------------------------------------------------------------------
/chapter15/src/com/concurrent/mouse/mouse_hit.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aqi00/java/HEAD/chapter15/src/com/concurrent/mouse/mouse_hit.png
--------------------------------------------------------------------------------
/NetServer/WebRoot/WEB-INF/lib/fastjson-1.2.70.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aqi00/java/HEAD/NetServer/WebRoot/WEB-INF/lib/fastjson-1.2.70.jar
--------------------------------------------------------------------------------
/chapter16/src/com/network/im_client/qq_login_bg.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aqi00/java/HEAD/chapter16/src/com/network/im_client/qq_login_bg.png
--------------------------------------------------------------------------------
/chapter16/src/com/network/im_client/qq_logo_100.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aqi00/java/HEAD/chapter16/src/com/network/im_client/qq_logo_100.png
--------------------------------------------------------------------------------
/chapter16/src/com/network/im_client/qq_logo_30.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aqi00/java/HEAD/chapter16/src/com/network/im_client/qq_logo_30.png
--------------------------------------------------------------------------------
/chapter01/src/com/donghan/nanjun/dangyang/Hello.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aqi00/java/HEAD/chapter01/src/com/donghan/nanjun/dangyang/Hello.class
--------------------------------------------------------------------------------
/chapter01/src/com/donghan/nanjun/dangyang/Maicheng.java:
--------------------------------------------------------------------------------
1 | package com.donghan.nanjun.dangyang; // 东汉帝国南郡当阳县
2 |
3 | public class Maicheng {
4 |
5 | }
6 |
--------------------------------------------------------------------------------
/NetServer/WebRoot/WEB-INF/lib/commons-fileupload-1.2.1.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aqi00/java/HEAD/NetServer/WebRoot/WEB-INF/lib/commons-fileupload-1.2.1.jar
--------------------------------------------------------------------------------
/chapter07/src/com/object/inherit/Swallow.java:
--------------------------------------------------------------------------------
1 | package com.object.inherit;
2 |
3 | //定义了一个继承自鸟类的燕子类
4 | public class Swallow extends Bird {
5 | // 燕子类未重写任何构造方法,则默认使用不带输入参数的构造方法
6 | }
7 |
--------------------------------------------------------------------------------
/chapter08/src/com/special/function/Behavior.java:
--------------------------------------------------------------------------------
1 | package com.special.function;
2 |
3 | //定义一个行为接口,给动物类调用
4 | public interface Behavior {
5 | public void act(); // 声明一个名叫行动的抽象方法
6 | }
7 |
--------------------------------------------------------------------------------
/chapter08/src/com/special/behavior/Behavior2.java:
--------------------------------------------------------------------------------
1 | package com.special.behavior;
2 |
3 | //定义另一个行为接口
4 | public interface Behavior2 {
5 | public void jump(); // 声明了一个抽象的跳跃方法
6 |
7 | }
8 |
--------------------------------------------------------------------------------
/chapter08/src/com/special/inner/Season.java:
--------------------------------------------------------------------------------
1 | package com.special.inner;
2 |
3 | //演示枚举类型的简单定义。这是个季节枚举
4 | public enum Season {
5 | // 几个枚举变量之间以逗号分隔
6 | SPRING, SUMMER, AUTUMN, WINTER
7 | }
8 |
--------------------------------------------------------------------------------
/chapter08/src/com/special/function/Calculator.java:
--------------------------------------------------------------------------------
1 | package com.special.function;
2 |
3 | //定义一个计算器接口,给算术类使用
4 | public interface Calculator {
5 | public double operate(double x, double y); // 声明一个名叫运算的抽象方法
6 | }
7 |
--------------------------------------------------------------------------------
/chapter08/src/com/special/function/StringFilter.java:
--------------------------------------------------------------------------------
1 | package com.special.function;
2 |
3 | //定义字符串的过滤接口
4 | public interface StringFilter {
5 | public boolean isMatch(String str); // 声明一个输入参数只有源字符串的抽象方法
6 | }
7 |
--------------------------------------------------------------------------------
/NetServer/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/chapter16/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/chapter08/src/com/special/function/StringFilter2.java:
--------------------------------------------------------------------------------
1 | package com.special.function;
2 |
3 | //定义字符串的过滤接口2
4 | public interface StringFilter2 {
5 | public boolean isMatch(String str, String sign); // 声明一个输入参数包括源字符串和标记串的抽象方法
6 | }
7 |
--------------------------------------------------------------------------------
/chapter16/src/com/network/parser/UserInfo.java:
--------------------------------------------------------------------------------
1 | package com.network.parser;
2 |
3 | //定义一个用户信息
4 | public class UserInfo {
5 | public String name; // 用户姓名
6 | public String address; // 收货地址
7 | public String phone; // 联系号码
8 | }
9 |
--------------------------------------------------------------------------------
/chapter16/src/com/network/im_client/QQListener.java:
--------------------------------------------------------------------------------
1 | package com.network.im_client;
2 |
3 | //定义QQ事件监听器,用于服务端的回调处理。这是一个函数式接口,调用时可写成Lambda表达式
4 | @FunctionalInterface
5 | public interface QQListener {
6 | public void doEvent(String content);
7 | }
8 |
--------------------------------------------------------------------------------
/chapter16/src/com/network/parser/GoodsItem.java:
--------------------------------------------------------------------------------
1 | package com.network.parser;
2 |
3 | //定义一项商品信息
4 | public class GoodsItem {
5 | public String goods_name; // 商品名称
6 | public int goods_number; // 商品数量
7 | public double goods_price; // 商品价格
8 | }
9 |
--------------------------------------------------------------------------------
/chapter17/src/db.properties:
--------------------------------------------------------------------------------
1 | #数据库连接信息配置
2 | jdbc.connection.driver_class=com.mysql.cj.jdbc.Driver
3 | jdbc.connection.url=jdbc:mysql://localhost:3306/study?serverTimezone=GMT%2B8
4 | jdbc.connection.username=root
5 | jdbc.connection.password=222@@@wwwWWW
6 |
--------------------------------------------------------------------------------
/chapter01/src/com/donghan/nanjun/dangyang/Hello.java:
--------------------------------------------------------------------------------
1 | package com.donghan.nanjun.dangyang;
2 |
3 | public class Hello {
4 |
5 | // 代码添加开始,下面的程序入口将会在控制台打印“Hello World”
6 | public static void main(String[] args) {
7 | System.out.println("Hello World");
8 | }
9 | // 代码添加结束
10 | }
11 |
--------------------------------------------------------------------------------
/NetServer/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/chapter01/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/chapter02/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/chapter03/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/chapter04/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/chapter05/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/chapter06/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/chapter07/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/chapter08/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/chapter09/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/chapter10/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/chapter11/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/chapter12/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/chapter13/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/chapter14/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/chapter15/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/chapter16/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/chapter17/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/NetServer/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/chapter01/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/chapter02/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/chapter03/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/chapter04/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/chapter05/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/chapter06/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/chapter07/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/chapter08/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/chapter09/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/chapter10/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/chapter11/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/chapter12/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/chapter13/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/chapter14/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/chapter15/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/chapter16/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/chapter17/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/chapter07/src/com/object/inherit/Eagle.java:
--------------------------------------------------------------------------------
1 | package com.object.inherit;
2 |
3 | //定义了一个继承自鸟类的老鹰类
4 | public class Eagle extends Bird {
5 |
6 | // 老鹰类重写了带三个参数的构造方法,则不使用没有输入参数的构造方法
7 | public Eagle(String name, int sexType, String voice) {
8 | super(name, sexType, voice); // 利用super指代父类的构造方法名称
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/chapter12/src/com/awt/window/TestSimple.java:
--------------------------------------------------------------------------------
1 | package com.awt.window;
2 |
3 | import java.awt.Frame;
4 |
5 | //演示一个最简单的窗口
6 | public class TestSimple {
7 |
8 | public static void main(String[] args) {
9 | Frame frame = new Frame(); // 创建一个窗口对象
10 | frame.setVisible(true); // 必须设置为true,否则看不见
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/chapter07/src/com/object/polymorphic/Chick.java:
--------------------------------------------------------------------------------
1 | package com.object.polymorphic;
2 |
3 | //定义一个小鸡类。因为小鸡的性别难以辨别,所以不再定义性别属性,小鸡类也不允许被继承
4 | final public class Chick {
5 |
6 | // 定义一个名称属性
7 | public String name;
8 |
9 | // 定义一个叫唤方法
10 | public void call() {
11 | System.out.println("叽叽喳喳");
12 | }
13 |
14 | }
15 |
--------------------------------------------------------------------------------
/chapter07/src/com/object/polymorphic/Chicken.java:
--------------------------------------------------------------------------------
1 | package com.object.polymorphic;
2 |
3 | //定义一个鸡类
4 | public class Chicken {
5 |
6 | // 定义一个名称属性
7 | public String name;
8 | // 定义一个性别属性
9 | public int sex;
10 |
11 | // 定义一个叫唤方法
12 | public void call() {
13 | System.out.println("半夜鸡叫");
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/chapter07/src/com/object/encapsulate/OrangeSimple.java:
--------------------------------------------------------------------------------
1 | package com.object.encapsulate;
2 |
3 | //演示如何定义类的属性
4 | public class OrangeSimple {
5 | public String name; // 定义了橘子的名称
6 | public double weight; // 定义了橘子的重量
7 | public boolean isRipe; // 定义了橘子是否成熟。true表示成熟,false表示未成熟
8 | public String place; // 定义了橘子的产地
9 | }
10 |
--------------------------------------------------------------------------------
/chapter16/.idea/libraries/lib1.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/chapter17/.idea/libraries/lib1.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/chapter08/src/com/special/behavior/Cock.java:
--------------------------------------------------------------------------------
1 | package com.special.behavior;
2 |
3 | //定义一个继承自抽象鸡类的公鸡类
4 | public class Cock extends Chicken {
5 | public Cock() {
6 | sex = 0; // 公鸡的性别固定为雄性
7 | }
8 |
9 | // 重写了公鸡的叫唤方法。如果不重写父类的抽象方法,那么该子类仍旧为抽象类
10 | public void call() {
11 | System.out.println("喔喔喔");
12 | }
13 |
14 | }
15 |
--------------------------------------------------------------------------------
/chapter08/src/com/special/behavior/Hen.java:
--------------------------------------------------------------------------------
1 | package com.special.behavior;
2 |
3 | //定义一个继承自抽象鸡类的母鸡类
4 | public class Hen extends Chicken {
5 | public Hen() {
6 | sex = 1; // 母鸡的性别固定为雌性
7 | }
8 |
9 | // 重写了母鸡的叫唤方法。如果不重写父类的抽象方法,那么该子类仍旧为抽象类
10 | public void call() {
11 | System.out.println("咯咯咯");
12 | }
13 |
14 | }
15 |
--------------------------------------------------------------------------------
/chapter10/src/com/addition/annotation/Calculator.java:
--------------------------------------------------------------------------------
1 | package com.addition.annotation;
2 |
3 | //该注解表示以下定义的是函数式接口,有且仅有一个抽象方法声明。
4 | //如果同时声明了多个抽象方法,则编译器在编码阶段就会报错。
5 | @FunctionalInterface
6 | //定义一个计算器接口,给算术类使用
7 | public interface Calculator {
8 |
9 | // 声明一个名叫运算的抽象方法
10 | public double operate(double x, double y);
11 | }
12 |
--------------------------------------------------------------------------------
/chapter07/src/com/object/polymorphic/Hen.java:
--------------------------------------------------------------------------------
1 | package com.object.polymorphic;
2 |
3 | //定义一个继承自鸡类的母鸡类
4 | public class Hen extends Chicken {
5 |
6 | public Hen() {
7 | // 母鸡的性别固定为雌性
8 | sex = 1;
9 | //sex = FEMALE;
10 | }
11 |
12 | // 重写了母鸡的叫唤方法
13 | public void call() {
14 | System.out.println("咯咯咯");
15 | }
16 |
17 | }
18 |
--------------------------------------------------------------------------------
/chapter08/src/com/special/function/Arithmetic.java:
--------------------------------------------------------------------------------
1 | package com.special.function;
2 |
3 | //定义一个算术类
4 | public class Arithmetic {
5 |
6 | // 定义一个静态的计算方法,根据传入的计算器接口,对后面两个数字做运算
7 | public static double calculate(Calculator calculator, double x, double y) {
8 | return calculator.operate(x, y); // 这里调用了计算器接口的运算方法
9 | }
10 |
11 | }
12 |
--------------------------------------------------------------------------------
/chapter16/src/com/network/parser/GoodsOrder.java:
--------------------------------------------------------------------------------
1 | package com.network.parser;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | //定义一次商品订单信息
7 | public class GoodsOrder {
8 | public UserInfo user_info = new UserInfo(); // 用户信息
9 | public List goods_list = new ArrayList(); // 购买的商品清单
10 | }
11 |
--------------------------------------------------------------------------------
/NetServer/.idea/libraries/lib1.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/chapter10/src/com/addition/exception/ArrayOutOfException.java:
--------------------------------------------------------------------------------
1 | package com.addition.exception;
2 |
3 | //定义一个数组越界异常。异常类必须由Exception派生而来
4 | public class ArrayOutOfException extends Exception {
5 | private static final long serialVersionUID = -1L;
6 |
7 | public ArrayOutOfException(String message) {
8 | super(message);
9 | }
10 |
11 | }
12 |
--------------------------------------------------------------------------------
/chapter16/src/com/network/socket/TestTcpServer.java:
--------------------------------------------------------------------------------
1 | package com.network.socket;
2 |
3 | //演示Socket服务器的运行(TCP协议的可靠连接)
4 | public class TestTcpServer {
5 |
6 | public static void main(String[] args) {
7 | new Thread(new ReceiveText()).start(); // 启动一个文本接收线程
8 | new Thread(new ReceiveFile()).start(); // 启动一个文件接收线程
9 | }
10 |
11 | }
12 |
--------------------------------------------------------------------------------
/chapter01/src/com/donghan/test/Year.java:
--------------------------------------------------------------------------------
1 | package com.donghan.test;
2 |
3 | import java.util.Date; // 引入系统库中的Date工具
4 |
5 | public class Year {
6 |
7 | public static void main(String[] args) {
8 | Date date = new Date(); // 创建一个当前日期的实例
9 | int year = date.getYear(); // 从当前日期中获取当前年份
10 | System.out.println("year="+year);
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/chapter08/src/com/special/singleton/Plain.java:
--------------------------------------------------------------------------------
1 | package com.special.singleton;
2 |
3 | //普通类,未实现单例模式
4 | public class Plain {
5 | private static int count = 0; // 创建实例的次数
6 |
7 | // 构造方法。每调用一次构造方法,实例的创建次数就加一
8 | public Plain() {
9 | count++;
10 | }
11 |
12 | // 获取实例的创建次数
13 | public int getCount() {
14 | return count;
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/chapter10/src/com/addition/annotation/Arithmetic.java:
--------------------------------------------------------------------------------
1 | package com.addition.annotation;
2 |
3 | //定义一个算术类
4 | public class Arithmetic {
5 |
6 | // 定义一个静态的计算方法,根据传入的计算器接口,对后面两个数字进行运算
7 | public static double calculate(Calculator calculator, double x, double y) {
8 | // 这里调用了计算器接口的运算方法
9 | return calculator.operate(x, y);
10 | }
11 |
12 | }
13 |
--------------------------------------------------------------------------------
/chapter10/src/com/addition/exception/ArrayIsNullException.java:
--------------------------------------------------------------------------------
1 | package com.addition.exception;
2 |
3 | //定义一个数组为空异常。异常类必须由Exception派生而来
4 | public class ArrayIsNullException extends Exception {
5 | private static final long serialVersionUID = -1L;
6 |
7 | public ArrayIsNullException(String message) {
8 | super(message);
9 | }
10 |
11 | }
12 |
--------------------------------------------------------------------------------
/NetServer/.idea/libraries/lib.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/chapter01/src/com/donghan/nanyang/wanxian/Wancheng.java:
--------------------------------------------------------------------------------
1 | package com.donghan.nanyang.wanxian; // 东汉帝国南阳郡宛县
2 |
3 | public class Wancheng {
4 |
5 | public static void getIron() {
6 | System.out.println("从宛城得到了一大批的铸铁兵器。");
7 | }
8 |
9 | public static void main(String[] args) {
10 | System.out.println("欢迎来到大汉的冶铁中心——宛城。");
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/chapter09/src/com/collect/algorithm/ArraySort.java:
--------------------------------------------------------------------------------
1 | package com.collect.algorithm;
2 |
3 | //定义一个数组排序的工具类
4 | public class ArraySort {
5 |
6 | // 定义了一个排序方法,支持给所有实现了Comparable接口的数组类型排序。
7 | // 第一个参数是待排序的数组,第二个参数是排序策略。
8 | public static > void sort(T[] array, SortInterface sorter) {
9 | sorter.sort(array);
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/chapter10/src/com/addition/annotation/NotNull.java:
--------------------------------------------------------------------------------
1 | package com.addition.annotation;
2 |
3 | import java.lang.annotation.*;
4 |
5 | @Documented // 该注解纳入到Java开发手册
6 | @Target(ElementType.FIELD) // 该注解的作用目标是字段(属性)
7 | @Retention(RetentionPolicy.RUNTIME) // 该注解保留至运行阶段,这样能够通过反射机制调用
8 | //定义了一个注解,在interface前面加上符号“@”,表示这是个注解
9 | public @interface NotNull {}
10 |
--------------------------------------------------------------------------------
/chapter10/src/com/addition/reflect/Hen.java:
--------------------------------------------------------------------------------
1 | package com.addition.reflect;
2 |
3 | //定义一个继承自鸡类的母鸡类
4 | public class Hen extends Chicken {
5 |
6 | public Hen() {
7 | name = "母鸡";
8 | // 母鸡的性别固定为雌性
9 | // sex = 1;
10 | sex = FEMALE;
11 | }
12 |
13 | // 重写了母鸡的叫唤方法
14 | public void call() {
15 | System.out.println("咯咯咯");
16 | }
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/chapter01/src/com/donghan/nanjun/xiangyang/Xiangyang.java:
--------------------------------------------------------------------------------
1 | package com.donghan.nanjun.xiangyang; // 东汉帝国南郡襄阳县
2 |
3 | import com.donghan.nanyang.wanxian.Wancheng; // 准备从宛城进口商品
4 |
5 | public class Xiangyang {
6 |
7 | public static void main(String[] args) {
8 | System.out.println("这里是华夏第一城池——襄阳城。");
9 | Wancheng.getIron(); // 从宛城进口铁器
10 | }
11 |
12 | }
13 |
--------------------------------------------------------------------------------
/chapter07/src/com/object/polymorphic/Cock.java:
--------------------------------------------------------------------------------
1 | package com.object.polymorphic;
2 |
3 | //定义一个继承自鸡类的公鸡类
4 | public class Cock extends Chicken {
5 |
6 | public Cock() {
7 | // 公鸡的性别固定为雄性
8 | sex = 0;
9 | //sex = MALE;
10 | }
11 |
12 | // 重写了公鸡的叫唤方法
13 | public void call() {
14 | super.call(); // 先调用鸡类的叫唤方法
15 | System.out.println("喔喔喔");
16 | }
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/chapter14/src/com/javafx/ToastUtil.java:
--------------------------------------------------------------------------------
1 | package com.javafx;
2 |
3 | import javafx.scene.control.Alert;
4 |
5 | //定义提示框工具类
6 | public class ToastUtil {
7 | // 弹出提示框
8 | public static void show(String message) {
9 | Alert alert = new Alert(Alert.AlertType.INFORMATION); // 创建消息对话框
10 | alert.setContentText(message); // 设置对话框的内容文本
11 | alert.show(); // 显示对话框
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/chapter16/src/com/network/ToastUtil.java:
--------------------------------------------------------------------------------
1 | package com.network;
2 |
3 | import javafx.scene.control.Alert;
4 |
5 | //定义提示框工具类
6 | public class ToastUtil {
7 | // 弹出提示框
8 | public static void show(String message) {
9 | Alert alert = new Alert(Alert.AlertType.INFORMATION); // 创建消息对话框
10 | alert.setContentText(message); // 设置对话框的内容文本
11 | alert.show(); // 显示对话框
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/chapter17/src/com/database/ToastUtil.java:
--------------------------------------------------------------------------------
1 | package com.database;
2 |
3 | import javafx.scene.control.Alert;
4 |
5 | //定义提示框工具类
6 | public class ToastUtil {
7 | // 弹出提示框
8 | public static void show(String message) {
9 | Alert alert = new Alert(Alert.AlertType.INFORMATION); // 创建消息对话框
10 | alert.setContentText(message); // 设置对话框的内容文本
11 | alert.show(); // 显示对话框
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/chapter10/src/com/addition/reflect/Cock.java:
--------------------------------------------------------------------------------
1 | package com.addition.reflect;
2 |
3 | //定义一个继承自鸡类的公鸡类
4 | public class Cock extends Chicken {
5 |
6 | public Cock() {
7 | name = "公鸡";
8 | // 公鸡的性别固定为雄性
9 | // sex = 0;
10 | sex = MALE;
11 | }
12 |
13 | // 重写了公鸡的叫唤方法
14 | public void call() {
15 | super.call(); // 先调用鸡类的叫唤方法
16 | System.out.println("喔喔喔");
17 | }
18 |
19 | }
20 |
--------------------------------------------------------------------------------
/chapter09/src/com/collect/algorithm/SortInterface.java:
--------------------------------------------------------------------------------
1 | package com.collect.algorithm;
2 |
3 | //定义排序接口
4 | public interface SortInterface> {
5 |
6 | // 声明一个排序操作的抽象方法
7 | public void sort(T[] array);
8 |
9 | // 定义一个交换数组元素的默认方法
10 | default void swap(T[] array, int i, int j) {
11 | T temp = array[i];
12 | array[i] = array[j];
13 | array[j] = temp;
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/chapter08/src/com/special/behavior/SortDescend.java:
--------------------------------------------------------------------------------
1 | package com.special.behavior;
2 |
3 | import java.util.Comparator;
4 |
5 | //定义一个整型数组的降序比较器
6 | public class SortDescend implements Comparator {
7 |
8 | @Override
9 | public int compare(Integer o1, Integer o2) {
10 | // return Integer.compare(o1, o2); // 默认的参数顺序是升序
11 | return Integer.compare(o2, o1); // 倒过来的参数顺序变成了降序
12 | }
13 |
14 | }
15 |
--------------------------------------------------------------------------------
/chapter10/src/com/addition/annotation/Cock.java:
--------------------------------------------------------------------------------
1 | package com.addition.annotation;
2 |
3 | //定义一个继承自鸡类的公鸡类
4 | public class Cock extends Chicken {
5 |
6 | public Cock() {
7 | // 公鸡的性别固定为雄性
8 | // sex = 0;
9 | sex = MALE;
10 | }
11 |
12 | // 该注解表示以下重写了父类的方法。
13 | // 如果父类找不到参数列表一致的同名方法,则编译器在编码阶段就会报错。
14 | @Override
15 | // 重写了公鸡的叫唤方法
16 | public void call() {
17 | System.out.println("喔喔喔");
18 | }
19 |
20 | }
21 |
--------------------------------------------------------------------------------
/chapter10/src/com/addition/annotation/Hen.java:
--------------------------------------------------------------------------------
1 | package com.addition.annotation;
2 |
3 | //定义一个继承自鸡类的母鸡类
4 | public class Hen extends Chicken {
5 |
6 | public Hen() {
7 | // 母鸡的性别固定为雌性
8 | // sex = 1;
9 | sex = FEMALE;
10 | }
11 |
12 | // 该注解表示以下重写了父类的方法。
13 | // 如果父类找不到参数列表一致的同名方法,则编译器在编码阶段就会报错。
14 | @Override
15 | // 重写了母鸡的叫唤方法
16 | public void call() {
17 | System.out.println("咯咯咯");
18 | }
19 |
20 | }
21 |
--------------------------------------------------------------------------------
/chapter14/src/com/javafx/mortgage/Repayment.java:
--------------------------------------------------------------------------------
1 | package com.javafx.mortgage;
2 |
3 | //还贷的数据结构
4 | public class Repayment {
5 | public double totalLoan; // 贷款总额
6 | public double monthRepay; // 每月还款金额
7 | public double monthMinus; // 等额本金方式的每月递减金额
8 | public double totalInterest; // 贷款总利息
9 |
10 | public Repayment() {
11 | totalLoan = 0;
12 | monthRepay = 0;
13 | monthMinus = 0;
14 | totalInterest = 0;
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/chapter08/src/com/special/behavior/Chicken.java:
--------------------------------------------------------------------------------
1 | package com.special.behavior;
2 |
3 | //演示抽象类的定义。这是一个抽象鸡类
4 | abstract public class Chicken {
5 | public String name; // 鸡的名称
6 | public int sex; // 鸡的性别
7 |
8 | // 定义一个抽象的叫唤方法。注意后面没有花括号,并且以分号结尾
9 | abstract public void call();
10 |
11 | // 即使抽象类定义了构造方法,外部也无法创建它的实例
12 | public Chicken() {
13 | }
14 |
15 | // Java只有抽象类和抽象方法,没有抽象属性的说法
16 | // abstract public String cry;
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/chapter01/src/com/donghan/test/Number.java:
--------------------------------------------------------------------------------
1 | package com.donghan.test;
2 |
3 | public class Number {
4 |
5 | public static void main(String[] args) {
6 | int x = 8; // 8对应的二进制数为00001000
7 | int y = x << 2; // 00001000左移两位后变成00100000,左移结果转成十进制则为32
8 | int z = x >> 2; // 00001000右移两位后变成00000010,右移结果转成十进制则为2
9 | System.out.println("原始数字x="+x);
10 | System.out.println("x左移两位后="+y);
11 | System.out.println("x右移两位后="+z);
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/chapter16/src/com/network/im_client/Friend.java:
--------------------------------------------------------------------------------
1 | package com.network.im_client;
2 |
3 | //定义一个好友信息类
4 | public class Friend {
5 | public String device_id; // 好友的设备编号
6 | public String nick_name; // 好友的昵称
7 | public String login_time; // 好友的登录时间
8 |
9 | public Friend(String device_id, String nick_name, String login_time) {
10 | this.device_id = device_id;
11 | this.nick_name = nick_name;
12 | this.login_time = login_time;
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/chapter01/src/com/donghan/huainan/hefei/Hefei.java:
--------------------------------------------------------------------------------
1 | package com.donghan.huainan.hefei; // 东汉帝国淮南郡合肥县
2 |
3 | import java.util.Scanner; // 导入系统自带的Scanner工具
4 |
5 | public class Hefei {
6 |
7 | public static void main(String[] args) {
8 | System.out.print("这里是张辽镇守的合肥城,吴国谁来挑战?");
9 | Scanner scan = new Scanner(System.in); // 从控制台接收输入文本
10 | /* nextLine方法表示接收一行文字,以回车键结尾 */
11 | System.out.println("吴国前来挑战的将领是:"+scan.nextLine());
12 | }
13 |
14 | }
15 |
--------------------------------------------------------------------------------
/chapter08/src/com/special/function/Animal.java:
--------------------------------------------------------------------------------
1 | package com.special.function;
2 |
3 | //演示动物类的定义,其中midnight方法的输入参数为Behavior类型
4 | public class Animal {
5 | private String name; // 动物名称
6 |
7 | public Animal(String name) {
8 | this.name = name;
9 | }
10 |
11 | public String getName() {
12 | return this.name;
13 | }
14 |
15 | // 定义一个半夜行动的方法。具体的动作由输入行为的act方法执行
16 | public void midnight(Behavior behavior) {
17 | behavior.act();
18 | }
19 |
20 | }
21 |
--------------------------------------------------------------------------------
/chapter02/src/com/arithmetic/numerical/Basic.java:
--------------------------------------------------------------------------------
1 | package com.arithmetic.numerical;
2 |
3 | //演示基本变量类型的用法
4 | public class Basic {
5 |
6 | public static void main(String[] args) {
7 | int zhumulanma; // 先声明变量
8 | // 8844是2005年中国测量得到的珠穆朗玛峰岩面高度,8848是冰雪高度
9 | zhumulanma = 8844; // 再对变量赋值
10 | System.out.println("珠穆朗玛峰的高度=" + zhumulanma);
11 | double yuanzhoulv = 3.1415926; // 在声明变量之时就初始化赋值
12 | System.out.println("圆周率=" + yuanzhoulv);
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/chapter08/src/com/special/behavior/TestAbstract.java:
--------------------------------------------------------------------------------
1 | package com.special.behavior;
2 |
3 | //演示抽象类派生而来的子类用法
4 | public class TestAbstract {
5 |
6 | public static void main(String[] args) {
7 | // 不能创建抽象类的实例,因为抽象类是个尚未完工的类
8 | // Chicken chicken = new Chicken();
9 | Cock cock = new Cock(); // 创建一个公鸡实例,公鸡类继承自抽象类Chicken
10 | cock.call(); // 调用公鸡实例的叫唤方法
11 | Hen hen = new Hen(); // 创建一个母鸡实例,母鸡类继承自抽象类Chicken
12 | hen.call(); // 调用母鸡实例的叫唤方法
13 | }
14 |
15 | }
16 |
--------------------------------------------------------------------------------
/chapter01/chapter01.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/chapter02/chapter02.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/chapter03/chapter03.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/chapter04/chapter04.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/chapter05/chapter05.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/chapter06/chapter06.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/chapter07/chapter07.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/chapter08/chapter08.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/chapter09/chapter09.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/chapter10/chapter10.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/chapter11/chapter11.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/chapter12/chapter12.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/chapter13/chapter13.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/chapter03/src/com/control/Geligaoli.java:
--------------------------------------------------------------------------------
1 | package com.control;
2 |
3 | //利用格里高利公式(又称格里高利级数)求圆周率
4 | //π=4*(1/1-1/3+1/5-1/7+1/9......)
5 | public class Geligaoli {
6 |
7 | public static void main(String[] arg) {
8 | double π = 0; // 圆周率
9 | double zhengfu = 1; // 正负标志
10 | for (int i=1; i<800000; i+=2) { // 使用循环语句计算有限次数的无穷级数
11 | π = π + zhengfu*4/i; // 通过无穷级数逼近圆周率
12 | zhengfu = -zhengfu; // 每次计算之后,翻转下次的正负号
13 | }
14 | System.out.println("圆周率="+π);
15 | }
16 |
17 | }
18 |
--------------------------------------------------------------------------------
/chapter03/src/com/control/array/OneDimensional3.java:
--------------------------------------------------------------------------------
1 | package com.control.array;
2 |
3 | // 演示如何通过循环语句遍历并修改每个元素的数值
4 | public class OneDimensional3 {
5 |
6 | public static void main(String[] args) {
7 | // 以下是分配数组空间的第三种形式:赋值等号右边直接跟着花括号
8 | int[] numbers = { 2, 3, 5, 7 };
9 | // 下面通过循环语句给数组的每个元素都加一
10 | for (int i = 0; i < numbers.length; i++) {
11 | // 给下标为i的数组元素加一
12 | numbers[i]++;
13 | // 打印下标为i的数组元素
14 | System.out.println("number = " + numbers[i]);
15 | }
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/chapter09/src/com/collect/container/MobilePhone.java:
--------------------------------------------------------------------------------
1 | package com.collect.container;
2 |
3 | //定义一个手机类
4 | public class MobilePhone {
5 |
6 | private String brand; // 手机品牌
7 | private Integer price; // 手机价格
8 |
9 | public MobilePhone(String brand, int price) {
10 | this.brand = brand;
11 | this.price = price;
12 | }
13 |
14 | // 获取手机品牌
15 | public String getBrand() {
16 | return this.brand;
17 | }
18 |
19 | // 获取手机价格
20 | public int getPrice() {
21 | return this.price;
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/chapter16/src/com/network/im_server/SocketBean.java:
--------------------------------------------------------------------------------
1 | package com.network.im_server;
2 |
3 | import java.net.Socket;
4 |
5 | //定义客户端的套接字详情
6 | public class SocketBean {
7 | public String id; // 客户端的服务编号,这是服务端给客户端分配的编号
8 | public Socket socket; // 客户端的套接字
9 | public String deviceId; // 客户端的设备编号,这是客户端给自己取的编号
10 | public String nickName; // 客户端的昵称
11 | public String loginTime; // 客户端的登录时间
12 |
13 | public SocketBean(String id, Socket socket) {
14 | this.id = id;
15 | this.socket = socket;
16 | }
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/chapter08/src/com/special/builder/UserCommon.java:
--------------------------------------------------------------------------------
1 | package com.special.builder;
2 |
3 | //定义一个不能修改属性的用户类
4 | public class UserCommon {
5 | private final String name; // 姓名
6 | private final String password; // 密码
7 |
8 | public UserCommon(String name, String password) {
9 | this.name = name;
10 | this.password = password;
11 | }
12 |
13 | // 获取用户的姓名
14 | public String getName() {
15 | return this.name;
16 | }
17 |
18 | // 获取用户的密码
19 | public String getPassword() {
20 | return this.password;
21 | }
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/chapter15/src/com/concurrent/PrintUtils.java:
--------------------------------------------------------------------------------
1 | package com.concurrent;
2 |
3 | import java.text.SimpleDateFormat;
4 | import java.util.Date;
5 |
6 | //定义了线程专用的日志打印工具
7 | public class PrintUtils {
8 | // 打印线程的运行日志,包括当前时间、当前线程名称、具体事件描述等信息
9 | public static void print(String threadName, String event) {
10 | SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");
11 | String dateTime = sdf.format(new Date());
12 | String desc = String.format("%s %s %s", dateTime, threadName, event);
13 | System.out.println(desc);
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/chapter16/src/com/network/PrintUtils.java:
--------------------------------------------------------------------------------
1 | package com.network;
2 |
3 | import java.text.SimpleDateFormat;
4 | import java.util.Date;
5 |
6 | //定义了线程专用的日志打印工具
7 | public class PrintUtils {
8 | // 打印线程的运行日志,包括当前时间、当前线程名称、具体事件描述等信息
9 | public static void print(String event) {
10 | SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");
11 | String dateTime = sdf.format(new Date());
12 | String desc = String.format("%s %s %s", dateTime, Thread.currentThread().getName(), event);
13 | System.out.println(desc);
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/chapter03/src/com/control/array/ArrayFill.java:
--------------------------------------------------------------------------------
1 | package com.control.array;
2 |
3 | import java.util.Arrays;
4 |
5 | //演示如何使用Arrays工具的fill方法
6 | public class ArrayFill {
7 |
8 | public static void main(String[] args) {
9 | // 构造一个包含十个99的数组变量
10 | // int[] prices = {99, 99, 99, 99, 99, 99, 99, 99, 99, 99};
11 | int[] prices = new int[10]; // 声明一个整型数组,数组大小为10
12 | Arrays.fill(prices, 99); // 给整型数组的每个元素全部填写99
13 | for (int price : prices) { // 循环遍历并打印整型数组的所有元素数值
14 | System.out.println("price = "+price);
15 | }
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/chapter14/chapter14.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/chapter15/chapter15.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/chapter07/src/com/object/polymorphic/ChickenFinal.java:
--------------------------------------------------------------------------------
1 | package com.object.polymorphic;
2 |
3 | //定义一个鸡类,它内部定义了终态属性和终态方法
4 | public class ChickenFinal {
5 |
6 | // 定义一个名称属性
7 | public String name;
8 | // 定义一个性别属性
9 | public int sex;
10 |
11 | // 定义一个叫唤方法
12 | public void call() {
13 | System.out.println("半夜鸡叫");
14 | }
15 |
16 | // 以下利用final修饰成员属性和成员方法
17 | public final int MALE = 0; // 雄性
18 | public final int FEMALE = 1; // 雌性
19 |
20 | // 定义一个能否游泳的方法
21 | public final boolean canSwim() {
22 | return false;
23 | }
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/chapter17/src/com/database/Utils.java:
--------------------------------------------------------------------------------
1 | package com.database;
2 |
3 | import java.net.URL;
4 |
5 | public class Utils {
6 |
7 | // 获取当前程序的本地路径。内部兼容了是否打成jar包的两种情况
8 | public static String getClassPath() {
9 | String class_path;
10 | URL url = Utils.class.getResource("/"); // 获取class文件的根目录
11 | if (url != null) {
12 | // class文件没被打成jar包时,采用下面办法
13 | class_path = url.getFile();
14 | } else {
15 | // class文件被打进jar包之时,采用下面办法。同时要把配置文件放到jar包的同级目录
16 | class_path = System.getProperty("user.dir");
17 | }
18 | return class_path;
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/chapter03/src/com/control/array/ColonErgodic.java:
--------------------------------------------------------------------------------
1 | package com.control.array;
2 |
3 | //演示如何利用冒号简化数组元素的遍历操作
4 | public class ColonErgodic {
5 |
6 | public static void main(String[] args) {
7 | int[] numbers = { 2, 3, 5, 7 };
8 | // for (int i = 0; i < numbers.length; i++) {
9 | // int number = numbers[i]; // 获取下标为i的元素,并赋值给名为number的变量
10 | // System.out.println("number = " + number);
11 | // }
12 | // 在for循环中,可以利用“变量类型 变量名称 : 数组名称”的形式,直接把数组元素赋值给该变量
13 | for (int number : numbers) {
14 | System.out.println("number = " + number);
15 | }
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/chapter07/src/com/object/polymorphic/TestChicken.java:
--------------------------------------------------------------------------------
1 | package com.object.polymorphic;
2 |
3 | //演示类的多态性
4 | public class TestChicken {
5 |
6 | public static void main(String[] args) {
7 | Chicken chicken = new Cock(); // 鸡类的实例变成了一只公鸡
8 | chicken.call(); // 此时鸡类的叫声就变为公鸡的叫声
9 | chicken = new Hen(); // 鸡类的实例变成了一只母鸡
10 | chicken.call(); // 此时鸡类的叫声就变为母鸡的叫声
11 |
12 | call(new Cock()); // 公鸡叫
13 | call(new Hen()); // 母鸡叫
14 | }
15 |
16 | // 定义一个叫唤方法,传入什么鸡,就让什么鸡叫
17 | private static void call(Chicken chicken) {
18 | chicken.call();
19 | }
20 |
21 | }
22 |
--------------------------------------------------------------------------------
/chapter08/src/com/special/behavior/Behavior.java:
--------------------------------------------------------------------------------
1 | package com.special.behavior;
2 |
3 | //定义一个接口。接口主要声明一些特定的行为方法
4 | public interface Behavior {
5 | // 注意,接口内部的方法默认为抽象方法,所以不必添加abstract前缀
6 | // abstract public void fly(); // 这里的abstract可加可不加
7 | public void fly(); // 声明了一个抽象的飞翔方法
8 | public void swim(); // 声明了一个抽象的游泳方法
9 | public void run(); // 声明了一个抽象的奔跑方法
10 |
11 | // 接口内部的属性默认都是终态属性,所以不必添加final前缀
12 | public String TAG = "动物世界";
13 | // public final String TAG = "动物世界"; // 这里的final可加可不加
14 |
15 | // 接口不允许定义构造方法。在Java8以前,接口内部的所有方法都必须是抽象方法
16 |
17 | }
18 |
--------------------------------------------------------------------------------
/chapter09/src/com/collect/algorithm/SortBubble.java:
--------------------------------------------------------------------------------
1 | package com.collect.algorithm;
2 |
3 | //冒泡排序的算法实现
4 | public class SortBubble implements SortInterface {
5 |
6 | // 以下的sort方法实现了冒泡排序算法的升序排列
7 | @Override
8 | public void sort(Comparable[] array) {
9 | for (int i = 1; i < array.length; i++) { // 假设数组长度为n,则需要n-1个循环
10 | for (int j = 0; j < array.length - i; j++) { // 每个循环都把最大的元素挪到本区间的末尾
11 | if (array[j].compareTo(array[j + 1]) > 0) { // 前一个元素比后一个元素要大
12 | swap(array, j, j + 1); // 交换两个数组元素,也就是把更大的元素往后挪
13 | }
14 | }
15 | }
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/chapter14/src/com/javafx/NumberField.java:
--------------------------------------------------------------------------------
1 | package com.javafx;
2 |
3 | import javafx.scene.control.TextField;
4 |
5 | //自定义数字输入框。只允许输入0到9,以及小数点
6 | public class NumberField extends TextField {
7 |
8 | @Override
9 | public void replaceText(int start, int end, String text) {
10 | if (text.matches("[0-9|.]")) { // 字符串由数字与小数点组成
11 | super.replaceText(start, end, text);
12 | }
13 | }
14 |
15 | @Override
16 | public void replaceSelection(String text) {
17 | if (text.matches("[0-9|.]")) { // 字符串由数字与小数点组成
18 | super.replaceSelection(text);
19 | }
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/chapter16/src/com/network/DateUtil.java:
--------------------------------------------------------------------------------
1 | package com.network;
2 |
3 | import java.text.SimpleDateFormat;
4 | import java.util.Date;
5 |
6 | public class DateUtil {
7 |
8 | // 获取当前时间的编号
9 | public static String getTimeId() {
10 | Date date = new Date();
11 | SimpleDateFormat sdf = new SimpleDateFormat("HHmmss");
12 | return sdf.format(date);
13 | }
14 |
15 | // 获取当前的日期时间
16 | public static String getNowTime() {
17 | Date date = new Date();
18 | SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
19 | return sdf.format(date);
20 | }
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/chapter03/src/com/control/Jitutonglong.java:
--------------------------------------------------------------------------------
1 | package com.control;
2 |
3 | //演示“鸡兔同笼”问题的解题过程
4 | public class Jitutonglong {
5 |
6 | public static void main(String[] arg) {
7 | // 今有雉兔同笼,上有三十五头,下有九十四足,问雉兔各几何?
8 | int chick, rabbit; // chick表示鸡的数量,rabbit表示兔子的数量
9 | int sum = 35; // 鸡和兔子加起来一共35只
10 | for (chick=0, rabbit=0; chick <= sum; chick++) { // 利用穷举法逐个尝试可能的鸡兔组合
11 | rabbit = sum - chick; // 计算兔子的数量
12 | if (chick * 2 + rabbit * 4 == 94) { // 满足鸡兔同笼的问题条件,则结束循环
13 | break;
14 | }
15 | }
16 | System.out.println("鸡的数量为" + chick + ",兔子的数量为" + rabbit);
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/chapter03/src/com/control/process/WhileLoop.java:
--------------------------------------------------------------------------------
1 | package com.control.process;
2 |
3 | import java.util.Scanner;
4 |
5 | // 演示while语句的用法
6 | public class WhileLoop {
7 |
8 | public static void main(String[] args) {
9 | System.out.println("长夜漫漫,无心睡眠,请给他设定一个睡醒的年限");
10 | Scanner scan = new Scanner(System.in); // 从控制台接收输入文本
11 | int limit = scan.nextInt(); // nextInt方法表示接收一个整数,以回车键结尾
12 | int year = 0;
13 | while (year < limit) { // 当条件满足时,在循环内部持续处理
14 | System.out.println("已经过去的年份:" + year);
15 | year++;
16 | }
17 | System.out.println("他足足睡了这么多年:" + year);
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/chapter04/src/com/method/function/Simple.java:
--------------------------------------------------------------------------------
1 | package com.method.function;
2 |
3 | //说明main方法的格式定义
4 | public class Simple {
5 |
6 | // 方法的定义格式为:“访问权限 可选的static 返回值的数据类型 方法名称(参数类型 参数名称)”
7 | // 其中多个参数之间以逗号分隔,如“参数1类型 参数1名称, 参数2类型 参数2名称”
8 | // 如果该方法无需返回任何数值,则返回值的数据类型填void
9 | public static void main(String[] args) { // String[]表示字符串数组
10 | if (args.length == 0) {
11 | System.out.println("您没有输入任何参数");
12 | }
13 | for (int i=0; i
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/chapter03/src/com/control/process/WhileLoop2.java:
--------------------------------------------------------------------------------
1 | package com.control.process;
2 |
3 | import java.util.Scanner;
4 |
5 | // 演示do-while的用法
6 | public class WhileLoop2 {
7 |
8 | public static void main(String[] args) {
9 | System.out.println("长夜漫漫,无心睡眠,请给他设定一个睡醒的年限");
10 | Scanner scan = new Scanner(System.in); // 从控制台接收输入文本
11 | int limit = scan.nextInt(); // nextInt方法表示接收一个整数,以回车键结尾
12 | int year = 0;
13 | do { // 开始循环处理
14 | System.out.println("已经过去的年份:" + year);
15 | year++;
16 | } while (year < limit); // 当条件满足时,在循环内部持续处理
17 | System.out.println("他足足睡了这么多年:" + year);
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/chapter08/src/com/special/singleton/SingletonHungry.java:
--------------------------------------------------------------------------------
1 | package com.special.singleton;
2 |
3 | //饿汉方式实现单例模式。在程序启动时就创建实例
4 | public class SingletonHungry {
5 | private static int count = 0; // 创建实例的次数
6 | // 声明一个当前类的实例。在程序启动之后,会自动给类的静态属性赋初始值
7 | private static final SingletonHungry instance = new SingletonHungry();
8 |
9 | // 构造方法。每调用一次构造方法,实例的创建次数就加一
10 | private SingletonHungry() {
11 | count++;
12 | }
13 |
14 | // 获取当前类的实例
15 | public static SingletonHungry getInstance() {
16 | return instance;
17 | }
18 |
19 | // 获取实例的创建次数
20 | public int getCount() {
21 | return count;
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/chapter16/chapter16.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/chapter17/chapter17.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/chapter03/src/com/control/process/Condition.java:
--------------------------------------------------------------------------------
1 | package com.control.process;
2 |
3 | import java.util.Scanner;
4 |
5 | // 演示if语句的用法
6 | public class Condition {
7 |
8 | public static void main(String[] args) {
9 | System.out.println("凉风有信,秋月无边。打二字");
10 | System.out.println("获取“凉风有信”的谜底请按1,获取“秋月无边”的谜底请按2");
11 | Scanner scan = new Scanner(System.in); // 从控制台接收输入文本
12 | int seq = scan.nextInt(); // nextInt方法表示接收一个整数,以回车键结尾
13 | if (seq == 1) { // 按1时打印“凉风有信”的谜底
14 | System.out.println("凉风有信的谜底是“讽”");
15 | }
16 | if (seq == 2) { // 按2时打印“秋月无边”的谜底
17 | System.out.println("秋月无边的谜底是“二”");
18 | }
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/chapter03/src/com/control/process/Condition3.java:
--------------------------------------------------------------------------------
1 | package com.control.process;
2 |
3 | import java.util.Scanner;
4 |
5 | // 演示else语句的用法
6 | public class Condition3 {
7 |
8 | public static void main(String[] args) {
9 | System.out.println("凉风有信,秋月无边。打二字");
10 | System.out.println("获取“凉风有信”的谜底请按1,获取“秋月无边”的谜底请按2");
11 | Scanner scan = new Scanner(System.in); // 从控制台接收输入文本
12 | int seq = scan.nextInt(); // nextInt方法表示接收一个整数,以回车键结尾
13 | if (seq == 1) { // 条件式子为真时,进入if分支处理
14 | System.out.println("凉风有信的谜底是“讽”");
15 | } else { // 否则(条件式子为假),进入else分支处理
16 | System.out.println("秋月无边的谜底是“二”");
17 | }
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/chapter08/src/com/special/inner/SeasonCn.java:
--------------------------------------------------------------------------------
1 | package com.special.inner;
2 |
3 | //演示枚举类型的扩展定义
4 | public enum SeasonCn {
5 | // 在定义枚举变量的同时,调用该枚举变量的构造方法
6 | SPRING(1, "春天"), SUMMER(2, "夏天"), AUTUMN(3, "秋天"), WINTER(4, "冬天");
7 |
8 | private int value; // 季节的数字序号
9 | private String name; // 季节的中文名称
10 |
11 | // 在构造方法中传入该季节的阿拉伯数字和中文名称
12 | private SeasonCn(int value, String name) {
13 | this.value = value;
14 | this.name = name;
15 | }
16 |
17 | // 获取季节的数字序号
18 | public int getValue() {
19 | return this.value;
20 | }
21 |
22 | // 获取季节的中文名称
23 | public String getName() {
24 | return this.name;
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/chapter08/src/com/special/behavior/Frog.java:
--------------------------------------------------------------------------------
1 | package com.special.behavior;
2 |
3 | //定义一个实现了接口Behavior和Behavior2的蛙类。类只能继承一个,但接口可以实现多个
4 | public class Frog implements Behavior, Behavior2 {
5 |
6 | // 实现了Behavior2接口的jump方法
7 | @Override
8 | public void jump() {
9 | System.out.println("青蛙跳跃的技能叫做“蛙跳”");
10 | }
11 |
12 | // 实现了Behavior接口的fly方法。因为青蛙不会飞,所以fly方法留空
13 | @Override
14 | public void fly() {}
15 |
16 | // 实现了Behavior接口的swim方法
17 | @Override
18 | public void swim() {
19 | System.out.println("青蛙游泳的技能叫做“蛙泳”");
20 | }
21 |
22 | // 实现了Behavior接口的run方法。因为青蛙不会跑,所以fly方法留空
23 | @Override
24 | public void run() {}
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/chapter08/src/com/special/behavior/Goose.java:
--------------------------------------------------------------------------------
1 | package com.special.behavior;
2 |
3 | //定义一个实现了接口Behavior的鹅类。注意鹅类需要实现Behavior接口的所有抽象方法
4 | public class Goose extends Bird implements Behavior {
5 |
6 | public Goose(String name, int sexType) {
7 | super(name, sexType);
8 | }
9 |
10 | // 实现了接口的fly方法
11 | @Override
12 | public void fly() {
13 | System.out.println("鹅飞不高,也飞不远。");
14 | }
15 |
16 | // 实现了接口的swim方法
17 | @Override
18 | public void swim() {
19 | System.out.println("鹅,鹅,鹅,曲项向天歌。白毛浮绿水,红掌拨清波。");
20 | }
21 |
22 | // 实现了接口的run方法
23 | @Override
24 | public void run() {
25 | System.out.println("槛外萧声轻荡漾,沙间鹅步满蹒跚。");
26 | }
27 |
28 | }
29 |
--------------------------------------------------------------------------------
/chapter07/src/com/object/polymorphic/TestFinal.java:
--------------------------------------------------------------------------------
1 | package com.object.polymorphic;
2 |
3 | //演示终类、终属性、终方法的用法
4 | public class TestFinal {
5 |
6 | public static void main(String[] args) {
7 | Chick chick = new Chick(); // 小鸡类是个终类,它不能被其它类继承
8 | chick.call();
9 | ChickenFinal chicken = new ChickenFinal(); // 创建一个鸡类的实例
10 | // ChickenFinal类的MALE属性是个终态属性,首次初始化后就不能再做修改
11 | System.out.println("MALE=" + chicken.MALE);
12 | // ChickenFinal类的FEMALE属性是个终态属性,首次初始化后就不能再做修改
13 | System.out.println("FEMALE=" + chicken.FEMALE);
14 | // ChickenFinal类的canSwim方法是个终态方法,子类不能重写该方法
15 | System.out.println("Chicken canSwim=" + chicken.canSwim());
16 | }
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/chapter07/src/com/object/inherit/DuckProtected.java:
--------------------------------------------------------------------------------
1 | package com.object.inherit;
2 |
3 | // 演示关键字protected的用法
4 | public class DuckProtected extends Bird {
5 |
6 | public DuckProtected(String name, int sex) {
7 | super(name, sex, "嘎嘎");
8 | }
9 |
10 | public void setSexType(int sexType) {
11 | super.setSexType(sexType);
12 | // 若想对父类的属性直接赋值,又不想对外开放该属性,可将父类的属性从private改为protected
13 | // 被protected修饰的成员,表示受保护,它允许子类访问,但不允许外部访问。
14 | // 倘若父类的属性被protected修饰,则子类可以直接读写该属性;
15 | // 倘若父类的方法被protected修饰,则子类可以直接读写该方法;
16 | // 所谓读方法,就是方法的调用操作;所谓写方法,就是方法的重载操作。
17 | sexName = (sexType == 0) ? "公" : "母";
18 | // super.sexName = (sexType==0) ? "公" : "母";
19 | }
20 |
21 | }
22 |
--------------------------------------------------------------------------------
/chapter08/src/com/special/singleton/SingletonNest.java:
--------------------------------------------------------------------------------
1 | package com.special.singleton;
2 |
3 | //嵌套方式实现单例模式。在SingletonHolder类首次加载时创建实例
4 | public class SingletonNest {
5 | private static int count = 0; // 创建实例的次数
6 |
7 | // 定义一个嵌套类,并在嵌套类的内部声明当前类的实例
8 | private static class SingletonHolder {
9 | private static SingletonNest instance = new SingletonNest(); // 创建一个外层类的实例
10 | }
11 |
12 | // 构造方法。每调用一次构造方法,实例的创建次数就加一
13 | private SingletonNest() {
14 | count++;
15 | }
16 |
17 | // 获取当前类的实例
18 | public static SingletonNest getInstance() {
19 | return SingletonHolder.instance;
20 | }
21 |
22 | // 获取实例的创建次数
23 | public int getCount() {
24 | return count;
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/chapter03/src/com/control/Feibonaqi.java:
--------------------------------------------------------------------------------
1 | package com.control;
2 |
3 | // 演示利用数组求解斐波那契数列
4 | public class Feibonaqi {
5 |
6 | public static void main(String[] args) {
7 | int[] rabbitNumbers; // 声明一个兔子数量(多少对)的数组变量
8 | rabbitNumbers = new int[12]; // 一年有12个月,故兔子数组大小为12
9 | // 循环计算兔子数组在每个月的兔子对数
10 | for (int i = 0; i < rabbitNumbers.length; i++) {
11 | if (i < 2) { // 数列的头两个元素都是1
12 | rabbitNumbers[i] = 1;
13 | } else { // 从第三个元素开始,每个元素都等于它的前面两个元素之和
14 | rabbitNumbers[i] = rabbitNumbers[i - 2] + rabbitNumbers[i - 1];
15 | }
16 | int month = i + 1;
17 | // 打印当前的月份和兔子对数
18 | System.out.println("第" + month + "个月,兔子对数=" + rabbitNumbers[i]);
19 | }
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/chapter08/src/com/special/singleton/SingletonLazy.java:
--------------------------------------------------------------------------------
1 | package com.special.singleton;
2 |
3 | //懒汉方式实现单例模式。在调用getInstance方法时才创建实例
4 | public class SingletonLazy {
5 | private static int count = 0; // 创建实例的次数
6 | private static SingletonLazy instance; // 声明一个当前类的实例
7 |
8 | // 构造方法。每调用一次构造方法,实例的创建次数就加一
9 | private SingletonLazy() {
10 | count++;
11 | }
12 |
13 | // 获取当前类的实例
14 | public static SingletonLazy getInstance() {
15 | // 第一次调用getInstance方式时,instance变量是空的,需要给它创建新实例
16 | if (instance == null) {
17 | instance = new SingletonLazy(); // 创建当前类的实例
18 | }
19 | return instance;
20 | }
21 |
22 | // 获取实例的创建次数
23 | public int getCount() {
24 | return count;
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/chapter13/src/com/swing/window/TestFrame.java:
--------------------------------------------------------------------------------
1 | package com.swing.window;
2 |
3 | import javax.swing.JFrame;
4 |
5 | //演示框架的用法
6 | public class TestFrame {
7 |
8 | public static void main(String[] args) {
9 | JFrame frame = new JFrame("测试框架的窗口"); // 创建一个窗口对象
10 | frame.setSize(400, 200); // 必须设置宽高,否则没有窗体
11 | //frame.setTitle("222"); // 设置窗口标题,否则窗口无标题
12 | frame.setLocationRelativeTo(null);// 将窗口居中。若无该方法,窗口将位于屏幕左上角
13 | //frame.setResizable(false); // 禁止调整窗口大小。默认允许调整窗口尺寸
14 | //frame.setBackground(Color.GREEN); // JFrame不能直接设置背景色
15 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置默认的关闭操作:退出程序
16 | frame.setVisible(true); // 必须设置为true,否则看不见
17 | }
18 |
19 | }
20 |
--------------------------------------------------------------------------------
/chapter02/src/com/arithmetic/numerical/Prefix.java:
--------------------------------------------------------------------------------
1 | package com.arithmetic.numerical;
2 |
3 | //演示数字的前缀,主要是标明该数字的归属进制
4 | public class Prefix {
5 |
6 | public static void main(String[] args) {
7 | int binary = 0b11; // 二进制数,0b也可以写成0B
8 | System.out.println("binary=" + binary);
9 | int octonary = 011; // 以0开头,后面非bB非xX的就是八进制数
10 | System.out.println("octonary=" + octonary);
11 | int hexadecimal = 0x11; // 十六进制数,0x也可以写成0X
12 | System.out.println("hexadecimal=" + hexadecimal);
13 | int hexLetter = 0xff; // 十六进制数不区分大小写,如ff也可以写成FF
14 | System.out.println("hexadecimal=" + hexLetter);
15 | int decimal = 11; // 没有任何前缀,则默认为十进制数
16 | System.out.println("decimal=" + decimal);
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/chapter03/src/com/control/process/ForLoop2.java:
--------------------------------------------------------------------------------
1 | package com.control.process;
2 |
3 | import java.util.Scanner;
4 |
5 | // 演示for语句内部break-continue的用法
6 | public class ForLoop2 {
7 |
8 | public static void main(String[] args) {
9 | System.out.println("长夜漫漫,无心睡眠,请给他设定一个睡醒的年限");
10 | Scanner scan = new Scanner(System.in); // 从控制台接收输入文本
11 | int limit = scan.nextInt(); // nextInt方法表示接收一个整数,以回车键结尾
12 | for (int year = 0;; year++) {
13 | System.out.println("已经过去的年份:" + year);
14 | if (year >= limit) { // 这里判断能否跳出循环
15 | System.out.println("他足足睡了这么多年:" + year);
16 | break; // 跳出循环。即跳到for循环的右花括号之后
17 | } else {
18 | continue; // 继续下一次循环。此时先执行year++,再执行循环内部语句
19 | }
20 | }
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/chapter07/src/com/object/account/TestAccount.java:
--------------------------------------------------------------------------------
1 | package com.object.account;
2 |
3 | // 演示各种账户类的使用
4 | public class TestAccount {
5 |
6 | public static void main(String[] args) {
7 | testBankAccount(); // 演示银行账户的操作
8 | }
9 |
10 | // 演示银行账户的操作
11 | private static void testBankAccount() {
12 | BankAccount bank = new BankAccount(); // 创建一个银行账户
13 | bank.saveCurrent(5000); // 活期账户存入5000元
14 | bank.saveDeposit(6, 5000); // 存入6个月定期的5000元
15 | System.out.println("第一次存款操作之后,"+bank.toString());
16 | bank.takeCurrent(2000); // 活期账户取出2000元
17 | bank.takeDeposit(6); // 取出6个月定期存款
18 | bank.saveDeposit(12, 5000); // 存入12个月定期的5000元
19 | System.out.println("第二次存款操作之后,"+bank.toString());
20 | }
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/chapter16/src/com/network/im_client/friend_list.fxml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/chapter03/src/com/control/process/Multipath.java:
--------------------------------------------------------------------------------
1 | package com.control.process;
2 |
3 | import java.util.Scanner;
4 |
5 | // 演示“else if”的用法
6 | public class Multipath {
7 |
8 | public static void main(String[] args) {
9 | System.out.println("凉风有信,秋月无边。打二字");
10 | System.out.println("获取“凉风有信”的谜底请按1,获取“秋月无边”的谜底请按2");
11 | Scanner scan = new Scanner(System.in); // 从控制台接收输入文本
12 | int seq = scan.nextInt(); // nextInt方法表示接收一个整数,以回车键结尾
13 | if (seq == 1) { // 条件式子1为真时,进入第一个if分支处理
14 | System.out.println("凉风有信的谜底是“讽”");
15 | } else if (seq == 2) { // 否则的话,继续判断条件式子2为真时,进入第二个if分支处理
16 | System.out.println("秋月无边的谜底是“二”");
17 | } else { // 否则(前面的判断条件都不满足),进入else分支处理
18 | System.out.println("您的按键有误");
19 | }
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/chapter13/src/com/swing/window/TestPanel.java:
--------------------------------------------------------------------------------
1 | package com.swing.window;
2 |
3 | import java.awt.Color;
4 |
5 | import javax.swing.JFrame;
6 | import javax.swing.JPanel;
7 |
8 | //演示面板的用法
9 | public class TestPanel {
10 |
11 | public static void main(String[] args) {
12 | JFrame frame = new JFrame("测试面板的窗口"); // 创建一个窗口对象
13 | frame.setSize(400, 200); // 必须设置宽高,否则没有窗体
14 | frame.setLocationRelativeTo(null);// 将窗口居中。若无该方法,窗口将位于屏幕左上角
15 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置默认的关闭操作:退出程序
16 |
17 | JPanel panel = new JPanel(); // 创建一个面板
18 | panel.setBackground(Color.GREEN); // 设置面板的背景
19 | frame.add(panel); // 在窗口上添加面板
20 |
21 | frame.setVisible(true); // 必须设置为true,否则看不见
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/chapter17/src/com/database/autocode/Student.java:
--------------------------------------------------------------------------------
1 | package com.database.autocode;
2 |
3 | import java.util.Date;
4 |
5 | //学生信息表
6 | public class Student {
7 | private int xuehao; // 学号
8 | private String name; // 姓名
9 | private Date birthday; // 生日
10 |
11 | public void setXuehao(int xuehao) {
12 | this.xuehao = xuehao;
13 | }
14 |
15 | public int getXuehao() {
16 | return this.xuehao;
17 | }
18 |
19 | public void setName(String name) {
20 | this.name = name;
21 | }
22 |
23 | public String getName() {
24 | return this.name;
25 | }
26 |
27 | public void setBirthday(Date birthday) {
28 | this.birthday = birthday;
29 | }
30 |
31 | public Date getBirthday() {
32 | return this.birthday;
33 | }
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/chapter03/src/com/control/process/WhileLoop3.java:
--------------------------------------------------------------------------------
1 | package com.control.process;
2 |
3 | import java.util.Scanner;
4 |
5 | // 演示while语句内部break-continue的用法
6 | public class WhileLoop3 {
7 |
8 | public static void main(String[] args) {
9 | System.out.println("长夜漫漫,无心睡眠,请给他设定一个睡醒的年限");
10 | Scanner scan = new Scanner(System.in); // 从控制台接收输入文本
11 | int limit = scan.nextInt(); // nextInt方法表示接收一个整数,以回车键结尾
12 | int year = 0;
13 | while (true) { // 当条件满足时,在循环内部持续处理
14 | System.out.println("已经过去的年份:" + year);
15 | if (year < limit) { // 这里判断能否跳出循环
16 | year++;
17 | continue; // 继续下一次循环
18 | } else {
19 | break; // 跳出循环。即跳到while循环的右花括号之后
20 | }
21 | }
22 | System.out.println("他足足睡了这么多年:" + year);
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/chapter03/src/com/control/SunziDingli.java:
--------------------------------------------------------------------------------
1 | package com.control;
2 |
3 | import java.util.Arrays;
4 |
5 | //演示孙子定理的求解过程(西方称作“中国剩余定理”)
6 | public class SunziDingli {
7 |
8 | public static void main(String[] arg) {
9 | // 有物不知其数,三三数之剩二,五五数之剩三,七七数之剩二。问物几何?
10 | int count = 0; // 数组的容量计数
11 | int[] numbers = new int[0]; // 符合条件的整数都放在这个数组
12 | for (int i = 1; i < 1000; i++) { // 查找一千之内所有符合要求的整数
13 | if (i%3==2 && i%5==3 && i%7==2) { // 找到了一个满足条件的整数
14 | count++; // 计数加一
15 | numbers = Arrays.copyOf(numbers, count); // 数组容量增大一个
16 | numbers[count-1] = i; // 往数组末尾填入刚才找到的整数
17 | }
18 | }
19 | for (int number : numbers) { // 遍历并打印所有找到的整数解
20 | System.out.println("符合孙子定理的整数number=" + number);
21 | }
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/chapter07/src/com/object/inherit/DuckPublic.java:
--------------------------------------------------------------------------------
1 | package com.object.inherit;
2 |
3 | //演示同名的父类属性、子类属性、输入参数三者的优先级顺序
4 | public class DuckPublic extends Bird {
5 |
6 | public DuckPublic(String name, int sex) {
7 | super(name, sex, "嘎嘎");
8 | }
9 |
10 | public void setSexType(int sexType) {
11 | super.setSexType(sexType);
12 | // 若想对父类的属性直接赋值,则考虑把父类的属性从private改为public
13 | super.sexName = (sexType == 0) ? "公" : "母";
14 | // 父类和子类拥有同名属性,则不带前缀的属性字段默认为子类属性
15 | // sexName = (sexType==0) ? "公" : "母";
16 | // this.sexName = (sexType==0) ? "公" : "母";
17 | }
18 |
19 | private String sexName; // 性别名称
20 |
21 | public void setSexName(String sexName) {
22 | // 输入参数与类的属性同名,则不带前缀的参数字段默认为输入参数
23 | this.sexName = sexName;
24 | }
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/chapter01/src/com/donghan/weijun/yexian/Yecheng.java:
--------------------------------------------------------------------------------
1 | package com.donghan.weijun.yexian; // 东汉帝国魏郡邺县
2 |
3 | // 此乃魏国首都邺城
4 | public class Yecheng {
5 |
6 | public static void tongquetai() {
7 | System.out.println("这里是铜雀台。");
8 | }
9 |
10 | public static void jinhutai() {
11 | System.out.println("这里是金虎台。");
12 | }
13 |
14 | public static void bingjingtai() {
15 | System.out.println("这里是冰井台。");
16 | }
17 |
18 | public static void main(String[] args) {
19 | // System.out.print("欢迎来到魏国的都城——邺城。");
20 | // System.out.print("这里是邺城的正南门——凤阳门。");
21 | System.out.println("欢迎来到魏国的都城——邺城。");
22 | System.out.println("这里是邺城的正南门——凤阳门。");
23 | tongquetai(); // 参观朱雀台
24 | jinhutai(); // 参观金虎台
25 | bingjingtai(); // 参观冰井台
26 | }
27 |
28 | }
29 |
--------------------------------------------------------------------------------
/chapter02/src/com/arithmetic/math/Angle.java:
--------------------------------------------------------------------------------
1 | package com.arithmetic.math;
2 |
3 | //演示数学函数库中的三角函数用法
4 | public class Angle {
5 |
6 | public static void main(String[] args) {
7 | double angle = 60; // 三角函数的角度
8 | // 弧度=该角度对应的弧长/半径。数学函数库Math专门提供了常量PI表示圆周率π的粗略值
9 | double radian = angle * Math.PI / 180;
10 | double sin = Math.sin(radian); // 求某弧度的正弦。求反正弦要调用asin方法
11 | System.out.println("sin=" + sin);
12 | double cos = Math.cos(radian); // 求某弧度的余弦。求反余弦要调用acos方法
13 | System.out.println("cos=" + cos);
14 | double tan = Math.tan(radian); // 求某弧度的正切。求反正切要调用atan方法
15 | System.out.println("tan=" + tan);
16 | // 求某弧度的余切。Math库未提供求余切值的方法,其实余切值就是正切值的倒数
17 | double ctan = 1.0 / tan;
18 | System.out.println("ctan=" + ctan);
19 | }
20 |
21 | }
22 |
--------------------------------------------------------------------------------
/chapter16/src/com/network/socket/TestTcpClient.java:
--------------------------------------------------------------------------------
1 | package com.network.socket;
2 |
3 | //演示Socket客户端的运行(TCP协议的可靠连接)
4 | public class TestTcpClient {
5 |
6 | public static void main(String[] args) {
7 | //testSendText(); // 发送文本消息
8 | testSendFile();
9 | }
10 |
11 | // 发送文本消息
12 | private static void testSendText() {
13 | SendText task = new SendText(); // 创建一个文本发送任务
14 | new Thread(task).start(); // 为文本发送任务开启分线程
15 | task.sendText("你好呀"); // 命令该线程发送文本消息
16 | task.sendText("Hello World"); // 命令该线程发送文本消息
17 | }
18 |
19 | // 发送本地文件
20 | private static void testSendFile() {
21 | new Thread(new SendFile("E:/bliss.jpg")).start(); // 为文件发送任务开启分线程
22 | new Thread(new SendFile("E:/qq_qrcode.png")).start(); // 为文件发送任务开启分线程
23 | }
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/chapter02/src/com/arithmetic/math/Science.java:
--------------------------------------------------------------------------------
1 | package com.arithmetic.math;
2 |
3 | //演示数学函数库中的科学函数用法
4 | public class Science {
5 |
6 | public static void main(String[] args) {
7 | double nine = 9;
8 | double sqrt = Math.sqrt(nine); // 开平方。对应数学符号√ ̄
9 | System.out.println("sqrt=" + sqrt);
10 | // 求n次方。pow的第一个参数为幂运算的底数,第二个参数为幂运算的指数
11 | double pow = Math.pow(nine, 2);
12 | System.out.println("pow=" + pow);
13 |
14 | double five = 5;
15 | double exp = Math.exp(five); // 求自然常数e的n次方
16 | System.out.println("exp=" + exp);
17 | double log = Math.log(exp); // 求自然对数,为exp方法的逆运算。对应数学函数lnN
18 | System.out.println("log=" + log);
19 | double log10 = Math.log10(100); // 求底数为10的对数。对应数学函数logN
20 | System.out.println("log10=" + log10);
21 | }
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/chapter08/src/com/special/inner/Tree.java:
--------------------------------------------------------------------------------
1 | package com.special.inner;
2 |
3 | //演示内部类的简单定义。这是一个树木类
4 | public class Tree {
5 | private String tree_name; // 树木名称
6 |
7 | public Tree(String tree_name) {
8 | this.tree_name = tree_name;
9 | }
10 |
11 | public void sprout() {
12 | System.out.println(tree_name + "发芽啦");
13 | // 外部类访问它的内部类,就像访问其它类一样,都要先创建类的实例,再访问它的成员
14 | Flower flower = new Flower("花朵");
15 | flower.bloom(); // 调用花儿对象的开花方法
16 | }
17 |
18 | // Flower类位于Tree类的内部,它是个内部类
19 | public class Flower {
20 | private String flower_name; // 花朵名称
21 |
22 | public Flower(String flower_name) {
23 | this.flower_name = flower_name;
24 | }
25 |
26 | public void bloom() {
27 | System.out.println(flower_name + "开花啦");
28 | }
29 | }
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/chapter03/src/com/control/array/OneDimensional.java:
--------------------------------------------------------------------------------
1 | package com.control.array;
2 |
3 | // 演示一维数组的简单用法
4 | public class OneDimensional {
5 |
6 | public static void main(String[] args) {
7 | // 以下是声明数组的第一种形式:“变量类型 数组名称[]”
8 | int numbers[];
9 | // 以下是分配数组空间的第一种形式:在方括号内填入数字,表示数组有多大
10 | numbers = new int[4];
11 | // 数组名称后面的“[数字]”,就是数组元素的下标,表示当前操作的是第几个数组元素
12 | numbers[0] = 2; // 给下标为0的数组元素赋值,下标0对应日常生活中的第一个
13 | numbers[1] = 3; // 给下标为1的数组元素赋值,下标1对应日常生活中的第二个
14 | numbers[2] = 5; // 给下标为2的数组元素赋值,下标2对应日常生活中的第三个
15 | numbers[3] = 7; // 给下标为3的数组元素赋值,下标3对应日常生活中的第四个
16 | // 下面通过循环语句依次读出数组中的所有元素,“数组名称.length”表示获取该数组的长度(数组大小)
17 | for (int i = 0; i < numbers.length; i++) {
18 | System.out.println("number = " + numbers[i]); // 打印下标为i的数组元素
19 | }
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/chapter03/src/com/control/process/ForLoop3.java:
--------------------------------------------------------------------------------
1 | package com.control.process;
2 |
3 | import java.util.Scanner;
4 |
5 | // 演示for语句在圆括号内全部留空的写法
6 | public class ForLoop3 {
7 |
8 | public static void main(String[] args) {
9 | System.out.println("长夜漫漫,无心睡眠,请给他设定一个睡醒的年限");
10 | Scanner scan = new Scanner(System.in); // 从控制台接收输入文本
11 | int limit = scan.nextInt(); // nextInt方法表示接收一个整数,以回车键结尾
12 | int year = 0; // 把式子A挪到整个循环的前面
13 | for (;;) { // for语句后面的三个位置全部留空
14 | System.out.println("已经过去的年份:" + year);
15 | if (year >= limit) { // 这里判断能否跳出循环
16 | System.out.println("他足足睡了这么多年:" + year);
17 | break; // 跳出循环。即跳到for循环的右花括号之后
18 | } else {
19 | year++; // 把式子C挪到continue之前
20 | continue; // 继续下一次循环。此时先执行year++,再执行循环内部语句
21 | }
22 | }
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/chapter11/src/com/io/huffman/TestHuffman.java:
--------------------------------------------------------------------------------
1 | package com.io.huffman;
2 |
3 | //演示如何利用哈夫曼算法来压缩文件
4 | public class TestHuffman {
5 |
6 | public static void main(String[] args) {
7 | testCompress(); // 测试文件压缩的过程
8 | testDecompress(); // 测试文件解压的过程
9 | }
10 |
11 | // 测试文件压缩的过程
12 | private static void testCompress() {
13 | // 创建压缩工具
14 | Compress compress = new Compress();
15 | // 压缩文件
16 | compress.compress("E:/test/aaa.java", "E:/test/aaa.hfm");
17 | System.out.println("Compress Done");
18 | }
19 |
20 | // 测试文件解压的过程
21 | private static void testDecompress() {
22 | // 创建解压工具
23 | Decompress decompress = new Decompress();
24 | // 解压已压缩的目标文件
25 | decompress.decompress("E:/test/aaa.hfm", "E:/test/aaa2.java");
26 | System.out.println("Decompress Done");
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/chapter16/src/log4j.properties:
--------------------------------------------------------------------------------
1 | #日志的根配置,等号后面第一个是日志级别,其后的文字标签指定了输出位置,例如Console代表控制台、LogFile代表日志文件等等
2 | log4j.rootLogger=DEBUG,Console,LogFile
3 |
4 | #Console控制台的配置
5 | log4j.appender.Console=org.apache.log4j.ConsoleAppender
6 | log4j.appender.Console.layout=org.apache.log4j.PatternLayout
7 | #每行日志的格式定义。%d代表时间,%t代表线程名称,%-5p代表日志等级,%c代表当前类的完整路径,%m代表日志文本,%n代表换行符
8 | log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
9 |
10 | #LogFile日志文件的配置
11 | log4j.appender.LogFile=org.apache.log4j.DailyRollingFileAppender
12 | #日志文件的保存路径
13 | log4j.appender.LogFile.File=${WORKDIR}/http.log
14 | log4j.appender.LogFile.layout=org.apache.log4j.PatternLayout
15 | log4j.appender.LogFile.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
16 | #设置输出日志文件编码(可以避免中文乱码)
17 | log4j.appender.LogFile.Encoding=UTF-8
18 |
--------------------------------------------------------------------------------
/chapter03/src/com/control/array/TwoDimensional2.java:
--------------------------------------------------------------------------------
1 | package com.control.array;
2 |
3 | //演示二维数组的初始化赋值
4 | public class TwoDimensional2 {
5 |
6 | public static void main(String[] args) {
7 | // 以下是声明二维数组的第二种形式:“变量类型[][] 数组名称”
8 | // 其中“变量类型[][]”构成了数组类型,例如int[][]被称作二维整型数组
9 | // 以下是分配二维数组空间的第二种形式:
10 | // 方括号内留空,然后紧跟花括号,花括号内部是以逗号分隔的几个一维数组
11 | double[][] triangle = new double[][] { new double[] { -2.0, 0.0 },
12 | new double[] { 0.0, -1.0 }, new double[] { 2.0, 1.0 } };
13 | // 通过循环语句依次读出数组中的所有元素。 “二维数组名称.length”表示获取该数组的行数
14 | for (int i = 0; i < triangle.length; i++) {
15 | // “triangle[i].length”表示获取该数组第i行的列数
16 | for (int j = 0; j < triangle[i].length; j++) {
17 | // 打印第i行第j列的数组元素
18 | System.out.println("triangle[" + i + "][" + j + "]=" + triangle[i][j]);
19 | }
20 | }
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/chapter10/src/com/addition/reflect/Chicken.java:
--------------------------------------------------------------------------------
1 | package com.addition.reflect;
2 |
3 | //定义一个鸡类
4 | public class Chicken {
5 |
6 | // 定义一个名称属性
7 | protected String name;
8 | // 定义一个性别属性
9 | protected int sex;
10 |
11 | // 定义一个叫唤方法
12 | public void call() {
13 | System.out.println("半夜鸡叫");
14 | }
15 |
16 | // 以下利用final修饰成员属性和成员方法
17 | public final int MALE = 0; // 雄性
18 | public final int FEMALE = 1; // 雌性
19 |
20 | // 定义一个能否游泳的方法
21 | public final boolean canSwim() {
22 | return false;
23 | }
24 |
25 | private void setName(String name) { // 设置名称
26 | this.name = name;
27 | }
28 |
29 | private String getName() { // 获取名称
30 | return this.name;
31 | }
32 |
33 | private void setSex(int sex) { // 设置性别
34 | this.sex = sex;
35 | }
36 |
37 | private int getSex() { // 获取性别
38 | return this.sex;
39 | }
40 |
41 | }
42 |
--------------------------------------------------------------------------------
/chapter14/src/com/javafx/loan/Main.java:
--------------------------------------------------------------------------------
1 | package com.javafx.loan;
2 |
3 | import javafx.application.Application;
4 | import javafx.fxml.FXMLLoader;
5 | import javafx.scene.Parent;
6 | import javafx.scene.Scene;
7 | import javafx.stage.Stage;
8 |
9 | //房贷计算器的程序入口(FXML布局控件)
10 | public class Main extends Application {
11 |
12 | @Override
13 | public void start(Stage stage) throws Exception { // 应用程序开始运行
14 | stage.setTitle("房贷计算器"); // 设置舞台的标题
15 | // 从FXML资源文件中加载程序的初始界面
16 | Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
17 | Scene scene = new Scene(root, 400, 180); // 创建一个场景
18 | stage.setScene(scene); // 设置舞台的场景
19 | stage.setResizable(false); // 设置舞台的尺寸是否允许变化
20 | stage.show(); // 显示舞台
21 | }
22 |
23 | public static void main(String[] args) {
24 | launch(args); // 启动JavaFX应用,接下来会跳到start方法
25 | }
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/chapter15/src/com/concurrent/mouse/hit_mouse.fxml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/chapter17/src/com/database/poem/PoemMain.java:
--------------------------------------------------------------------------------
1 | package com.database.poem;
2 |
3 | import javafx.application.Application;
4 | import javafx.fxml.FXMLLoader;
5 | import javafx.scene.Parent;
6 | import javafx.scene.Scene;
7 | import javafx.stage.Stage;
8 |
9 | //诗歌赏析软件的程序入口
10 | public class PoemMain extends Application {
11 |
12 | @Override
13 | public void start(Stage stage) throws Exception { // 应用程序开始运行
14 | stage.setTitle("古诗三百首"); // 设置舞台的标题
15 | // 从FXML资源文件中加载程序的初始界面
16 | Parent root = FXMLLoader.load(getClass().getResource("poem.fxml"));
17 | Scene scene = new Scene(root, 800, 500); // 创建一个场景
18 | stage.setScene(scene); // 设置舞台的场景
19 | stage.setResizable(true); // 设置舞台的尺寸是否允许变化
20 | stage.show(); // 显示舞台
21 | }
22 |
23 | public static void main(String[] args) {
24 | launch(args); // 启动JavaFX应用,接下来会跳到start方法
25 | }
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/chapter14/src/com/javafx/scene/TestHello.java:
--------------------------------------------------------------------------------
1 | package com.javafx.scene;
2 |
3 | import javafx.application.Application;
4 | import javafx.scene.Group;
5 | import javafx.scene.Scene;
6 | import javafx.scene.paint.Color;
7 | import javafx.stage.Stage;
8 |
9 | //演示简单的JavaFX程序
10 | //JavaFX程序的入口类继承自Application
11 | public class TestHello extends Application {
12 |
13 | public static void main(String[] args) {
14 | launch(args); // 启动JavaFX应用,接下来会跳到start方法
15 | }
16 |
17 | @Override
18 | public void start(Stage stage) { // 应用程序开始运行
19 | stage.setTitle("Hello World"); // 设置舞台的标题
20 | Group group = new Group(); // 创建一个小组
21 | Scene scene = new Scene(group, 400, 50, Color.WHITE); // 创建一个场景
22 | stage.setScene(scene); // 设置舞台的场景
23 | stage.setResizable(false); // 设置舞台的尺寸是否允许变化
24 | stage.show(); // 显示舞台。相当于JFrame的setVisible(true)
25 | }
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/chapter14/src/com/javafx/fxml/LoginMain.java:
--------------------------------------------------------------------------------
1 | package com.javafx.fxml;
2 |
3 | import javafx.application.Application;
4 | import javafx.fxml.FXMLLoader;
5 | import javafx.scene.Parent;
6 | import javafx.scene.Scene;
7 | import javafx.stage.Stage;
8 |
9 | //登录窗口的程序入口(FXML布局控件)
10 | public class LoginMain extends Application {
11 |
12 | @Override
13 | public void start(Stage stage) throws Exception { // 应用程序开始运行
14 | stage.setTitle("登录窗口"); // 设置舞台的标题
15 | // 从FXML资源文件中加载程序的初始界面
16 | Parent root = FXMLLoader.load(getClass().getResource("login_with_expand.fxml"));
17 | Scene scene = new Scene(root, 410, 240); // 创建一个场景
18 | stage.setScene(scene); // 设置舞台的场景
19 | stage.setResizable(true); // 设置舞台的尺寸是否允许变化
20 | stage.show(); // 显示舞台
21 | }
22 |
23 | public static void main(String[] args) {
24 | launch(args); // 启动JavaFX应用,接下来会跳到start方法
25 | }
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/chapter15/src/com/concurrent/mouse/HitMouseMain.java:
--------------------------------------------------------------------------------
1 | package com.concurrent.mouse;
2 |
3 | import javafx.application.Application;
4 | import javafx.fxml.FXMLLoader;
5 | import javafx.scene.Parent;
6 | import javafx.scene.Scene;
7 | import javafx.stage.Stage;
8 |
9 | //打地鼠游戏的程序入口
10 | public class HitMouseMain extends Application {
11 |
12 | @Override
13 | public void start(Stage stage) throws Exception { // 应用程序开始运行
14 | stage.setTitle("打地鼠游戏"); // 设置舞台的标题
15 | // 从FXML资源文件中加载程序的初始界面
16 | Parent root = FXMLLoader.load(getClass().getResource("hit_mouse.fxml"));
17 | Scene scene = new Scene(root, 560, 700); // 创建一个场景
18 | stage.setScene(scene); // 设置舞台的场景
19 | stage.setResizable(false); // 设置舞台的尺寸是否允许变化
20 | stage.show(); // 显示舞台
21 | }
22 |
23 | public static void main(String[] args) {
24 | launch(args); // 启动JavaFX应用,接下来会跳到start方法
25 | }
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/chapter08/src/com/special/builder/TestBuilder.java:
--------------------------------------------------------------------------------
1 | package com.special.builder;
2 |
3 | //演示建造者模式的用户
4 | public class TestBuilder {
5 |
6 | public static void main(String[] args) {
7 | testCommon(); // 测试普通方式的用户实例
8 | testBuilder(); // 测试建造者模式的用户实例
9 | }
10 |
11 | // 测试普通方式的用户实例
12 | private static void testCommon() {
13 | // 创建用户实例,在构造方法中传入各属性
14 | UserCommon user = new UserCommon("张三", "111111");
15 | System.out.println("普通方式,用户名为"+user.getName()+", 密码为"+user.getPassword());
16 | }
17 |
18 | // 测试建造者模式的用户实例
19 | private static void testBuilder() {
20 | User.Builder builder = new User.Builder(); // 创建用户类的建造者实例
21 | builder.setName("李四"); // 设置用户的姓名
22 | builder.setPassword("888888"); // 设置用户的密码
23 | User user = builder.build(); // 通过建造者实例获取用户的实例
24 | System.out.println("建造者模式,用户名为"+user.getName()+", 密码为"+user.getPassword());
25 | }
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/chapter10/src/com/addition/reflect/TestReflectClass.java:
--------------------------------------------------------------------------------
1 | package com.addition.reflect;
2 |
3 | //演示如何利用反射获得某个类的类型
4 | public class TestReflectClass {
5 |
6 | public static void main(String[] arg) {
7 | // 第一种方式:通过“类名.class”获取
8 | Class clsFromClass = Cock.class;
9 | System.out.println("clsFromClass name = " + clsFromClass.getName());
10 |
11 | // 第二种方式:通过“实例名.getClass()”获取
12 | Cock cock = new Cock();
13 | Class clsFromInstance = cock.getClass();
14 | System.out.println("clsFromInstance name = " + clsFromInstance.getName());
15 |
16 | // 第三种方式:通过该类的完整路径字符串获取
17 | try { // 开始小心翼翼地尝试,随时准备捕捉异常
18 | Class clsFromString = Class.forName("com.addition.reflect.Cock");
19 | System.out.println("clsFromString name = " + clsFromString.getName());
20 | } catch (ClassNotFoundException e) { // 捕捉到“类型未找到”异常
21 | e.printStackTrace();
22 | }
23 | }
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/chapter14/src/com/javafx/mortgage/MortgageMain.java:
--------------------------------------------------------------------------------
1 | package com.javafx.mortgage;
2 |
3 | import javafx.application.Application;
4 | import javafx.fxml.FXMLLoader;
5 | import javafx.scene.Parent;
6 | import javafx.scene.Scene;
7 | import javafx.stage.Stage;
8 |
9 | //房贷计算器的程序入口(FXML布局控件)
10 | public class MortgageMain extends Application {
11 |
12 | @Override
13 | public void start(Stage stage) throws Exception { // 应用程序开始运行
14 | stage.setTitle("房贷计算器"); // 设置舞台的标题
15 | // 从FXML资源文件中加载程序的初始界面
16 | Parent root = FXMLLoader.load(getClass().getResource("mortgage.fxml"));
17 | Scene scene = new Scene(root, 400, 620); // 创建一个场景
18 | stage.setScene(scene); // 设置舞台的场景
19 | stage.setResizable(false); // 设置舞台的尺寸是否允许变化
20 | stage.show(); // 显示舞台
21 | }
22 |
23 | public static void main(String[] args) {
24 | launch(args); // 启动JavaFX应用,接下来会跳到start方法
25 | }
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/chapter17/src/com/database/autocode/AutoCodeMain.java:
--------------------------------------------------------------------------------
1 | package com.database.autocode;
2 |
3 | import javafx.application.Application;
4 | import javafx.fxml.FXMLLoader;
5 | import javafx.scene.Parent;
6 | import javafx.scene.Scene;
7 | import javafx.stage.Stage;
8 |
9 | //代码生成工具的程序入口
10 | public class AutoCodeMain extends Application {
11 |
12 | @Override
13 | public void start(Stage stage) throws Exception { // 应用程序开始运行
14 | stage.setTitle("代码生成工具"); // 设置舞台的标题
15 | // 从FXML资源文件中加载程序的初始界面
16 | Parent root = FXMLLoader.load(getClass().getResource("auto_code.fxml"));
17 | Scene scene = new Scene(root, 500, 350); // 创建一个场景
18 | stage.setScene(scene); // 设置舞台的场景
19 | stage.setResizable(false); // 设置舞台的尺寸是否允许变化
20 | stage.show(); // 显示舞台
21 | }
22 |
23 | public static void main(String[] args) {
24 | launch(args); // 启动JavaFX应用,接下来会跳到start方法
25 | }
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/chapter15/src/com/concurrent/stopwatch/WatchMain.java:
--------------------------------------------------------------------------------
1 | package com.concurrent.stopwatch;
2 |
3 | import javafx.application.Application;
4 | import javafx.fxml.FXMLLoader;
5 | import javafx.scene.Parent;
6 | import javafx.scene.Scene;
7 | import javafx.stage.Stage;
8 |
9 | //秒表计时器的程序入口(FXML布局控件)
10 | public class WatchMain extends Application {
11 |
12 | @Override
13 | public void start(Stage stage) throws Exception { // 应用程序开始运行
14 | stage.setTitle("秒表计时器"); // 设置舞台的标题
15 | // 从FXML资源文件中加载程序的初始界面
16 | Parent root = FXMLLoader.load(getClass().getResource("watch.fxml"));
17 | Scene scene = new Scene(root, 400, 150); // 创建一个场景
18 | stage.setScene(scene); // 设置舞台的场景
19 | stage.setResizable(false); // 设置舞台的尺寸是否允许变化
20 | stage.show(); // 显示舞台
21 | }
22 |
23 | public static void main(String[] args) {
24 | launch(args); // 启动JavaFX应用,接下来会跳到start方法
25 | }
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/chapter16/src/com/network/httptest/HttpTesterMain.java:
--------------------------------------------------------------------------------
1 | package com.network.httptest;
2 |
3 | import javafx.application.Application;
4 | import javafx.fxml.FXMLLoader;
5 | import javafx.scene.Parent;
6 | import javafx.scene.Scene;
7 | import javafx.stage.Stage;
8 |
9 | //HTTP测试工具的程序入口
10 | public class HttpTesterMain extends Application {
11 |
12 | @Override
13 | public void start(Stage stage) throws Exception { // 应用程序开始运行
14 | stage.setTitle("HTTP测试工具"); // 设置舞台的标题
15 | // 从FXML资源文件中加载程序的初始界面
16 | Parent root = FXMLLoader.load(getClass().getResource("http_tester.fxml"));
17 | Scene scene = new Scene(root, 800, 300); // 创建一个场景
18 | stage.setScene(scene); // 设置舞台的场景
19 | stage.setResizable(true); // 设置舞台的尺寸是否允许变化
20 | stage.show(); // 显示舞台
21 | }
22 |
23 | public static void main(String[] args) {
24 | launch(args); // 启动JavaFX应用,接下来会跳到start方法
25 | }
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/chapter03/src/com/control/process/Condition2.java:
--------------------------------------------------------------------------------
1 | package com.control.process;
2 |
3 | import java.util.Scanner;
4 |
5 | // 演示三元运算符“?:”的用法
6 | public class Condition2 {
7 |
8 | public static void main(String[] args) {
9 | System.out.println("凉风有信,秋月无边。打二字");
10 | System.out.println("获取“凉风有信”的谜底请按1,获取“秋月无边”的谜底请按2");
11 | Scanner scan = new Scanner(System.in); // 从控制台接收输入文本
12 | int seq = scan.nextInt(); // nextInt方法表示接收一个整数,以回车键结尾
13 | // if (seq == 1) { // 为1的情况
14 | // seq = 1;
15 | // } else { // 不为1的情况
16 | // seq = 2;
17 | // }
18 | // “式子A ? 式子B : 式子C”里的问号加冒号构成了一个三元运算符,
19 | // 当式子A成立时,运算结果为B,否则(式子A不成立)结果为C
20 | seq = seq == 1 ? 1 : 2; // 等价于 seq = (seq==1)?1:2
21 | if (seq == 1) { // 按1时打印“凉风有信”的谜底
22 | System.out.println("凉风有信的谜底是“讽”");
23 | }
24 | if (seq == 2) { // 按2时打印“秋月无边”的谜底
25 | System.out.println("秋月无边的谜底是“二”");
26 | }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/chapter07/src/com/object/trade/DepositAccount.java:
--------------------------------------------------------------------------------
1 | package com.object.trade;
2 |
3 | // 定一个定期存款账户类
4 | public class DepositAccount extends Account {
5 | private int depositTerm; // 存款期限,单位月
6 |
7 | // 存款账户的构造方法
8 | public DepositAccount(int depositTerm, String depositName) {
9 | super(depositName);
10 | this.depositTerm = depositTerm;
11 | setUnit("元"); // 设置余额的单位
12 | }
13 |
14 | // 获取存款的期限
15 | public int getDepositTerm() {
16 | return this.depositTerm;
17 | }
18 |
19 | // 这里的定期存款只能一次性全部取出,不能分批取出,适合整存整取、零存整取。
20 | public boolean takeOut(long amount) {
21 | if (amount == getBalance()) { // 全部取出
22 | return super.takeOut(amount);
23 | } else {
24 | return false;
25 | }
26 | }
27 |
28 | // 输出存款现金账户信息
29 | public String toString() {
30 | String desc = String.format("存款期限为%d个月,%s", this.depositTerm,
31 | super.toString());
32 | return desc;
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/chapter08/src/com/special/behavior/ExpandGoose.java:
--------------------------------------------------------------------------------
1 | package com.special.behavior;
2 |
3 | //定义实现了扩展接口的鹅类
4 | public class ExpandGoose extends Bird implements ExpandBehavior {
5 |
6 | public ExpandGoose(String name, int sexType) {
7 | super(name, sexType);
8 | }
9 |
10 | // 实现了接口的fly方法
11 | @Override
12 | public void fly() {
13 | System.out.println("鹅飞不高,也飞不远。");
14 | }
15 |
16 | // 实现了接口的swim方法
17 | @Override
18 | public void swim() {
19 | System.out.println("鹅,鹅,鹅,曲项向天歌。白毛浮绿水,红掌拨清波。");
20 | }
21 |
22 | // 实现了接口的run方法
23 | @Override
24 | public void run() {
25 | System.out.println("槛外萧声轻荡漾,沙间鹅步满蹒跚。");
26 | }
27 |
28 | // 根据产地和祖先拼接并打印该动物的描述文字
29 | public void show(String place, String ancestor) {
30 | // getOrigin是来自扩展接口ExpandBehavior的默认方法,可以在实现类中直接使用
31 | String desc = getOrigin(place, getName(), ancestor);
32 | System.out.println(desc);
33 | }
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/chapter10/src/com/addition/annotation/Chicken.java:
--------------------------------------------------------------------------------
1 | package com.addition.annotation;
2 |
3 | //该注解表示屏蔽“未使用”这种警告
4 | @SuppressWarnings("unused")
5 | //定义一个抽象的鸡类
6 | abstract public class Chicken {
7 |
8 | // 定义一个名称属性
9 | public String name;
10 | // 定义一个性别属性
11 | public int sex;
12 |
13 | // 定义一个抽象的叫唤方法。注意后面没有花括号,并且以分号结尾
14 | abstract public void call();
15 |
16 | // 以下利用final修饰成员属性和成员方法
17 | public final int MALE = 0; // 雄性
18 | public final int FEMALE = 1; // 雌性
19 |
20 | // 定义一个能否游泳的方法
21 | public final boolean canSwim() {
22 | return false;
23 | }
24 |
25 | private void setName(String name) { // 设置名称
26 | this.name = name;
27 | }
28 |
29 | private String getName() { // 获取名称
30 | return this.name;
31 | }
32 |
33 | private void setSex(int sex) { // 设置性别
34 | this.sex = sex;
35 | }
36 |
37 | private int getSex() { // 获取性别
38 | return this.sex;
39 | }
40 |
41 | }
42 |
--------------------------------------------------------------------------------
/chapter08/src/com/special/inner/TreeNest.java:
--------------------------------------------------------------------------------
1 | package com.special.inner;
2 |
3 | //演示嵌套类的定义
4 | public class TreeNest {
5 | private String tree_name; // 树木名称
6 |
7 | public TreeNest(String tree_name) {
8 | this.tree_name = tree_name;
9 | }
10 |
11 | public void sprout() {
12 | System.out.println(tree_name + "发芽啦");
13 | }
14 |
15 | // Flower类虽然位于TreeNest类的里面,但是它被static修饰,故而与TreeNest类的关系比起一般的内部类要弱。
16 | // 为了与一般的内部类区别开来,这里的Flower类被叫做嵌套类。
17 | public static class Flower {
18 | private String flower_name; // 花朵名称
19 |
20 | public Flower(String flower_name) {
21 | this.flower_name = flower_name;
22 | }
23 |
24 | public void bloom() {
25 | System.out.println(flower_name + "开花啦");
26 | }
27 |
28 | public void bloomOuterTree() {
29 | // 注意下面的写法是错误的,嵌套类不能直接访问外层类的成员
30 | // System.out.println(TreeNest.this.tree_name+"的"+flower_name+"开花啦");
31 | }
32 | }
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/chapter03/src/com/control/array/TwoDimensional.java:
--------------------------------------------------------------------------------
1 | package com.control.array;
2 |
3 | // 演示二维数组的简单用法
4 | public class TwoDimensional {
5 |
6 | public static void main(String[] args) {
7 | // 以下是声明二维数组的第一种形式:“变量类型 数组名称[][]”
8 | double triangle[][];
9 | // 以下是分配二维数组空间的第一种形式:在两对方括号内分别填入数字,表示数组有几行几列
10 | triangle = new double[3][2];
11 | // 数组名称后面的“[数字][数字]”为数组元素的行列下标,表示当前操作第几行第几列的元素
12 | triangle[0][0] = -2.0;
13 | triangle[0][1] = 0.0;
14 | triangle[1][0] = 0.0;
15 | triangle[1][1] = -1.0;
16 | triangle[2][0] = 2.0;
17 | triangle[2][1] = 1.0;
18 | // 通过循环语句依次读出数组中的所有元素。 “二维数组名称.length”表示获取该数组的行数
19 | for (int i = 0; i < triangle.length; i++) {
20 | // “triangle[i].length”表示获取该数组第i行的列数
21 | for (int j = 0; j < triangle[i].length; j++) {
22 | // 打印第i行第j列的数组元素
23 | System.out.println("triangle[" + i + "][" + j + "]=" + triangle[i][j]);
24 | }
25 | }
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/chapter09/src/com/collect/generic/TestClass.java:
--------------------------------------------------------------------------------
1 | package com.collect.generic;
2 |
3 | import java.util.Arrays;
4 | import java.util.List;
5 |
6 | //演示如何使用自定义的泛型类
7 | public class TestClass {
8 |
9 | public static void main(String[] arg) {
10 | // 清单List是系统自带的泛型类
11 | // 数组工具Arrays的asList方法可以把一系列元素直接赋值给清单对象
12 | List doubleList = Arrays.asList(1.1, 2D, 3.1415926, 11.11);
13 | System.out.println("doubleList.toString()=" + doubleList.toString());
14 | // 泛型类允许在声明变量时指定某几个内部参数的参数类型
15 | // 泛型实例的参数类型跟在类名称后面,以尖括号包裹
16 | SimpleList simpleList = new SimpleList(doubleList);
17 | simpleList.setDelimiter(" | ");
18 | System.out.println("simpleList.toString()=" + simpleList.toString());
19 | // 打印清单中最长的元素
20 | System.out.println("最长的元素=" + simpleList.getMaxLengthItem());
21 | // 打印清单中最短的元素
22 | System.out.println("最短的元素=" + simpleList.getMinLengthItem());
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/chapter03/src/com/control/process/Multipath2.java:
--------------------------------------------------------------------------------
1 | package com.control.process;
2 |
3 | import java.util.Scanner;
4 |
5 | // 演示switch-case-default的用法
6 | public class Multipath2 {
7 |
8 | public static void main(String[] args) {
9 | System.out.println("凉风有信,秋月无边。打二字");
10 | System.out.println("获取“凉风有信”的谜底请按1,获取“秋月无边”的谜底请按2");
11 | Scanner scan = new Scanner(System.in); // 从控制台接收输入文本
12 | int seq = scan.nextInt(); // nextInt方法表示接收一个整数,以回车键结尾
13 | // switch允许判断某个变量的多个取值,并分别进行单独处理
14 | switch (seq) {
15 | case 1: // seq值为1时进入该分支
16 | System.out.println("凉风有信的谜底是“讽”");
17 | break; // 跳出多路分支。即跳到switch分支的右花括号之后
18 | case 2: // seq值为2时进入该分支
19 | System.out.println("秋月无边的谜底是“二”");
20 | break; // 跳出多路分支。即跳到switch分支的右花括号之后
21 | default: // seq值为其它时进入该分支
22 | System.out.println("您的按键有误");
23 | break; // 跳出多路分支。即跳到switch分支的右花括号之后
24 | }
25 | System.out.println("猜谜结束");
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/chapter11/src/com/io/bio/UserInfo.java:
--------------------------------------------------------------------------------
1 | package com.io.bio;
2 |
3 | import java.io.Serializable;
4 |
5 | //定义一个可序列化的用户信息类。实现Serializable接口表示当前类支持序列化
6 | public class UserInfo implements Serializable {
7 | private static final long serialVersionUID = 1L; // 该类的实例在序列化时的版本编码
8 | private String name; // 用户名
9 | private String phone; // 手机号码
10 | // 关键字transient可让它所修饰的字段无法序列化,也就是说,序列化无法保存该字段的数值
11 | private transient String password; // 密码
12 |
13 | public void setName(String name) {
14 | this.name = name;
15 | }
16 |
17 | public String getName() {
18 | return this.name;
19 | }
20 |
21 | public void setPhone(String phone) {
22 | this.phone = phone;
23 | }
24 |
25 | public String getPhone() {
26 | return this.phone;
27 | }
28 |
29 | public void setPassword(String password) {
30 | this.password = password;
31 | }
32 |
33 | public String getPassword() {
34 | return this.password;
35 | }
36 |
37 | }
38 |
--------------------------------------------------------------------------------
/chapter14/src/com/javafx/fxml/login.fxml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/chapter09/src/com/collect/algorithm/LruMap.java:
--------------------------------------------------------------------------------
1 | package com.collect.algorithm;
2 |
3 | import java.util.LinkedHashMap;
4 | import java.util.Map;
5 |
6 | //把LinkedHashMap改造成为LRU(最久未使用)的数据结构。使用了泛型类
7 | public class LruMap extends LinkedHashMap {
8 | private static final long serialVersionUID = -1L;
9 |
10 | private int maxSize = 6; // 最大容量
11 |
12 | public LruMap(int maxSize) {
13 | // LinkedHashMap构造方法的三个输入参数说明:
14 | // initialCapacity 初始容量
15 | // loadFactor 加载因子,一般是0.75f
16 | // accessOrder 排序规则。false 基于插入时间;true 基于访问时间
17 | super(0, 0.75f, true);
18 | if (maxSize > 0) { // 最大容量必须为自然数
19 | this.maxSize = maxSize;
20 | }
21 | }
22 |
23 | // 重写removeEldestEntry方法,当LRU中的元素多于上限时,删除最久未使用的元素
24 | protected boolean removeEldestEntry(Map.Entry eldest) {
25 | if (size() > maxSize) { // 队列中的元素个数超过了最大容量
26 | return true; // 返回true表示允许移除最久未使用的元素
27 | }
28 | return false;
29 | }
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/chapter12/src/com/awt/window/TestFrame.java:
--------------------------------------------------------------------------------
1 | package com.awt.window;
2 |
3 | import java.awt.Color;
4 | import java.awt.Frame;
5 | import java.awt.event.WindowAdapter;
6 | import java.awt.event.WindowEvent;
7 |
8 | //演示如何显示窗口
9 | public class TestFrame {
10 |
11 | public static void main(String[] args) {
12 | final Frame frame = new Frame("测试窗口"); // 创建一个窗口对象
13 |
14 | frame.addWindowListener(new WindowAdapter() { // 为窗口注册监听器,实现窗口关闭功能
15 | public void windowClosing(WindowEvent e) { // 单击了窗口右上角的叉号按钮
16 | frame.dispose(); // 关闭窗口
17 | }
18 | });
19 |
20 | frame.setSize(400, 200); // 必须设置宽高,否则没有窗体
21 | //frame.setTitle("222"); // 设置窗口标题,或者在Frame的构造方法中直接填写标题文字也行
22 | frame.setLocationRelativeTo(null);// 将窗口居中。若无该方法,窗口将位于屏幕左上角
23 | //frame.setResizable(false); // 禁止调整窗口大小。默认允许调整窗口尺寸
24 | frame.setBackground(Color.GREEN); // 设置窗口背景色。默认白色
25 | frame.setVisible(true); // 必须设置为true,否则看不见
26 | }
27 |
28 | }
29 |
--------------------------------------------------------------------------------
/chapter16/src/com/network/im_client/FriendListMain.java:
--------------------------------------------------------------------------------
1 | package com.network.im_client;
2 |
3 | import javafx.fxml.FXMLLoader;
4 | import javafx.scene.Parent;
5 | import javafx.scene.Scene;
6 | import javafx.stage.Stage;
7 |
8 | //聊天工具好友列表的程序入口
9 | public class FriendListMain {
10 | private Stage mStage; // 当前程序的舞台
11 |
12 | public FriendListMain(Stage stage) {
13 | mStage = stage;
14 | }
15 |
16 | // 显示窗口
17 | public void show() {
18 | try {
19 | mStage.setTitle("好友列表"); // 设置舞台的标题
20 | mStage.getIcons().add(QQPartner.QQ_LOGO); // 设置标题栏图标
21 | // 从FXML资源文件中加载程序的初始界面
22 | Parent root = FXMLLoader.load(getClass().getResource("friend_list.fxml"));
23 | Scene scene = new Scene(root, 300, 300); // 创建一个场景
24 | mStage.setScene(scene); // 设置舞台的场景
25 | mStage.setResizable(false); // 设置舞台的尺寸是否允许变化
26 | mStage.show(); // 显示舞台
27 | } catch (Exception e) {
28 | e.printStackTrace();
29 | }
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/chapter17/src/com/database/autocode/auto_code.fxml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/chapter02/src/com/arithmetic/math/Rand.java:
--------------------------------------------------------------------------------
1 | package com.arithmetic.math;
2 |
3 | import java.util.Random;
4 |
5 | //演示如何生成随机数
6 | public class Rand {
7 |
8 | public static void main(String[] args) {
9 | double decimal = Math.random(); // 生成小于一的随机小数(包括0和正小数)
10 | System.out.println("decimal=" + decimal);
11 | int integer = new Random().nextInt(); // 生成随机整数(包括负数)
12 | System.out.println("integer=" + integer);
13 | long long_integer = new Random().nextLong(); // 生成随机长整数(包括负数)
14 | System.out.println("long_integer=" + long_integer);
15 | float float_decimal = new Random().nextFloat(); // 生成随机的浮点小数(不包括负数)
16 | System.out.println("float_decimal=" + float_decimal);
17 | double double_decimal = new Random().nextDouble(); // 生成随机的双精度小数(不包括负数)
18 | System.out.println("double_decimal=" + double_decimal);
19 | int hundred = new Random().nextInt(100); // 生成一百以内的随机整数(0≤随机整数<100,即包括0但不包括100)
20 | System.out.println("hundred=" + hundred);
21 | }
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/chapter02/src/com/arithmetic/numerical/Suffix.java:
--------------------------------------------------------------------------------
1 | package com.arithmetic.numerical;
2 |
3 | //演示数字的后缀,主要是特殊数字的表达
4 | public class Suffix {
5 |
6 | public static void main(String[] args) {
7 | // long worldPopulation = 7444443881; // 这样写会报错,因为整数默认是int类型
8 | // 截至2018年元旦,世界人口大约有74亿
9 | long worldPopulation = 7444443881L; // 长整型数要在数值末尾加上l或者L
10 | System.out.println("worldPopulation=" + worldPopulation);
11 | // float huilv = 3.14; // 这样写会报错,因为小数默认是double类型
12 | // 3.14是中国古代数学家刘徽求得的圆周率数值,又称徽率
13 | float huilv = 3.14F; // 浮点数要在数值末尾加上f或者F
14 | System.out.println("huilv=" + huilv);
15 | // double yuanzhoulv = 3.1415926D; // 小数默认是double类型,此时末尾的D可加可不加
16 | // 中国的领土面积是960万平方千米
17 | int chinaArea = 960_0000; // 从Java7开始,数字中间允许添加下划线,从而可以更方便地区分位数
18 | System.out.println("chinaArea=" + chinaArea);
19 | // 太阳距离地球1.5亿千米
20 | double sunDistance = 1.5E8; // E8表示乘以10的8次方,E是exponent(指数)的首字母
21 | System.out.println("sunDistance=" + sunDistance);
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/chapter11/src/com/io/huffman/HuffmNode.java:
--------------------------------------------------------------------------------
1 | package com.io.huffman;
2 |
3 | //定义哈夫曼树的节点类
4 | public class HuffmNode {
5 | private int data; // 节点值
6 | private int index; // 字符的ASCII码值
7 | private HuffmNode left; // 左边的子节点
8 | private HuffmNode right; // 右边的子节点
9 |
10 | public HuffmNode(int data, int index) {
11 | this.data = data;
12 | this.index = index;
13 | }
14 |
15 | public int getData() {
16 | return data;
17 | }
18 |
19 | public void setData(int data) {
20 | this.data = data;
21 | }
22 |
23 | public int getIndex() {
24 | return index;
25 | }
26 |
27 | public void setIndex(int index) {
28 | this.index = index;
29 | }
30 |
31 | public HuffmNode getLeft() {
32 | return left;
33 | }
34 |
35 | public void setLeft(HuffmNode left) {
36 | this.left = left;
37 | }
38 |
39 | public HuffmNode getRight() {
40 | return right;
41 | }
42 |
43 | public void setRight(HuffmNode right) {
44 | this.right = right;
45 | }
46 |
47 | }
48 |
--------------------------------------------------------------------------------
/chapter16/src/com/network/im_client/ChatMain.java:
--------------------------------------------------------------------------------
1 | package com.network.im_client;
2 |
3 | import javafx.fxml.FXMLLoader;
4 | import javafx.scene.Parent;
5 | import javafx.scene.Scene;
6 | import javafx.stage.Stage;
7 |
8 | //聊天界面的程序入口
9 | public class ChatMain {
10 |
11 | // 显示窗口
12 | public void show() {
13 | Stage stage = new Stage(); // 创建一个舞台
14 | QQPartner partner = QQPartner.getInstance(""); // 获得QQ伴侣
15 | try {
16 | String title = String.format("与%s聊天", partner.getFriend().nick_name);
17 | stage.setTitle(title); // 设置舞台的标题
18 | stage.getIcons().add(QQPartner.QQ_LOGO); // 设置标题栏图标
19 | // 从FXML资源文件中加载程序的初始界面
20 | Parent root = FXMLLoader.load(getClass().getResource("chat.fxml"));
21 | Scene scene = new Scene(root, 500, 470); // 创建一个场景
22 | stage.setScene(scene); // 设置舞台的场景
23 | stage.setResizable(false); // 设置舞台的尺寸是否允许变化
24 | stage.show(); // 显示舞台
25 | } catch (Exception e) {
26 | e.printStackTrace();
27 | }
28 | }
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/chapter07/src/com/object/inherit/Duck.java:
--------------------------------------------------------------------------------
1 | package com.object.inherit;
2 |
3 | //定义了一个继承自鸟类的鸭子类
4 | public class Duck extends Bird {
5 | private String sexName; // 定义一个家禽类的性别名称
6 |
7 | public Duck(String name, int sex) {
8 | super(name, sex, "嘎嘎"); // 利用super指代父类的构造方法名称
9 | }
10 |
11 | public void setSexType(int sexType) {
12 | // 方法内部再调用自身方法,会变成递归调用,如果没有退出机制就变成死循环了
13 | // setSexType(sexType);
14 | // 在方法前面添加前缀“super.”,表示这里调用的是父类的方法
15 | super.setSexType(sexType);
16 | // 修改家禽类的性别名称,此时父类和子类都有属性sexName,不加前缀的话默认为子类的属性
17 | sexName = (sexType == 0) ? "公" : "母";
18 | // this.sexName = (sexType==0) ? "公" : "母";
19 | }
20 |
21 | // 父类的getSexName方法需要重写,否则父类的方法会使用父类的属性
22 | public String getSexName() {
23 | return this.sexName;
24 | }
25 |
26 | // 父类的toString方法需要重写,否则父类的方法会使用父类的属性
27 | public String toString() {
28 | String desc = String.format("这是一只%s%s,它会%3$s、%3$s地叫。", this.sexName,
29 | getName(), getVoice());
30 | return desc;
31 | }
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/chapter08/src/com/special/behavior/Bird.java:
--------------------------------------------------------------------------------
1 | package com.special.behavior;
2 |
3 | //定义一个名叫Bird的鸟类(作为可被其它类继承的基类)
4 | public class Bird {
5 | // 定义鸟的名称
6 | private String name;
7 | // 定义鸟的性别类型。0表示雄性,1表示雌性
8 | private int sexType;
9 | // 定义鸟的性别名称
10 | private String sexName; // 与Duck搭配使用
11 |
12 | // 鸟类的构造方法
13 | public Bird(String name, int sexType) {
14 | this.name = name;
15 | setSexType(sexType); // 该方法内部同时修改性别类型和性别名称
16 | }
17 |
18 | // 设置鸟的名称
19 | public void setName(String name) {
20 | this.name = name;
21 | }
22 |
23 | // 获取鸟的名称
24 | public String getName() {
25 | return this.name;
26 | }
27 |
28 | // 设置鸟的性别类型,并自动调整性别名称
29 | public void setSexType(int sexType) {
30 | this.sexType = sexType;
31 | this.sexName = (sexType == 0) ? "雄" : "雌";
32 | }
33 |
34 | // 获取鸟的性别类型
35 | public int getSexType() {
36 | return this.sexType;
37 | }
38 |
39 | // 获取鸟的性别名称
40 | public String getSexName() {
41 | return this.sexName;
42 | }
43 |
44 | }
45 |
--------------------------------------------------------------------------------
/chapter13/src/com/swing/login/MainWindow.java:
--------------------------------------------------------------------------------
1 | package com.swing.login;
2 |
3 | import java.awt.Dimension;
4 | import java.awt.Font;
5 |
6 | import javax.swing.JFrame;
7 | import javax.swing.JLabel;
8 | import javax.swing.JPanel;
9 |
10 | //登录进去的程序主界面
11 | public class MainWindow {
12 |
13 | // 显示程序主界面
14 | public static void show() {
15 | JFrame frame = new JFrame("主界面"); // 创建一个窗口对象
16 | frame.setSize(400, 200); // 必须设置宽高,否则没有窗体
17 | frame.setLocationRelativeTo(null);// 将窗口居中。若无该方法,窗口将位于屏幕左上角
18 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置默认的关闭操作:退出程序
19 |
20 | JPanel panel = new JPanel(); // 创建一个面板
21 |
22 | JLabel label = new JLabel("恭喜您登录成功!"); // 创建一个标签
23 | label.setPreferredSize(new Dimension(350, 120)); // 设置标签的推荐宽高
24 | label.setFont(new Font("楷体", Font.PLAIN, 40)); // 设置标签文字的字体与大小
25 | panel.add(label); // 在面板上添加标签
26 |
27 | frame.add(panel); // 在窗口上添加面板
28 |
29 | frame.setVisible(true); // 必须设置为true,否则看不见
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/chapter03/src/com/control/array/TwoDimensional3.java:
--------------------------------------------------------------------------------
1 | package com.control.array;
2 |
3 | // 演示如何通过二维数组计算三角形的三条边长
4 | public class TwoDimensional3 {
5 |
6 | public static void main(String[] args) {
7 | // 以下是分配二维数组空间的第三种形式:
8 | // 赋值等号右边直接跟着花括号,花括号又内嵌好几个花括号分别表示对应的一维数组
9 | double[][] triangle = { { -2.0, 0.0 }, { 0.0, -1.0 }, { 2.0, 1.0 } };
10 | // 下面通过循环语句依次计算三角形每条边的长度
11 | // 假设第一个数组元素代表点A,第二个数组元素代表点B,第三个数组元素代表点C,
12 | // 则本循环将依次求得AB、AC、BC这三条边的长度
13 | for (int i = 0; i < triangle.length - 1; i++) {
14 | for (int j = i + 1; j < triangle.length; j++) {
15 | // 获取两个顶点在横轴方向的距离
16 | double xDistance = Math.abs(triangle[j][0] - triangle[i][0]);
17 | // 获取两个顶点在纵轴方向的距离
18 | double yDistance = Math.abs(triangle[j][1] - triangle[i][1]);
19 | // 根据勾股定理计算连接两顶点的斜边长度
20 | double distance = Math.sqrt(xDistance * xDistance + yDistance * yDistance);
21 | System.out.println("i=" + i + ", j=" + j + ", distance=" + distance);
22 | }
23 | }
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/chapter09/src/com/collect/algorithm/FifoList.java:
--------------------------------------------------------------------------------
1 | package com.collect.algorithm;
2 |
3 | import java.util.LinkedList;
4 |
5 | //把LinkedList改进成为FIFO(先进先出)的数据结构。使用了泛型类
6 | public class FifoList extends LinkedList {
7 | private static final long serialVersionUID = -1L;
8 |
9 | private int maxSize = 6; // 最大容量
10 |
11 | public FifoList(int maxSize) {
12 | super();
13 | if (maxSize > 0) { // 最大容量必须为自然数
14 | this.maxSize = maxSize;
15 | }
16 | }
17 |
18 | // 给新的小伙伴排队
19 | public boolean add(T new_item) {
20 | addLast(new_item); // 把新的小伙伴加到队列末尾
21 | return true;
22 | }
23 |
24 | // 给新的小伙伴排队,加到队列末尾
25 | public void addLast(T new_item) {
26 | for (T item : this) { // 已在队列中的小伙伴无需处理
27 | if (item.equals(new_item)) { // 队列中已存在该小伙伴
28 | return; // 无需处理已存在的小伙伴,直接返回
29 | }
30 | }
31 | if (this.size() >= this.maxSize) { // 超过了最大容量
32 | this.removeFirst(); // 移除双端队列开头的小伙伴
33 | }
34 | super.addLast(new_item); // 往双端队列末尾插入新的小伙伴
35 | }
36 |
37 | }
38 |
--------------------------------------------------------------------------------
/chapter03/src/com/control/YanghuiSanjiao.java:
--------------------------------------------------------------------------------
1 | package com.control;
2 |
3 | //演示如何通过递推计算杨辉三角
4 | public class YanghuiSanjiao {
5 |
6 | public static void main(String[] args) {
7 | int MAX_ROW = 10; // 杨辉三角的总行数
8 | // 利用二维数组保存杨辉三角的各项系数
9 | int[][] triangles = new int[MAX_ROW][];
10 | for (int i = 0; i < MAX_ROW; i++) {
11 | triangles[i] = new int[i + 1]; // 分配每行的系数数组
12 | }
13 |
14 | for (int i = 0; i < triangles.length; i++) { // 计算每行每列的各项系数
15 | for (int j = 0; j < triangles[i].length; j++) {
16 | if (j==0 || j==triangles[i].length-1) { // 每行的头尾系数都是1
17 | triangles[i][j] = 1;
18 | } else { // 非头尾的系数值为上一行前一列与上一行当前列的系数之和
19 | triangles[i][j] = triangles[i-1][j-1] + triangles[i-1][j];
20 | }
21 | }
22 | }
23 |
24 | for (int[] row : triangles) { // 遍历并打印杨辉三角的所有系数
25 | for (int number : row) {
26 | System.out.printf("%5d", number); // 该数字占据五个数字位,靠右对齐且左边留空
27 | }
28 | System.out.println(); // 每行末尾打印换行符,下个循环另起一行
29 | }
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/chapter09/src/com/collect/container/MobilePhoneTree.java:
--------------------------------------------------------------------------------
1 | package com.collect.container;
2 |
3 | //定义用于二叉集合TreeSet的手机类。二叉树需要实现接口Comparable
4 | public class MobilePhoneTree implements Comparable {
5 |
6 | private String brand; // 手机品牌
7 | private Integer price; // 手机价格
8 |
9 | public MobilePhoneTree(String brand, int price) {
10 | this.brand = brand;
11 | this.price = price;
12 | }
13 |
14 | // 获取手机品牌
15 | public String getBrand() {
16 | return this.brand;
17 | }
18 |
19 | // 获取手机价格
20 | public int getPrice() {
21 | return this.price;
22 | }
23 |
24 | // 二叉树除了检查是否相等,还要判断先后顺序。
25 | // 相等和先后顺序的校验结果从compareTo方法获得。
26 | @Override
27 | public int compareTo(MobilePhoneTree other) {
28 | if (this.price.compareTo(other.price) > 0) { // 当前价格较高
29 | return 1;
30 | } else if (this.price.compareTo(other.price) < 0) { // 当前价格较低
31 | return -1;
32 | } else { // 二者价格相等,再比较它们的品牌
33 | return this.brand.compareTo(other.brand);
34 | }
35 | }
36 |
37 | }
38 |
--------------------------------------------------------------------------------
/NetServer/WebRoot/upload.jsp:
--------------------------------------------------------------------------------
1 | <%@ page language="java" contentType="text/html; charset=UTF-8"
2 | pageEncoding="UTF-8"%>
3 |
4 |
5 |
6 |
7 | upload
8 |
9 |
10 |
39 |
40 |
--------------------------------------------------------------------------------
/chapter03/src/com/control/logic/Relation.java:
--------------------------------------------------------------------------------
1 | package com.control.logic;
2 |
3 | //演示关系运算符的用法
4 | public class Relation {
5 |
6 | public static void main(String[] args) {
7 | int seven = 7;
8 | int eight = 8;
9 | // 数学的等号“=”对应Java的“==”符号
10 | boolean equal = seven == eight; // 结果为真表示等式成立,为假表示等式不成立
11 | System.out.println("equal=" + equal);
12 | // 数学的不等号“≠”对应Java的“!=”符号
13 | boolean not_equal = seven != eight; // 结果为真表示不等式成立,为假表示不等式不成立
14 | System.out.println("not_equal=" + not_equal);
15 | boolean greater = seven > eight; // 数学的大于号“>”对应Java的“>”符号
16 | System.out.println("greater=" + greater);
17 | boolean less = seven < eight; // 数学的小于号“<”对应Java的“<”符号
18 | System.out.println("less=" + less);
19 | boolean greater_and_equal = seven >= eight; // 数学的大等号“≥”对应Java的“>=”符号
20 | System.out.println("greater_and_equal=" + greater_and_equal);
21 | boolean less_and_equal = seven <= eight; // 数学的小等号“≤”对应Java的“<=”符号
22 | System.out.println("less_and_equal=" + less_and_equal);
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/chapter07/src/com/object/account/DepositAccount.java:
--------------------------------------------------------------------------------
1 | package com.object.account;
2 |
3 | // 定一个定期存款账户类
4 | public class DepositAccount extends Account {
5 | private int depositTerm; // 存款期限,单位月
6 |
7 | // 存款账户的构造方法
8 | public DepositAccount(int depositTerm, String depositName) {
9 | super(depositName);
10 | this.depositTerm = depositTerm;
11 | setUnit("元"); // 设置余额的单位
12 | }
13 |
14 | // 获取存款的期限
15 | public int getDepositTerm() {
16 | return this.depositTerm;
17 | }
18 |
19 | // 设置存款的期限
20 | public void setDepositTerm(int depositTerm) {
21 | this.depositTerm = depositTerm;
22 | }
23 |
24 | // 这里的定期存款只能一次性全部取出,不能分批取出,适合整存整取、零存整取。
25 | public boolean takeOut(long amount) {
26 | if (amount == getBalance()) { // 全部取出
27 | return super.takeOut(amount);
28 | } else {
29 | return false;
30 | }
31 | }
32 |
33 | // 输出存款现金账户信息
34 | public String toString() {
35 | String desc = String.format("存款期限为%d个月,%s", this.depositTerm, super.toString());
36 | return desc;
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/chapter13/src/com/swing/widget/TestTextField.java:
--------------------------------------------------------------------------------
1 | package com.swing.widget;
2 |
3 | import java.awt.Font;
4 |
5 | import javax.swing.JFrame;
6 | import javax.swing.JPanel;
7 | import javax.swing.JTextField;
8 |
9 | //演示单行输入框的用法
10 | public class TestTextField {
11 |
12 | public static void main(String[] args) {
13 | JFrame frame = new JFrame("测试单行输入框的窗口"); // 创建一个窗口对象
14 | frame.setSize(400, 90); // 必须设置宽高,否则没有窗体
15 | frame.setLocationRelativeTo(null);// 将窗口居中。若无该方法,窗口将位于屏幕左上角
16 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置默认的关闭操作:退出程序
17 |
18 | JPanel panel = new JPanel(); // 创建一个面板
19 |
20 | JTextField textField = new JTextField(); // 创建一个单行输入框
21 | textField.setFont(new Font("中号", Font.PLAIN, 16)); // 设置输入框的文本字体及其大小
22 | textField.setEditable(true); // 设置输入框允许编辑
23 | textField.setColumns(11); // 设置输入框的长度为11个字符
24 |
25 | panel.add(textField); // 在面板上添加单行输入框
26 | frame.add(panel); // 在窗口上添加面板
27 |
28 | frame.setVisible(true); // 必须设置为true,否则看不见
29 | }
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/chapter08/src/com/special/builder/User.java:
--------------------------------------------------------------------------------
1 | package com.special.builder;
2 |
3 | //定义一个采用建造者模式的用户类
4 | public class User {
5 | private final String name; // 姓名
6 | private final String password; // 密码
7 |
8 | // 获取用户的姓名
9 | public String getName() {
10 | return this.name;
11 | }
12 |
13 | // 获取用户的密码
14 | public String getPassword() {
15 | return this.password;
16 | }
17 |
18 | // 用户类的构造方法。通过该类的建造者来传递具体参数。注意它是私有方法,外部不能直接调用。
19 | private User(Builder builder) {
20 | this.name = builder.name;
21 | this.password = builder.password;
22 | }
23 |
24 | // 定义用户类的建造者
25 | public static class Builder {
26 | private String name; // 姓名
27 | private String password; // 密码
28 |
29 | // 设置用户的姓名
30 | public void setName(String name) {
31 | this.name = name;
32 | }
33 |
34 | // 设置用户的密码
35 | public void setPassword(String password) {
36 | this.password = password;
37 | }
38 |
39 | // 建造方法,返回用户类的实例
40 | public User build() {
41 | return new User(this);
42 | }
43 | }
44 |
45 | }
46 |
--------------------------------------------------------------------------------
/chapter09/src/com/collect/algorithm/SortQuick.java:
--------------------------------------------------------------------------------
1 | package com.collect.algorithm;
2 |
3 | //快速排序的算法实现
4 | public class SortQuick implements SortInterface {
5 |
6 | @Override
7 | public void sort(Comparable[] array) {
8 | sort(array, 0, array.length - 1);
9 | }
10 |
11 | private void sort(Comparable[] array, int start, int end) {
12 | if (start >= end) {
13 | return;
14 | }
15 | int index = partition(array, start, end);
16 | sort(array, start, index - 1);
17 | sort(array, index + 1, end);
18 | }
19 |
20 | private int partition(Comparable[] array, int start, int end) {
21 | // 固定的切分方式
22 | Comparable key = array[start];
23 | while (start < end) {
24 | while (array[end].compareTo(key) > 0 && end > start) { // 从后半部分向前扫描
25 | end--;
26 | }
27 | array[start] = array[end];
28 | while (array[start].compareTo(key) < 0 && end > start) { // 从前半部分向后扫描
29 | start++;
30 | }
31 | array[end] = array[start];
32 | }
33 | array[end] = key;
34 | return end;
35 | }
36 |
37 | }
38 |
--------------------------------------------------------------------------------
/chapter08/src/com/special/behavior/ExpandBehavior.java:
--------------------------------------------------------------------------------
1 | package com.special.behavior;
2 |
3 | //定义一个增加了Java8新特性的接口
4 | public interface ExpandBehavior {
5 | public void fly(); // 声明了一个抽象的飞翔方法
6 | public void swim(); // 声明了一个抽象的游泳方法
7 | public void run(); // 声明了一个抽象的奔跑方法
8 |
9 | // 默认方法,以前缀default标识。默认方法不支持重写,但可以被继承。
10 | public default String getOrigin(String place, String name, String ancestor) {
11 | return String.format("%s%s的祖先是%s。", place, name, ancestor);
12 | }
13 |
14 | // 接口内部的静态属性也默认为终态属性,所以final前缀可加可不加
15 | public static int MALE = 0; // 雄性
16 | public static int FEMALE = 1; // 雌性
17 | // public final static int MALE = 0;
18 | // public final static int FEMALE = 1;
19 |
20 | // 静态方法,以关键字static标识。静态方法支持重写,但不能被继承。
21 | public static String getNameByLeg(int leg_count) {
22 | if (leg_count == 2) {
23 | return "二足动物";
24 | } else if (leg_count == 4) {
25 | return "四足动物";
26 | } else if (leg_count >= 6) {
27 | return "多足动物";
28 | } else {
29 | return "奇异动物";
30 | }
31 | }
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/chapter17/src/com/database/autocode/Column.java:
--------------------------------------------------------------------------------
1 | package com.database.autocode;
2 |
3 | import javafx.beans.property.SimpleStringProperty;
4 |
5 | //定义表格列的信息类
6 | public class Column {
7 | private SimpleStringProperty name; // 名称
8 | private SimpleStringProperty type; // 类型
9 | private SimpleStringProperty comment; // 说明
10 |
11 | public Column(String name, String type, String comment) {
12 | this.name = new SimpleStringProperty(name);
13 | this.type = new SimpleStringProperty(type);
14 | this.comment = new SimpleStringProperty(comment);
15 | }
16 |
17 | public String getName() {
18 | return name.get();
19 | }
20 |
21 | public void setName(String name) {
22 | this.name.set(name);
23 | }
24 |
25 | public String getType() {
26 | return type.get();
27 | }
28 |
29 | public void setType(String type) {
30 | this.type.set(type);
31 | }
32 |
33 | public String getComment() {
34 | return comment.get();
35 | }
36 |
37 | public void setComment(String comment) {
38 | this.comment.set(comment);
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/chapter02/src/com/arithmetic/Pingfanggen.java:
--------------------------------------------------------------------------------
1 | package com.arithmetic;
2 |
3 | //演示利用牛顿迭代法计算平方根
4 | public class Pingfanggen {
5 |
6 | public static void main(String[] arg) {
7 | // double number = 3; // 需要求n次方根的数字
8 | // int n = 2; // n次方根的n
9 | // double Xm = number; // 每次迭代后的数值
10 | // double slope; // 曲线的导数,切线的斜率
11 |
12 | // slope = n * Math.pow(Xm, n-1); // 曲线的导数,切线的斜率
13 | // Xm = Xm - (Math.pow(Xm, n)-number)/slope; // 求n次方根的牛顿迭代式子
14 |
15 | // slope = 2 * Xm; // 曲线的导数,切线的斜率
16 | // Xm = Xm - (Xm*Xm-number)/(2 * Xm); // 求2次方根的牛顿迭代式子
17 | // Xm = (Xm*Xm +number)/(2 * Xm);
18 | // Xm = (Xm + number/Xm) / 2; // 简化之后求平方根的迭代式子
19 |
20 | double number = 3; // 需要求平方根的数字
21 | double Xm = number; // 每次迭代后的数值
22 |
23 | Xm = (Xm + number/Xm) / 2; // 第一次迭代后的平方根
24 | System.out.println(number+"的平方根="+Xm);
25 |
26 | Xm = (Xm + number/Xm) / 2; // 第二次迭代后的平方根
27 | System.out.println(number+"的平方根="+Xm);
28 |
29 | Xm = (Xm + number/Xm) / 2; // 第三次迭代后的平方根
30 | System.out.println(number+"的平方根="+Xm);
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/chapter17/src/com/database/autocode/Teacher.java:
--------------------------------------------------------------------------------
1 | package com.database.autocode;
2 |
3 | import java.util.Date;
4 |
5 | //教师信息表
6 | public class Teacher {
7 | private int gonghao; // 工号
8 | private String name; // 姓名
9 | private Date birthday; // 生日
10 | private int sex; // 性别。0 男性;1 女性
11 | private String course; // 任教课程
12 |
13 | public void setGonghao(int gonghao) {
14 | this.gonghao = gonghao;
15 | }
16 | public int getGonghao() {
17 | return this.gonghao;
18 | }
19 | public void setName(String name) {
20 | this.name = name;
21 | }
22 | public String getName() {
23 | return this.name;
24 | }
25 | public void setBirthday(Date birthday) {
26 | this.birthday = birthday;
27 | }
28 | public Date getBirthday() {
29 | return this.birthday;
30 | }
31 | public void setSex(int sex) {
32 | this.sex = sex;
33 | }
34 | public int getSex() {
35 | return this.sex;
36 | }
37 | public void setCourse(String course) {
38 | this.course = course;
39 | }
40 | public String getCourse() {
41 | return this.course;
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/chapter03/src/com/control/process/ForLoop.java:
--------------------------------------------------------------------------------
1 | package com.control.process;
2 |
3 | import java.util.Scanner;
4 |
5 | // 演示for语句的用法
6 | public class ForLoop {
7 |
8 | public static void main(String[] args) {
9 | System.out.println("长夜漫漫,无心睡眠,请给他设定一个睡醒的年限");
10 | Scanner scan = new Scanner(System.in); // 从控制台接收输入文本
11 | int limit = scan.nextInt(); // nextInt方法表示接收一个整数,以回车键结尾
12 | int year;
13 | // for (式子A; 式子B; 式子C;)的三个式子A、B、C说明如下:
14 | // 式子A在首次进入循环时执行;
15 | // 式子B是循环的判断条件,B成立时继续循环,不成立时退出循环;
16 | // 式子C在开始下一次循环之前执行。注意,每次循环结束之后,先执行式子C,再判断式子B
17 | for (year = 0; year < limit; year++) {
18 | System.out.println("已经过去的年份:" + year);
19 | }
20 | // 上面的for循环等价于下面的while循环
21 | // year = 0;
22 | // if (year < limit) {
23 | // while (true) { // 开始循环处理
24 | // System.out.println("已经过去的年份:" + year);
25 | // year++;
26 | // if (year < limit) { // 年份未达到条件,继续循环
27 | // continue;
28 | // } else { // 年份已达到条件,退出循环
29 | // break;
30 | // }
31 | // }
32 | // }
33 | System.out.println("他足足睡了这么多年:" + year);
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/chapter14/src/com/javafx/widget/Snack.java:
--------------------------------------------------------------------------------
1 | package com.javafx.widget;
2 |
3 | import javafx.beans.property.SimpleStringProperty;
4 |
5 | //定义快餐类
6 | public class Snack {
7 | private SimpleStringProperty xuhao; // 序号
8 | private SimpleStringProperty name; // 快餐名称
9 | private SimpleStringProperty price; // 快餐价格
10 |
11 | public Snack(String xuhao, String name, String price) {
12 | this.xuhao = new SimpleStringProperty(xuhao);
13 | this.name = new SimpleStringProperty(name);
14 | this.price = new SimpleStringProperty(price);
15 | }
16 |
17 | public String getXuhao() { // 获取序号
18 | return xuhao.get();
19 | }
20 |
21 | public void setXuhao(String xuhao) { // 设置序号
22 | this.xuhao.set(xuhao);
23 | }
24 |
25 | public String getName() { // 获取快餐名称
26 | return name.get();
27 | }
28 |
29 | public void setName(String name) { // 设置快餐名称
30 | this.name.set(name);
31 | }
32 |
33 | public String getPrice() { // 获取快餐价格
34 | return price.get();
35 | }
36 |
37 | public void setPrice(String price) { // 设置快餐价格
38 | this.price.set(price);
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/chapter07/src/com/object/trade/StockAccount.java:
--------------------------------------------------------------------------------
1 | package com.object.trade;
2 |
3 | // 定义一个股票账户类
4 | public class StockAccount extends Account {
5 |
6 | // 股票代码
7 | private String stockId;
8 |
9 | // 股票账户的构造方法
10 | public StockAccount(String stockId, String stockName) {
11 | super(stockName);
12 | this.stockId = stockId;
13 | setUnit("股");
14 | }
15 |
16 | // 获取股票代码
17 | public String getStockId() {
18 | return this.stockId;
19 | }
20 |
21 | // 买入股票,买入数量必须是100的整数倍,至少买入100股
22 | public boolean saveIn(long amount) {
23 | if (amount % 100 == 0 && amount >= 100) { // 整百买入
24 | return super.saveIn(amount);
25 | } else {
26 | return false;
27 | }
28 | }
29 |
30 | // 卖出股票,卖出数量必须是100的整数倍,至少卖出100股
31 | public boolean takeOut(long amount) {
32 | if (amount % 100 == 0 && amount >= 100) { // 整百卖出
33 | return super.takeOut(amount);
34 | } else {
35 | return false;
36 | }
37 | }
38 |
39 | // 输出股票现金账户信息
40 | public String toString() {
41 | String desc = String.format("股票代码为%s,%s", this.stockId,
42 | super.toString());
43 | return desc;
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/chapter09/src/com/collect/container/MobilePhoneHash.java:
--------------------------------------------------------------------------------
1 | package com.collect.container;
2 |
3 | //定义用于哈希集合HashSet的手机类
4 | public class MobilePhoneHash {
5 |
6 | private String brand; // 手机品牌
7 | private Integer price; // 手机价格
8 |
9 | public MobilePhoneHash(String brand, int price) {
10 | this.brand = brand;
11 | this.price = price;
12 | }
13 |
14 | // 获取手机品牌
15 | public String getBrand() {
16 | return this.brand;
17 | }
18 |
19 | // 获取手机价格
20 | public int getPrice() {
21 | return this.price;
22 | }
23 |
24 | // hashCode方法计算出来的哈希值对应于该对象的保存位置
25 | @Override
26 | public int hashCode() {
27 | return brand.hashCode() + price.hashCode();
28 | }
29 |
30 | // 同一个存储位置上可能有多个对象(哈希值恰好相等),
31 | // 此时系统自动调用equals方法判断是否存在相同的对象。
32 | @Override
33 | public boolean equals(Object obj) {
34 | if (!(obj instanceof MobilePhoneHash)) {
35 | return false;
36 | }
37 | MobilePhoneHash other = (MobilePhoneHash) obj;
38 | // 手机品牌和手机价格都相等,才算是这两个手机相等
39 | boolean equals = this.brand.equals(other.brand)
40 | && this.price.equals(other.price);
41 | return equals;
42 | }
43 |
44 | }
45 |
--------------------------------------------------------------------------------
/chapter07/src/com/object/trade/TransactionRecord.java:
--------------------------------------------------------------------------------
1 | package com.object.trade;
2 |
3 | import java.time.LocalDateTime;
4 | import java.time.format.DateTimeFormatter;
5 |
6 | // 定义交易记录类
7 | public class TransactionRecord {
8 |
9 | public final static int IN = 0; // 存入、买入
10 | public final static int OUT = 1; // 取出、卖出
11 |
12 | private int dealType; // 交易类型
13 | private String dealName; // 交易名称
14 | private long dealAmonut; // 交易数额
15 | private LocalDateTime dealTime; // 交易时间
16 |
17 | // 交易记录类的构造方法
18 | public TransactionRecord(int dealType, String dealName, long dealAmonut,
19 | LocalDateTime dealTime) {
20 | this.dealType = dealType;
21 | this.dealName = dealName;
22 | this.dealAmonut = dealAmonut;
23 | this.dealTime = dealTime;
24 | }
25 |
26 | // 输出交易详情
27 | public String toString() {
28 | DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
29 | String desc = String.format("%s 于 %s %s %d", this.dealName, this.dealTime.format(formatter),
30 | (this.dealType==IN) ? "存入/买入" : "取出/卖出", this.dealAmonut);
31 | return desc;
32 | }
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/chapter13/src/com/swing/widget/TestPasswordField.java:
--------------------------------------------------------------------------------
1 | package com.swing.widget;
2 |
3 | import java.awt.Font;
4 |
5 | import javax.swing.JFrame;
6 | import javax.swing.JPanel;
7 | import javax.swing.JPasswordField;
8 |
9 | //演示密码输入框的用法
10 | public class TestPasswordField {
11 |
12 | public static void main(String[] args) {
13 | JFrame frame = new JFrame("测试密码输入框的窗口"); // 创建一个窗口对象
14 | frame.setSize(400, 90); // 必须设置宽高,否则没有窗体
15 | frame.setLocationRelativeTo(null);// 将窗口居中。若无该方法,窗口将位于屏幕左上角
16 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置默认的关闭操作:退出程序
17 |
18 | JPanel panel = new JPanel(); // 创建一个面板
19 |
20 | JPasswordField passwordField = new JPasswordField(); // 创建一个密码框
21 | passwordField.setFont(new Font("中号", Font.PLAIN, 16)); // 设置密码框的文本字体及其大小
22 | passwordField.setEditable(true); // 设置密码框允许编辑
23 | passwordField.setColumns(6); // 设置密码框的长度为11个字符
24 | //passwordField.setEchoChar('*'); // 设置密码框的回显字符。默认的回显字符为圆点
25 |
26 | panel.add(passwordField); // 在面板上添加密码框
27 | frame.add(panel); // 在窗口上添加面板
28 |
29 | frame.setVisible(true); // 必须设置为true,否则看不见
30 | }
31 |
32 | }
33 |
--------------------------------------------------------------------------------
/chapter07/src/com/object/account/CashAccount.java:
--------------------------------------------------------------------------------
1 | package com.object.account;
2 |
3 | // 定义一个现金账户类
4 | public class CashAccount extends Account {
5 | public final static int RMB = 0; // 人民币
6 | public final static int SGD = 1; // 新加坡元
7 | public final static int USD = 2; // 美元
8 | public final static int EUR = 3; // 欧元
9 | public final static int GBP = 4; // 英镑
10 | public final static int JPY = 5; // 日元
11 | public final static String[] typeNames = new String[]{"人民币","新加坡元","美元","欧元","英镑","日元"};
12 | private int cashType; // 现金类型
13 |
14 | // 现金账户的构造方法
15 | public CashAccount(int cashType, String cashName) {
16 | super(cashName);
17 | this.cashType = cashType;
18 | setUnit(cashType==GBP ? "镑" : "元"); // 设置余额的单位
19 | }
20 |
21 | // 获取现金的类型
22 | public int getCashType() {
23 | return this.cashType;
24 | }
25 |
26 | // 设置现金的类型
27 | public void setCashType(int cashType) {
28 | this.cashType = cashType;
29 | }
30 |
31 | // 输出现金账户信息
32 | public String toString() {
33 | String desc = String.format("现金类型为%s,%s", typeNames[this.cashType], super.toString());
34 | return desc;
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/chapter07/src/com/object/trade/CashAccount.java:
--------------------------------------------------------------------------------
1 | package com.object.trade;
2 |
3 | // 定义一个现金账户类
4 | public class CashAccount extends Account {
5 | public final static int RMB = 0; // 人民币
6 | public final static int HKD = 1; // 港币
7 | public final static int USD = 2; // 美元
8 | public final static int EUR = 3; // 欧元
9 | public final static int GBP = 4; // 英镑
10 | public final static int JPY = 5; // 日元
11 | public final static String[] typeNames = new String[]{"人民币","港币","美元","欧元","英镑","日元"};
12 |
13 | private int cashType; // 现金类型
14 |
15 | // 现金账户的构造方法
16 | public CashAccount(int cashType, String cashName) {
17 | super(cashName);
18 | this.cashType = cashType;
19 | setUnit(cashType==GBP ? "镑" : "元"); // 设置余额的单位
20 | }
21 |
22 | // 获取现金的类型
23 | public int getCashType() {
24 | return this.cashType;
25 | }
26 |
27 | // 设置现金的类型
28 | public void setCashType(int cashType) {
29 | this.cashType = cashType;
30 | }
31 |
32 | // 输出现金账户信息
33 | public String toString() {
34 | String desc = String.format("现金类型为%s,%s", typeNames[this.cashType],
35 | super.toString());
36 | return desc;
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/chapter09/src/com/collect/algorithm/ArrayFind.java:
--------------------------------------------------------------------------------
1 | package com.collect.algorithm;
2 |
3 | //二分查找算法的工具类。使用了泛型方法
4 | public class ArrayFind {
5 | private static int count; // 查找次数
6 |
7 | // 二分查找的入口方法。注意泛型类型T必须实现了接口Comparable
8 | // 请求参数为待查找的数组及目标元素,返回参数为目标元素的数组下标(位置)
9 | public static > int binarySearch(T[] array, T aim) {
10 | count = 0; // 开始查找前先把查找次数清零
11 | return binarySearch(array, 0, array.length - 1, aim);
12 | }
13 |
14 | // 使用递归实现的二分查找
15 | private static > int binarySearch(T[] array, int start, int end, T aim) {
16 | count++; // 查找次数加一
17 | if (start>=end && aim.compareTo(array[start])!=0) { // 起点和终点都重合了还没找到
18 | return -1; // 返回-1表示没找到
19 | }
20 | int middle = (start + end) / 2; // 计算中间的位置
21 | if (aim.compareTo(array[middle]) == 0) { // 找到目标值,返回目标值所处的位置
22 | System.out.println("查找次数="+count);
23 | return middle;
24 | } else if (aim.compareTo(array[middle]) < 0) { // 目标值在前半段,继续查找
25 | return binarySearch(array, start, middle - 1, aim);
26 | } else { // 目标值在后半段,继续查找
27 | return binarySearch(array, middle + 1, end, aim);
28 | }
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/chapter03/src/com/control/array/ArraySort.java:
--------------------------------------------------------------------------------
1 | package com.control.array;
2 |
3 | import java.util.Arrays;
4 | import java.util.Random;
5 |
6 | // 演示如何使用Arrays工具的sort方法
7 | public class ArraySort {
8 |
9 | public static void main(String[] args) {
10 | int[] prices = { 99, 80, 18, 68, 8 };
11 | // 对整型数组prices里的元素排序,sort方法得到的结果是升序排列
12 | Arrays.sort(prices);
13 | for (int price : prices) { // 循环遍历并打印整型数组的所有元素数值
14 | System.out.println("price = " + price);
15 | }
16 |
17 | int[] numbers = new int[10]; // 创建一个大小为10的整型数组
18 | loop: for (int i = 0; i < numbers.length; i++) {
19 | int item = new Random().nextInt(100); // 生成一个小于100的随机整数
20 | // 下面的循环用来检查数组中是否已经存在该随机数
21 | for (int j = 0; j < i; j++) {
22 | if (numbers[j] == item) { // 已经存在该随机数,则继续第一层循环,重新生成随机数
23 | i--; // 本次循环做了无用功,取消当前的计数
24 | continue loop; // 继续以loop标记的外层循环
25 | }
26 | }
27 | numbers[i] = item; // 原数组不存在该随机数,则把随机数加入到数组中
28 | }
29 | // 对整型数组numbers里的元素排序,sort方法得到的结果是升序排列
30 | Arrays.sort(numbers);
31 | for (int number : numbers) { // 循环遍历并打印整型数组的所有随机数
32 | System.out.println("number = " + number);
33 | }
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/chapter10/src/com/addition/exception/TestError.java:
--------------------------------------------------------------------------------
1 | package com.addition.exception;
2 |
3 | //演示一些错误的发生场景
4 | public class TestError {
5 |
6 | public static void main(String[] args) {
7 | testUnlimitedString(); // 内存溢出错误:程序需要的内存超过了最大的堆内存配置
8 | //testUnlimitedRecursion(); // 栈溢出错误:程序占用的栈空间超过了配置的栈内存大小
9 | }
10 |
11 | // 测试内存溢出错误:程序需要的内存超过了最大的堆内存配置
12 | // java.lang.OutOfMemoryError: Java heap space
13 | private static void testUnlimitedString() {
14 | String str = "Hello world";
15 | String result = getUnlimitedString(str); // 获取无限大小的字符串
16 | System.out.println("result=" + result.toString());
17 | }
18 |
19 | // 获取无限大小的字符串
20 | private static String getUnlimitedString(String str) {
21 | System.out.println("getUnlimitedString");
22 | String append = String.format("%s+%s", str, str);
23 | return getUnlimitedString(append);
24 | }
25 |
26 | // 测试栈溢出错误:程序占用的栈空间超过了配置的栈内存大小
27 | // java.lang.StackOverflowError
28 | private static void testUnlimitedRecursion() {
29 | recursionAction(); // 用于递归动作的方法
30 | }
31 |
32 | // 用于递归动作的方法
33 | public static void recursionAction() {
34 | System.out.println("recursionAction");
35 | recursionAction();
36 | }
37 |
38 | }
39 |
--------------------------------------------------------------------------------
/chapter13/src/com/swing/widget/TestTextArea.java:
--------------------------------------------------------------------------------
1 | package com.swing.widget;
2 |
3 | import java.awt.Dimension;
4 | import java.awt.Font;
5 |
6 | import javax.swing.JFrame;
7 | import javax.swing.JPanel;
8 | import javax.swing.JTextArea;
9 |
10 | //演示多行输入框的用法
11 | public class TestTextArea {
12 |
13 | public static void main(String[] args) {
14 | JFrame frame = new JFrame("测试多行输入框的窗口"); // 创建一个窗口对象
15 | frame.setSize(400, 130); // 必须设置宽高,否则没有窗体
16 | frame.setLocationRelativeTo(null);// 将窗口居中。若无该方法,窗口将位于屏幕左上角
17 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置默认的关闭操作:退出程序
18 |
19 | JPanel panel = new JPanel(); // 创建一个面板
20 |
21 | JTextArea area = new JTextArea(); // 创建一个多行输入框
22 | area.setPreferredSize(new Dimension(180, 30)); // 设置输入框的推荐宽高
23 | area.setFont(new Font("中号", Font.PLAIN, 16)); // 设置输入框的文本字体及其大小
24 | area.setEditable(true); // 设置输入框允许编辑
25 | area.setColumns(14); // 设置输入框的长度为14个字符
26 | area.setRows(3); // 设置输入框的高度为3行字符
27 | area.setLineWrap(true); // 设置每行是否允许折叠。为true的话,输入字符超过每行宽度就会自动换行
28 |
29 | panel.add(area); // 在面板上添加多行输入框
30 | frame.add(panel); // 在窗口上添加面板
31 |
32 | frame.setVisible(true); // 必须设置为true,否则看不见
33 | }
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/chapter02/src/com/arithmetic/operator/Assign.java:
--------------------------------------------------------------------------------
1 | package com.arithmetic.operator;
2 |
3 | //演示赋值运算符的用法
4 | public class Assign {
5 |
6 | public static void main(String[] args) {
7 | // 日常生活中的加法例子是:1+1=2,运算结果在右边。但Java编程中是把运算结果放在左边的
8 | int x = 1 + 1;
9 | System.out.println("初始值 x=" + x);
10 | // 注意这里的等号是赋值操作,并非代数方程式里面的等号,否则x=x+7将会求得0=7的荒诞结果
11 | // x = x+7;
12 | // 对变量做加法运算后,假如相加之和仍然保存在原变量,那么可按如下格式使用运算符“+=”
13 | x += 7; // 该行代码等同于 x = x+7;
14 | System.out.println("相加之和 x=" + x);
15 | // 运算符“-=”的作用类似“+=”,即把相减之差保存到原变量中
16 | x -= 7; // 该行代码等同于 x = x-7;
17 | System.out.println("相减之差 x=" + x);
18 | // 若要将相乘之积保存到原变量中,则可使用运算符“*=”
19 | x *= 7; // 该行代码等同于 x = x*7;
20 | System.out.println("相乘之积 x=" + x);
21 | // 若要将相除之商保存到原变量中,则可使用运算符“/=”
22 | x /= 7; // 该行代码等同于 x = x/7;
23 | System.out.println("相除之商 x=" + x);
24 | // 若要将相除之余保存到原变量中,则可使用运算符“%=”
25 | x %= 7; // 该行代码等同于 x = x%7;
26 | System.out.println("相除之余 x=" + x);
27 | // 若要将按位左移结果保存到原变量中,则可使用运算符“<<=”
28 | x <<= 2; // 该行代码等同于 x = x << 2;
29 | System.out.println("x按位左移两位=" + x);
30 | // 若要将按位右移结果保存到原变量中,则可使用运算符“>>=”
31 | x >>= 2; // 该行代码等同于 x = x >> 2;
32 | System.out.println("x按位右移两位=" + x);
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/chapter08/src/com/special/behavior/TestInterface.java:
--------------------------------------------------------------------------------
1 | package com.special.behavior;
2 |
3 | //演示如何使用接口对应的实现类
4 | public class TestInterface {
5 |
6 | public static void main(String[] args) {
7 | testSimple(); // 演示简单接口的实现类用法
8 | testMultiple(); // 演示某个类同时实现了多个接口
9 | testExpand(); // 演示扩展接口的实现类用法
10 | }
11 |
12 | // 演示简单接口的实现类用法
13 | private static void testSimple() {
14 | Goose goose = new Goose("家鹅", 0); // 创建一个家鹅实例
15 | goose.fly(); // 实现了接口的fly方法
16 | goose.swim(); // 实现了接口的swim方法
17 | goose.run(); // 实现了接口的run方法
18 | }
19 |
20 | // 演示某个类同时实现了多个接口
21 | private static void testMultiple() {
22 | Frog frog = new Frog(); // 创建一个青蛙实例
23 | frog.swim(); // 实现了Behavior接口的swim方法
24 | frog.jump(); // 实现了Behavior2接口的run方法
25 | }
26 |
27 | // 演示扩展接口的实现类用法
28 | private static void testExpand() {
29 | // 实现类可以继承接口的静态属性
30 | ExpandGoose goose = new ExpandGoose("鹅", ExpandGoose.FEMALE);
31 | // goose.fly();
32 | // goose.swim();
33 | // goose.run();
34 | goose.show("中国", "鸿雁");
35 | goose.show("欧洲", "灰雁");
36 | // 接口中的静态方法没有被实现类所继承,因而只能通过扩展接口自身访问
37 | String typeName = ExpandBehavior.getNameByLeg(2);
38 | System.out.println("鹅是" + typeName);
39 | }
40 |
41 | }
42 |
--------------------------------------------------------------------------------
/chapter04/src/com/method/big/TestInteger.java:
--------------------------------------------------------------------------------
1 | package com.method.big;
2 |
3 | import java.math.BigInteger;
4 |
5 | //演示大整数BigInteger的用法
6 | public class TestInteger {
7 |
8 | public static void main(String[] args) {
9 | BigInteger nine = BigInteger.valueOf(9); // 生成一个指定数值的大整数变量
10 | BigInteger four = BigInteger.valueOf(4); // 生成一个指定数值的大整数变量
11 | BigInteger sum = nine.add(four); // add方法用来替代加法运算符“+”
12 | System.out.println("sum="+sum);
13 | BigInteger sub = nine.subtract(four); // subtract方法用来替代减法运算符“-”
14 | System.out.println("sub="+sub);
15 | BigInteger mul = nine.multiply(four); // multiply方法用来替代乘法运算符“*”
16 | System.out.println("mul="+mul);
17 | BigInteger div = nine.divide(four); // divide方法用来替代除法运算符“/”
18 | System.out.println("div="+div);
19 | BigInteger remainder = nine.remainder(four); // remainder方法用来替代取余数运算符“%”
20 | System.out.println("remainder="+remainder);
21 | BigInteger neg = nine.negate(); // negate方法用来替代负号运算符“-”
22 | System.out.println("neg="+neg);
23 | BigInteger abs = nine.abs(); // abs方法用来替代数学库函数Math.abs
24 | System.out.println("abs="+abs);
25 | BigInteger pow = nine.pow(2); // pow方法用来替代数学库函数Math.pow
26 | System.out.println("pow="+pow);
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/chapter12/src/com/awt/image/TestImage.java:
--------------------------------------------------------------------------------
1 | package com.awt.image;
2 |
3 | import java.awt.Frame;
4 | import java.awt.Panel;
5 | import java.awt.event.WindowAdapter;
6 | import java.awt.event.WindowEvent;
7 |
8 | //演示如何展示图片
9 | public class TestImage {
10 |
11 | public static void main(String[] args) {
12 | final Frame frame = new Frame("测试图片"); // 创建一个窗口对象
13 |
14 | frame.addWindowListener(new WindowAdapter() { // 为窗口注册监听器,实现窗口关闭功能
15 | public void windowClosing(WindowEvent e) { // 单击了窗口右上角的叉号按钮
16 | frame.dispose(); // 关闭窗口
17 | }
18 | });
19 |
20 | frame.setSize(400, 300); // 必须设置宽高,否则没有窗体
21 | frame.setLocationRelativeTo(null);// 将窗口居中。若无该方法,窗口将位于屏幕左上角
22 |
23 | Panel panel = new Panel(); // 创建一个面板
24 |
25 | ImageView imageView = new ImageView(); // 创建一个自定义的图像视图
26 | imageView.setSize(320, 240); // 设置图像视图的宽高
27 | imageView.setImagePath("E:/apple.png"); // 在图像视图上显示指定路径的图片
28 | //imageView.setImageStream(TestImage.class.getResourceAsStream("apple.png")); // 在图像视图上显示当前代码位置的图片
29 | panel.add(imageView); // 在面板上添加图像视图
30 |
31 | frame.add(panel); // 在窗口上添加面板
32 |
33 | // setVisible方法放到最后面,这样才能显示所有控件
34 | frame.setVisible(true); // 必须设置为true,否则看不见
35 | }
36 |
37 | }
38 |
--------------------------------------------------------------------------------
/chapter16/src/com/network/socket/TestUdpServer.java:
--------------------------------------------------------------------------------
1 | package com.network.socket;
2 |
3 | import java.net.DatagramPacket;
4 | import java.net.DatagramSocket;
5 |
6 | import com.network.PrintUtils;
7 |
8 | //演示Socket服务器的运行(UDP协议的不可靠连接)
9 | public class TestUdpServer {
10 | private static final int UDP_PORT = 61000; // UDP传输专用端口
11 |
12 | public static void main(String[] args) {
13 | startUdpServer(); // 启动UDP服务器接收文本消息
14 | }
15 |
16 | // 启动UDP服务器接收文本消息
17 | private static void startUdpServer() {
18 | PrintUtils.print("UDP服务器已启动");
19 | // 创建一个监听指定端口的DatagramSocket对象
20 | try (DatagramSocket socket = new DatagramSocket(UDP_PORT)) {
21 | byte[] data = new byte[1024]; // 接收数据的字节数组
22 | // 创建一个DatagramPacket对象,并指定数据包的字节数组及其大小
23 | DatagramPacket packet = new DatagramPacket(data, data.length);
24 | while (true) { // 持续侦听
25 | socket.receive(packet); // 接收到了数据包
26 | // 把收到的数据转换为字符串。字符串构造方法的三个参数依次为:
27 | // 已收到的数据、起始偏移、数据的长度。
28 | String message = new String(packet.getData(),
29 | packet.getOffset(), packet.getLength());
30 | PrintUtils.print("UDP服务器收到消息:" + message);
31 | }
32 | } catch (Exception e) {
33 | e.printStackTrace();
34 | }
35 | }
36 |
37 | }
38 |
--------------------------------------------------------------------------------
/chapter04/src/com/method/pack/PackNumber.java:
--------------------------------------------------------------------------------
1 | package com.method.pack;
2 |
3 | //以Integer为例,演示数值包装类型的赋值
4 | public class PackNumber {
5 |
6 | public static void main(String[] args) {
7 | // 数值包装类型下面以Integer举例,其它包装类型的用法同Integer,包括Byte、Short、Long、Float、Double
8 | // 初始化包装变量的第一种方式:直接用等号赋值
9 | Integer oneInteger = 1;
10 | // 初始化包装变量的第二种方式:调用包装类型的valueOf方法
11 | //Integer oneInteger = Integer.valueOf(1);
12 | // 初始化包装变量的第三种方式:使用关键字new创建新变量
13 | //Integer oneInteger = new Integer(1);
14 | System.out.println("oneInteger="+oneInteger);
15 | byte oneByte = oneInteger.byteValue(); // 把包装变量转换成字节变量
16 | System.out.println("oneByte="+oneByte);
17 | short oneShort = oneInteger.shortValue(); // 把包装变量转换成短整变量
18 | System.out.println("oneShort="+oneShort);
19 | int oneInt = oneInteger.intValue(); // 把包装变量转换成整型变量
20 | System.out.println("oneInt="+oneInt);
21 | long oneLong = oneInteger.longValue(); // 把包装变量转换成长整变量
22 | System.out.println("oneLong="+oneLong);
23 | float oneFloat = oneInteger.floatValue(); // 把包装变量转换成浮点变量
24 | System.out.println("oneFloat="+oneFloat);
25 | double oneDouble = oneInteger.doubleValue(); // 把包装变量转换成双精度变量
26 | System.out.println("oneDouble="+oneDouble);
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/chapter02/src/com/arithmetic/operator/Four.java:
--------------------------------------------------------------------------------
1 | package com.arithmetic.operator;
2 |
3 | //演示四则运算符的用法
4 | public class Four {
5 |
6 | public static void main(String[] args) {
7 | int sum = 1 + 2; // 求两数相加之和
8 | System.out.println("sum=" + sum);
9 | int differ = 7 - 3; // 求两数相减之差
10 | System.out.println("differ=" + differ);
11 | int product = 5 * 6; // 求两数相乘之积
12 | System.out.println("product=" + product);
13 | int quotient = 81 / 9; // 求两数相除之商
14 | System.out.println("quotient=" + quotient);
15 | int remainder = 40 % 3; // 求两数相除之余数
16 | System.out.println("remainder=" + remainder);
17 | // 被除数和除数都是整数,则求得的商为去掉小数部分的整数
18 | int quotientInt = 25 / 4;
19 | System.out.println("quotientInt=" + quotientInt);
20 | // 被除数和除数只要有一个是浮点或双精度数,则求得的商保留小数部分
21 | double quotientDouble = 25.0 / 4; // 25/4.0的运算结果跟25.0/4是一样的
22 | System.out.println("quotientDouble=" + quotientDouble);
23 | // 因float和double类型为约数表示,故相除得到的商也是约数,不能保证小数部分是精确的
24 | double quotientDecimal = 8.1 / 3;
25 | System.out.println("quotientDecimal=" + quotientDecimal);
26 | // 对浮点数和双精度数求余数,也存在约数造成的问题,即余数的小数部分可能并不准确
27 | double remainderDecimal = 5.1 % 2;
28 | System.out.println("remainderDecimal=" + remainderDecimal);
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/chapter08/src/com/special/function/TestFunctional.java:
--------------------------------------------------------------------------------
1 | package com.special.function;
2 |
3 | //演示外部如何调用函数式接口
4 | public class TestFunctional {
5 |
6 | public static void main(String[] args) {
7 | testCock(); // 测试公鸡在半夜干了啥
8 | testCat(); // 测试老猫在半夜干了啥
9 | testPig(); // 测试猪仔在半夜干了啥
10 | }
11 |
12 | // 测试公鸡在半夜干了啥
13 | private static void testCock() {
14 | Animal cock = new Animal("公鸡"); // 创建一个公鸡实例
15 | // 调用midnight方法时,传入匿名内部类的实例
16 | cock.midnight(new Behavior() {
17 | @Override
18 | public void act() {
19 | System.out.println(cock.getName() + "在叫啦。");
20 | }
21 | });
22 | // 调用midnight方法时,传入Lambda表达式的代码。
23 | // 匿名方法不存在输入参数的话,也要保留一对圆括号占位子。
24 | cock.midnight(() -> System.out.println(cock.getName() + "在叫啦。"));
25 | }
26 |
27 | // 测试老猫在半夜干了啥
28 | private static void testCat() {
29 | Animal cat = new Animal("老猫"); // 创建一个老猫实例
30 | // 调用midnight方法时,传入Lambda表达式的代码
31 | cat.midnight(() -> System.out.println(cat.getName() + "在捉老鼠。"));
32 | }
33 |
34 | // 测试猪仔在半夜干了啥
35 | private static void testPig() {
36 | Animal pig = new Animal("猪仔"); // 创建一个猪仔实例
37 | // 调用midnight方法时,传入Lambda表达式的代码
38 | pig.midnight(() -> System.out.println(pig.getName() + "在呼呼大睡。"));
39 | }
40 |
41 | }
42 |
--------------------------------------------------------------------------------
/chapter02/src/com/arithmetic/Niudundiedai.java:
--------------------------------------------------------------------------------
1 | package com.arithmetic;
2 |
3 | //演示利用牛顿迭代法计算n次方根
4 | public class Niudundiedai {
5 |
6 | public static void main(String[] arg) {
7 | double number = 7; // 需要求n次方根的数字
8 | int n = 3; // n次方根的n
9 | double Xm = number; // 每次迭代后的数值
10 | double slope; // 曲线的导数,切线的斜率,物理学的瞬时速度
11 |
12 | slope = n * Math.pow(Xm, n-1); // f(Xm)的导数,也就是经过该点的切线斜率
13 | Xm = Xm - (Math.pow(Xm, n)-number)/slope; // 第一次迭代后的n次方根
14 | System.out.println(number+"的"+n+"次方根="+Xm);
15 |
16 | slope = n * Math.pow(Xm, n-1); // f(Xm)的导数,也就是经过该点的切线斜率
17 | Xm = Xm - (Math.pow(Xm, n)-number)/slope; // 第二次迭代后的n次方根
18 | System.out.println(number+"的"+n+"次方根="+Xm);
19 |
20 | slope = n * Math.pow(Xm, n-1); // f(Xm)的导数,也就是经过该点的切线斜率
21 | Xm = Xm - (Math.pow(Xm, n)-number)/slope; // 第三次迭代后的n次方根
22 | System.out.println(number+"的"+n+"次方根="+Xm);
23 |
24 | slope = n * Math.pow(Xm, n-1); // f(Xm)的导数,也就是经过该点的切线斜率
25 | Xm = Xm - (Math.pow(Xm, n)-number)/slope; // 第四次迭代后的n次方根
26 | System.out.println(number+"的"+n+"次方根="+Xm);
27 |
28 | slope = n * Math.pow(Xm, n-1); // f(Xm)的导数,也就是经过该点的切线斜率
29 | Xm = Xm - (Math.pow(Xm, n)-number)/slope; // 第五次迭代后的n次方根
30 | System.out.println(number+"的"+n+"次方根="+Xm);
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/chapter05/src/com/string/character/TypeChar.java:
--------------------------------------------------------------------------------
1 | package com.string.character;
2 |
3 | //演示字符类型、字符数组的用法,以及特殊字符的转义处理
4 | public class TypeChar {
5 |
6 | public static void main(String[] arg) {
7 | char a = 'A'; // 声明一个字符变量,并对其赋值
8 | System.out.println("a=" + a);
9 | char tian = '田'; // 字符包括英文字符,也包括中文字符
10 | System.out.println("tian=" + tian);
11 | char one = '1'; // 字符还包括数字字符,以及标点符号
12 | System.out.println("one=" + one);
13 | char[] array = new char[]{'A', 'B', 'C'}; // 声明一个字符数组,并对其初始化
14 | //char[] array = { 'A', 'B', 'C' }; // 简化之后的字符数组初始化操作
15 | for (char item : array) { // 遍历并打印字符数组中的每个字符
16 | System.out.println("item=" + item);
17 | }
18 | // 下列是特殊字符的转义表达形式
19 | char tab = '\t'; // 制表符的转义符为\t
20 | System.out.println("tab=" + tab);
21 | char enter = '\r'; // 回车符的转义符为\r
22 | System.out.println("enter=" + enter);
23 | char line = '\n'; // 换行符的转义符为\n
24 | System.out.println("line=" + line);
25 | char singleQuote = '\''; // 单引号的转义符为\'
26 | System.out.println("singleQuote=" + singleQuote);
27 | char doubleQuote = '\"'; // 双引号的转义符为\"
28 | System.out.println("doubleQuote=" + doubleQuote);
29 | char reverseTilt = '\\'; // 反斜杆的转义符为\\
30 | System.out.println("reverseTilt=" + reverseTilt);
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/chapter08/src/com/special/function/StringUtil.java:
--------------------------------------------------------------------------------
1 | package com.special.function;
2 |
3 | import java.util.Arrays;
4 |
5 | //定义字符串工具类
6 | public class StringUtil {
7 |
8 | // 根据过滤器StringFilter从字符串数组挑选符合条件的元素,并重组成新数组返回。
9 | // 其中StringFilter只校验完整的字符串。
10 | public static String[] select(String[] originArray, StringFilter filter) {
11 | int count = 0;
12 | String[] resultArray = new String[0];
13 | for (String str : originArray) { // 遍历所有字符串
14 | if (filter.isMatch(str)) { // 符合过滤条件
15 | count++;
16 | resultArray = Arrays.copyOf(resultArray, count); // 数组容量增大一个
17 | resultArray[count-1] = str; // 往数组末尾填入刚才找到的字符串
18 | }
19 | }
20 | return resultArray;
21 | }
22 |
23 | // 根据过滤器StringFilter2从字符串数组挑选符合条件的元素,并重组成新数组返回。
24 | // 其中StringFilter2根据标记串校验字符串。
25 | public static String[] select2(String[] originArray, StringFilter2 filter, String sign) {
26 | int count = 0;
27 | String[] resultArray = new String[0];
28 | for (String str : originArray) { // 遍历所有字符串
29 | if (filter.isMatch(str, sign)) { // 符合过滤条件
30 | count++;
31 | resultArray = Arrays.copyOf(resultArray, count); // 数组容量增大一个
32 | resultArray[count - 1] = str; // 往数组末尾填入刚才找到的字符串
33 | }
34 | }
35 | return resultArray;
36 | }
37 |
38 | }
39 |
--------------------------------------------------------------------------------
/chapter08/src/com/special/function/MathUtil.java:
--------------------------------------------------------------------------------
1 | package com.special.function;
2 |
3 | //定义数学工具类
4 | public class MathUtil {
5 |
6 | // 加法运算
7 | public double add(double x, double y) {
8 | return x + y;
9 | }
10 |
11 | // 减法运算
12 | public double minus(double x, double y) {
13 | return x - y;
14 | }
15 |
16 | // 乘法运算
17 | public double multiply(double x, double y) {
18 | return x * y;
19 | }
20 |
21 | // 除法运算
22 | public double divide(double x, double y) {
23 | return x / y;
24 | }
25 |
26 | // 取余数运算
27 | public double remainder(double x, double y) {
28 | return x % y;
29 | }
30 |
31 | // 取两数的较大值
32 | public double max(double x, double y) {
33 | return Math.max(x, y);
34 | }
35 |
36 | // 取两数的较小值
37 | public double min(double x, double y) {
38 | return Math.min(x, y);
39 | }
40 |
41 | // 幂运算,即乘方
42 | public double pow(double x, double y) {
43 | return Math.pow(x, y);
44 | }
45 |
46 | // 求方根运算,即开方
47 | public double sqrt(double x, double y) {
48 | double number = x; // 需要求n次方根的数字
49 | double root = x; // 每次迭代后的数值
50 | double n = y; // n次方根的n
51 | for (int i = 0; i < 5; i++) { // 下面利用牛顿迭代法求n次方根
52 | root = (root * (n - 1) + number / Math.pow(root, n - 1)) / n;
53 | }
54 | return root;
55 | }
56 |
57 | }
58 |
--------------------------------------------------------------------------------
/chapter09/src/com/collect/algorithm/TestQueue.java:
--------------------------------------------------------------------------------
1 | package com.collect.algorithm;
2 |
3 | //演示FIFO、LRU两种算法及其数据结构
4 | public class TestQueue {
5 |
6 | public static void main(String[] args) {
7 | testFifo(); // 测试FIFO算法用到的数据结构
8 | testLru(); // 测试LRU算法用到的数据结构
9 | }
10 |
11 | // 测试FIFO算法(先进先出)用到的数据结构
12 | private static void testFifo() {
13 | // 声明一个容量为5的先进先出队列
14 | FifoList fifoList = new FifoList(5);
15 | //String str = "abcdefghijklfghlijf";
16 | String str = "先天下之忧而忧后天下之乐而乐天天快乐";
17 | for (int i = 0; i < str.length(); i++) {
18 | fifoList.add(str.charAt(i)); // 把字符加入先进先出队列
19 | }
20 | System.out.println("先进先出队列的大小为" + fifoList.size());
21 | System.out.println("先进先出队列的当前元素包括:" + fifoList);
22 | }
23 |
24 | // 测试LRU算法(最久未使用)用到的数据结构
25 | private static void testLru() {
26 | // 声明一个容量为5的最久未使用队列
27 | LruMap lruMap = new LruMap(5);
28 | //String str = "abcdefghijklfghlijf";
29 | String str = "先天下之忧而忧后天下之乐而乐天天快乐";
30 | for (int i = 0; i < str.length(); i++) {
31 | // 把字符加入最久未使用队列。其中键名为该字符,键值为序号
32 | lruMap.put(str.charAt(i), i);
33 | }
34 | System.out.println("最久未使用队列的大小为" + lruMap.size());
35 | System.out.println("最久未使用队列的当前元素包括:" + lruMap);
36 | }
37 |
38 | }
39 |
--------------------------------------------------------------------------------
/chapter17/src/com/database/properties/TestProperties.java:
--------------------------------------------------------------------------------
1 | package com.database.properties;
2 |
3 | //演示属性文件工具类的用法
4 | public class TestProperties {
5 |
6 | public static void main(String[] args) {
7 | // 以下拼接数据库配置文件db.properties的完整路径
8 | String class_path = PropertiesUtil.class.getResource("/").getFile();
9 | String config_path = String.format("%s/%s", class_path, "db.properties");
10 | //System.out.println("config_path="+config_path);
11 | //write(config_path); // 写入属性文件
12 | read(config_path); // 读取属性文件
13 | }
14 |
15 | // 写入属性文件
16 | private static void write(String config_path) {
17 | PropertiesUtil prop = new PropertiesUtil(config_path); // 创建一个属性工具
18 | prop.writeString("jdbc.connection.username", "root"); // 写入数据库的用户名
19 | prop.writeString("jdbc.connection.password", "111111"); // 写入数据库的密码
20 | prop.commit(); // 提交属性文件的修改
21 | }
22 |
23 | // 读取属性文件
24 | private static void read(String config_path) {
25 | PropertiesUtil prop = new PropertiesUtil(config_path); // 创建一个属性工具
26 | String username = prop.readString("jdbc.connection.username", ""); // 读取数据库的用户名
27 | String password = prop.readString("jdbc.connection.password", ""); // 读取数据库的密码
28 | System.out.println("username="+username+", password="+password);
29 | }
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/chapter08/src/com/special/inner/TreeInner.java:
--------------------------------------------------------------------------------
1 | package com.special.inner;
2 |
3 | //演示内部类如何访问外部类的成员
4 | public class TreeInner {
5 | private String tree_name;
6 |
7 | public TreeInner(String tree_name) {
8 | this.tree_name = tree_name;
9 | }
10 |
11 | public void sprout() {
12 | System.out.println(tree_name + "发芽啦");
13 | }
14 |
15 | // Flower类位于TreeInner类的内部,它是个内部类
16 | public class Flower {
17 | private String flower_name;
18 | private String tree_name;
19 |
20 | public Flower(String flower_name) {
21 | this.flower_name = flower_name;
22 | }
23 |
24 | public Flower(String tree_name, String flower_name) {
25 | this.tree_name = tree_name;
26 | this.flower_name = flower_name;
27 | }
28 |
29 | // 该方法访问默认的flower_name字段
30 | public void bloom() {
31 | System.out.println(flower_name + "开花啦");
32 | }
33 |
34 | // 该方法访问内部类自身的tree_name字段
35 | public void bloomInnerTree() {
36 | // 内部类里面的this关键字指代内部类自身
37 | System.out.println(this.tree_name + "的" + flower_name + "开花啦");
38 | }
39 |
40 | // 该方法访问外部类TreeInner的tree_name字段
41 | public void bloomOuterTree() {
42 | // 要想在内部类里面访问外部类的成员,就必须在this之前添加“外部类的名称.”
43 | System.out.println(TreeInner.this.tree_name + "的" + flower_name
44 | + "开花啦");
45 | }
46 | }
47 |
48 | }
49 |
--------------------------------------------------------------------------------
/chapter15/src/com/concurrent/stopwatch/watch.fxml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
11 |
12 |
13 |
14 |
15 |
17 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/chapter17/src/com/database/properties/PropertiesUtil.java:
--------------------------------------------------------------------------------
1 | package com.database.properties;
2 |
3 | import java.io.FileInputStream;
4 | import java.io.FileOutputStream;
5 | import java.util.Properties;
6 |
7 | //定义属性文件的工具类
8 | public class PropertiesUtil {
9 | private Properties mProp; // 属性表
10 | private String mConfigPath; // 配置文件的路径
11 |
12 | public PropertiesUtil(String config_path) {
13 | mConfigPath = config_path;
14 | // 根据指定路径构建文件输入流对象
15 | try (FileInputStream fis = new FileInputStream(mConfigPath)) {
16 | mProp = new Properties(); // 创建一个属性表对象
17 | mProp.load(fis); // 把输入流中的数据加载到属性表
18 | } catch (Exception e) {
19 | e.printStackTrace();
20 | }
21 | }
22 |
23 | // 读取指定名称的属性值
24 | public String readString(String name, String defaultValue) {
25 | return mProp.getProperty(name, defaultValue); // 从属性表中获取指定名称的属性值
26 | }
27 |
28 | // 写入指定名称的属性值
29 | public void writeString(String name, String value) {
30 | mProp.setProperty(name, value); // 把指定名称的属性值写入属性表
31 | }
32 |
33 | // 提交属性表的修改
34 | public void commit() {
35 | // 根据指定路径构建文件输出流对象
36 | try (FileOutputStream fos = new FileOutputStream(mConfigPath)) {
37 | mProp.store(fos, ""); // 把属性表的数据保存到输出流
38 | } catch (Exception e) {
39 | e.printStackTrace();
40 | }
41 | }
42 |
43 | }
44 |
--------------------------------------------------------------------------------
/chapter02/src/com/arithmetic/operator/Unary.java:
--------------------------------------------------------------------------------
1 | package com.arithmetic.operator;
2 |
3 | //演示一元运算符的用法
4 | public class Unary {
5 |
6 | public static void main(String[] args) {
7 | int x = 3;
8 | System.out.println("初始 x=" + x);
9 | x++; // 等同于x=x+1或者x+=1
10 | System.out.println("自增1 x=" + x);
11 | x--; // 等同于x=x-1或者x-=1
12 | System.out.println("自减1 x=" + x);
13 | // 没有“**”这个运算符,求平方还是按照常规写法
14 | x *= x; // 也可以写成x = x*x
15 | System.out.println("求平方 x=" + x);
16 | // “//”已经被用作注释标记了,求倒数也得按照常规写法,而且整数的倒数只能是小数
17 | double y = 1.0 / x; // 注意这里的1.0/x,由于x是整型数,因此1/x无法求得小数
18 | System.out.println("求倒数 y=" + y);
19 | x = -x; // 等同于x=0-x
20 | System.out.println("负数 x=" + x);
21 | x = +x; // 等同于x=0+x
22 | System.out.println("正数 x=" + x);
23 | int y1 = 7;
24 | int z1 = y1++; // 后加加操作的优先级较低
25 | /*
26 | * int z1 = y1++; 上面语句在执行时会分解成两个步骤:先执行对z1的赋值操作,再执行对y1的自增操作。 int z1 = y1;
27 | * y1 = y1+1;
28 | */
29 | System.out.println("z1=" + z1);
30 | int y2 = 7;
31 | int z2 = ++y2; // 前加加操作的优先级较高
32 | /*
33 | * int z2 = ++y2; 上面语句在执行时会分解成两个步骤:先执行对y1的自增操作,再执行对z1的赋值操作。 y2 = y2+1;
34 | * int z2 = y2;
35 | */
36 | System.out.println("z2=" + z2);
37 | // 脑筋急转弯,猜猜下面的z3为何值?
38 | int z3 = ++z1 + z2++;
39 | System.out.println("z3=" + z3);
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/chapter16/src/com/network/im_server/ChatServer.java:
--------------------------------------------------------------------------------
1 | package com.network.im_server;
2 |
3 | import java.net.ServerSocket;
4 | import java.net.Socket;
5 | import java.util.ArrayList;
6 | import java.util.List;
7 |
8 | import com.network.DateUtil;
9 |
10 | //聊天服务器
11 | public class ChatServer {
12 | private static final int SOCKET_PORT = 52000; // 聊天服务的侦听端口
13 | public static List mSocketList = new ArrayList(); // 容纳客户端套接字的列表
14 |
15 | public static void main(String[] args) {
16 | ChatServer server = new ChatServer();
17 | server.initServer(); // 初始化服务器
18 | }
19 |
20 | // 初始化服务器
21 | private void initServer() {
22 | System.out.println("Socket聊天服务已启动");
23 | try {
24 | // 创建一个ServerSocket,用于监听客户端Socket的连接请求
25 | ServerSocket server = new ServerSocket(SOCKET_PORT);
26 | while (true) {
27 | // 每当接收到客户端的Socket请求,服务器端也相应的创建一个Socket
28 | Socket clientSocket = server.accept();
29 | // 为该客户端创建单独的套接字详情对象
30 | SocketBean socket = new SocketBean(DateUtil.getTimeId(), clientSocket);
31 | mSocketList.add(socket); // 把新的客户端套接字添加进列表
32 | System.out.println("连接了一个客户端");
33 | // 每连接一个客户端,就启动一个服务线程伺候该客户端
34 | new Thread(new ServerTask(socket)).start();
35 | }
36 | } catch (Exception e) {
37 | e.printStackTrace();
38 | }
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/chapter16/src/com/network/socket/TestUdpClient.java:
--------------------------------------------------------------------------------
1 | package com.network.socket;
2 |
3 | import java.net.DatagramPacket;
4 | import java.net.DatagramSocket;
5 | import java.net.InetAddress;
6 |
7 | import com.network.PrintUtils;
8 |
9 | //演示Socket客户端的运行(UDP协议的不可靠连接)
10 | public class TestUdpClient {
11 | // 以下为Socket服务器的IP和端口,根据实际情况修改
12 | private static final String SOCKET_IP = "192.168.1.8";
13 | private static final int UDP_PORT = 61000; // UDP传输专用端口
14 |
15 | public static void main(String[] args) {
16 | startUdpClient("Hello World"); // 启动UDP客户端发送文本消息
17 | startUdpClient("你好,世界"); // 启动UDP客户端发送文本消息
18 | }
19 |
20 | // 启动UDP客户端发送文本消息
21 | private static void startUdpClient(String message) {
22 | PrintUtils.print("UDP客户端发送消息:" + message);
23 | // 创建一个DatagramSocket对象
24 | try (DatagramSocket socket = new DatagramSocket()) {
25 | // 根据IP地址获得对应的网络地址对象
26 | InetAddress serverAddress = InetAddress.getByName(SOCKET_IP);
27 | byte data[] = message.getBytes(); // 把字符串转换为字节数组
28 | // 创建一个DatagramPacket对象,构造方法的四个参数依次为:
29 | // 待发送的数据、数据的长度、服务器的网络地址、服务器的端口号。
30 | DatagramPacket packet = new DatagramPacket(data, data.length, serverAddress, UDP_PORT);
31 | socket.send(packet); // 向服务器发送数据包
32 | } catch (Exception e) {
33 | e.printStackTrace();
34 | }
35 | }
36 |
37 | }
38 |
--------------------------------------------------------------------------------
/chapter17/src/com/database/poem/PoemColumn.java:
--------------------------------------------------------------------------------
1 | package com.database.poem;
2 |
3 | import javafx.beans.property.SimpleStringProperty;
4 |
5 | //定义诗歌列的信息类
6 | public class PoemColumn {
7 | private SimpleStringProperty id; // 编号
8 | private SimpleStringProperty title; // 名称
9 | private SimpleStringProperty author; // 类型
10 | private SimpleStringProperty dynasty; // 说明
11 |
12 | public PoemColumn(String id, String title, String author, String dynasty) {
13 | this.id = new SimpleStringProperty(id);
14 | this.title = new SimpleStringProperty(title);
15 | this.author = new SimpleStringProperty(author);
16 | this.dynasty = new SimpleStringProperty(dynasty);
17 | }
18 |
19 | public String getId() {
20 | return id.get();
21 | }
22 |
23 | public void setId(String id) {
24 | this.id.set(id);
25 | }
26 |
27 | public String getTitle() {
28 | return title.get();
29 | }
30 |
31 | public void setTitle(String title) {
32 | this.title.set(title);
33 | }
34 |
35 | public String getAuthor() {
36 | return author.get();
37 | }
38 |
39 | public void setAuthor(String author) {
40 | this.author.set(author);
41 | }
42 |
43 | public String getDynasty() {
44 | return dynasty.get();
45 | }
46 |
47 | public void setDynasty(String dynasty) {
48 | this.dynasty.set(dynasty);
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/chapter07/src/com/object/encapsulate/OrangeMember.java:
--------------------------------------------------------------------------------
1 | package com.object.encapsulate;
2 |
3 | //演示类的封装,对成员属性和成员方法的定义
4 | public class OrangeMember {
5 | private String name; // 定义了橘子的名称
6 | private double weight; // 定义了橘子的重量
7 | private boolean isRipe; // 定义了橘子是否成熟。true表示成熟,false表示未成熟
8 | private String place; // 定义了橘子的产地
9 |
10 | // 设置橘子的产地
11 | public void setPlace(String inputPlace) {
12 | place = inputPlace;
13 | name = (place.equals("淮北")) ? "枳子" : "橘子";
14 | }
15 |
16 | // 获取橘子的产地
17 | public String getPlace() {
18 | return place;
19 | }
20 |
21 | // 设置橘子的名称
22 | public void setName(String inputName) {
23 | name = inputName;
24 | }
25 |
26 | // 获取橘子的名称
27 | public String getName() {
28 | return name;
29 | }
30 |
31 | // 设置橘子的重量
32 | public void setWeight(double inputWeight) {
33 | weight = inputWeight;
34 | }
35 |
36 | // 获取橘子的重量
37 | public double getWeight() {
38 | return weight;
39 | }
40 |
41 | // 设置橘子是否成熟
42 | public void setRipe(boolean inputRipe) {
43 | isRipe = inputRipe;
44 | }
45 |
46 | // 获取橘子是否成熟
47 | public boolean getRipe() {
48 | return isRipe;
49 | }
50 |
51 | // 输出各属性字段的取值
52 | public String toString() {
53 | String desc = String.format("这个%s的重量是%f克,%s成熟,产地是%s。", name, weight,
54 | isRipe ? "已" : "未", place);
55 | return desc;
56 | }
57 |
58 | }
59 |
--------------------------------------------------------------------------------
/chapter07/src/com/object/trade/TestAccount.java:
--------------------------------------------------------------------------------
1 | package com.object.trade;
2 |
3 | import com.object.account.BankAccount;
4 |
5 | // 演示各种账户类的使用
6 | public class TestAccount {
7 |
8 | public static void main(String[] args) {
9 | testBankAccount(); // 演示银行账户的操作
10 | testSecurityAccount(); // 演示证券账户的操作
11 | }
12 |
13 | // 演示银行账户的操作
14 | private static void testBankAccount() {
15 | BankAccount bank = new BankAccount(); // 创建一个银行账户
16 | bank.saveCurrent(5000); // 活期账户存入5000元
17 | bank.saveDeposit(6, 5000); // 存入6个月定期的5000元
18 | System.out.println("第一次存款操作之后,"+bank.toString());
19 | bank.takeCurrent(2000); // 活期账户取出2000元
20 | bank.takeDeposit(6); // 取出6个月定期存款
21 | bank.saveDeposit(12, 5000); // 存入12个月定期的5000元
22 | System.out.println("第二次存款操作之后,"+bank.toString());
23 | }
24 |
25 | // 演示证券账户的操作
26 | private static void testSecurityAccount() {
27 | SecurityAccount security = new SecurityAccount(); // 创建一个证券账户
28 | security.saveFund(10000); // 可用资金存入10000元
29 | security.buyStock("601398", "工商银行", 500); // 买入500股工商银行
30 | System.out.println("第一次股票交易之后,"+security.toString());
31 | security.takeFund(3000); // 可用资金取出3000元
32 | security.sellStock("601398", 100); // 卖出100股工商银行
33 | security.buyStock("601857", "中国石油", 300); // 买入300股中国石油
34 | System.out.println("第二次股票交易之后,"+security.toString());
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/chapter02/src/com/arithmetic/math/Trunc.java:
--------------------------------------------------------------------------------
1 | package com.arithmetic.math;
2 |
3 | //演示数学函数库中的取整函数用法
4 | public class Trunc {
5 |
6 | public static void main(String[] args) {
7 | double decimalPositive = 9.9;
8 | // 注意下面的9.4999999999999999,取整之后会变成10。
9 | // 这是因为9.4999999999999999超出了双精度数的精度范围,Java会先将其转为近似9.5的小数,然后再进行取整操作
10 | // double decimalPositive = 9.4999999999999999;
11 | long roundPositive = Math.round(decimalPositive); // 四舍五入
12 | System.out.println("roundPositive=" + roundPositive);
13 | double floorPositive = Math.floor(decimalPositive); // 往下取整,也就是往数值小的方向取整
14 | System.out.println("floorPositive=" + floorPositive);
15 | double ceilPositive = Math.ceil(decimalPositive); // 往上取整,也就是往数值大的方向取整
16 | System.out.println("ceilPositive=" + ceilPositive);
17 |
18 | double decimalNegative = -9.9;
19 | long roundNegative = Math.round(decimalNegative); // 四舍五入
20 | System.out.println("roundNegative=" + roundNegative);
21 | double floorNegative = Math.floor(decimalNegative); // 往下取整,也就是往数值小的方向取整
22 | System.out.println("floorNegative=" + floorNegative);
23 | double ceilNegative = Math.ceil(decimalNegative); // 往上取整,也就是往数值大的方向取整
24 | System.out.println("ceilNegative=" + ceilNegative);
25 |
26 | double absoluteValue = Math.abs(decimalNegative); // 取绝对值
27 | System.out.println("absoluteValue=" + absoluteValue);
28 | }
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/chapter09/src/com/collect/generic/Apple.java:
--------------------------------------------------------------------------------
1 | package com.collect.generic;
2 |
3 | //定义一个苹果类
4 | public class Apple {
5 | private String name; // 名称
6 | private String color; // 颜色
7 | private Double weight; // 重量
8 | private Double price; // 价格
9 |
10 | public Apple(String name, String color, Double weight, Double price) {
11 | this.name = name;
12 | this.color = color;
13 | this.weight = weight;
14 | this.price = price;
15 | }
16 |
17 | public void setName(String name) {
18 | this.name = name;
19 | }
20 |
21 | public String getName() {
22 | return this.name;
23 | }
24 |
25 | public void setColor(String color) {
26 | this.color = color;
27 | }
28 |
29 | public String getColor() {
30 | return this.color;
31 | }
32 |
33 | public void setWeight(Double weight) {
34 | this.weight = weight;
35 | }
36 |
37 | public Double getWeight() {
38 | return this.weight;
39 | }
40 |
41 | public void setPrice(Double price) {
42 | this.price = price;
43 | }
44 |
45 | public Double getPrice() {
46 | return this.price;
47 | }
48 |
49 | // 获取该苹果的详细描述文字
50 | public String toString() {
51 | return String.format("\n(name=%s,color=%s,weight=%f,price=%f)", name,
52 | color, weight, price);
53 | }
54 |
55 | // 判断是否红苹果
56 | public boolean isRedApple() {
57 | return this.color.toLowerCase().equals("red");
58 | }
59 |
60 | }
61 |
--------------------------------------------------------------------------------
/chapter16/src/com/network/im_client/QQLoginMain.java:
--------------------------------------------------------------------------------
1 | package com.network.im_client;
2 |
3 | import javafx.application.Application;
4 | import javafx.event.EventHandler;
5 | import javafx.fxml.FXMLLoader;
6 | import javafx.scene.Parent;
7 | import javafx.scene.Scene;
8 | import javafx.stage.Stage;
9 | import javafx.stage.WindowEvent;
10 |
11 | //聊天工具登录窗口的程序入口
12 | public class QQLoginMain extends Application {
13 |
14 | @Override
15 | public void start(Stage stage) throws Exception { // 应用程序开始运行
16 | stage.setTitle("请登录QQ"); // 设置舞台的标题
17 | stage.getIcons().add(QQPartner.QQ_LOGO); // 设置标题栏图标
18 | // 从FXML资源文件中加载程序的初始界面
19 | Parent root = FXMLLoader.load(getClass().getResource("qq_login.fxml"));
20 | Scene scene = new Scene(root, 500, 420); // 创建一个场景
21 | stage.setScene(scene); // 设置舞台的场景
22 | stage.setResizable(false); // 设置舞台的尺寸是否允许变化
23 | stage.show(); // 显示舞台
24 | // 设置舞台的关闭请求事件,在用户单击右上角叉号按钮时,会触发这里的handle方法
25 | stage.setOnCloseRequest(new EventHandler() {
26 | @Override
27 | public void handle(WindowEvent event) {
28 | // 关闭窗口之前,要先通知服务器注销当前账号
29 | QQPartner partner = QQPartner.getInstance(""); // 获取当前的QQ伴侣
30 | partner.sendAction(QQPartner.LOGOUT, "", ""); // 吩咐QQ伴侣去注销
31 | }
32 | });
33 | }
34 |
35 | public static void main(String[] args) {
36 | launch(args); // 启动JavaFX应用,接下来会跳到start方法
37 | }
38 |
39 | }
40 |
--------------------------------------------------------------------------------
/chapter08/src/com/special/inner/TestInner.java:
--------------------------------------------------------------------------------
1 | package com.special.inner;
2 |
3 | //演示外部如何调用内部类和嵌套类
4 | public class TestInner {
5 |
6 | public static void main(String[] args) {
7 | testTree(); // 演示外部类的调用方法
8 | testInner(); // 演示内部类的调用方法
9 | testNest(); // 演示嵌套类的调用方法
10 | }
11 |
12 | // 演示外部类的调用方法
13 | private static void testTree() {
14 | // 先创建外部类的实例,再基于该实例去创建内部类的实例
15 | Tree tree = new Tree("杨树"); // 创建一个树木实例
16 | tree.sprout(); // 调用树木实例的sprout方法
17 | Tree.Flower flower = tree.new Flower("杨花"); // 通过树木实例创建内部类的花朵实例
18 | flower.bloom(); // 调用花朵实例的bloom方法
19 | }
20 |
21 | // 演示内部类的调用方法
22 | private static void testInner() {
23 | // 先创建外部类的实例,再基于该实例去创建内部类的实例
24 | TreeInner inner = new TreeInner("桃树");
25 | // 创建一个内部类的实例,需要在new之前添加“外层类的实例名.”
26 | TreeInner.Flower flower = inner.new Flower("桃花");
27 | flower.bloom(); // 调用内部类实例的bloom方法
28 | // 重新创建Flower类的实例,它的构造方法指定了另一个树木名称tree_name
29 | TreeInner.Flower flower2 = inner.new Flower("碧桃", "桃花");
30 | // bloomInnerTree方法里面访问了内部类自身的tree_name字段
31 | flower2.bloomInnerTree();
32 | // bloomOuterTree方法里面访问了外部类TreeInner的tree_name字段
33 | flower2.bloomOuterTree();
34 | }
35 |
36 | // 演示嵌套类的调用方法
37 | private static void testNest() {
38 | // 创建一个嵌套类的实例,格式为“new 外层类的名称.嵌套类的名称(...)”
39 | TreeNest.Flower flower = new TreeNest.Flower("茉莉花");
40 | flower.bloom(); // 调用嵌套类实例的bloom方法
41 | }
42 |
43 | }
44 |
--------------------------------------------------------------------------------
/chapter08/src/com/special/inner/TestStatic.java:
--------------------------------------------------------------------------------
1 | package com.special.inner;
2 |
3 | //演示外部如何调用静态属性和静态方法
4 | public class TestStatic {
5 |
6 | public static void main(String[] args) {
7 | testStaticMember(); // 演示静态成员的调用方式
8 | testStaticBlock(); // 演示静态代码块与构造方法的执行顺序
9 | testStaticProperty(); // 演示静态属性的持久性
10 | }
11 |
12 | // 演示静态成员的调用方式
13 | private static void testStaticMember() {
14 | // 使用静态属性无需创建该类的实例,只要通过“类名.静态属性名”即可访问静态属性
15 | System.out.println("类型TYPE_ARBOR的取值为" + TreeStatic.TYPE_ARBOR);
16 | System.out.println("类型TYPE_BUSH的取值为" + TreeStatic.TYPE_BUSH);
17 | // 使用静态方法无需创建该类的实例,只要通过“类名.静态方法名”即可访问静态方法
18 | String arbor_name = TreeStatic.getTypeName(TreeStatic.TYPE_ARBOR);
19 | System.out.println("类型TYPE_ARBOR对应的名称是" + arbor_name);
20 | String bush_name = TreeStatic.getTypeName(TreeStatic.TYPE_BUSH);
21 | System.out.println("类型TYPE_BUSH对应的名称是" + bush_name);
22 | }
23 |
24 | // 演示静态代码块与构造方法的执行顺序
25 | private static void testStaticBlock() {
26 | System.out.println("开始创建树木类的实例");
27 | TreeStatic tree = new TreeStatic("月桂");
28 | System.out.println("结束创建树木类的实例");
29 | }
30 |
31 | // 演示静态属性的持久性
32 | private static void testStaticProperty() {
33 | TreeStatic bigTree = new TreeStatic("大树"); // 创建一个大树实例
34 | bigTree.grow(); // 大树在生长
35 | TreeStatic littleTree = new TreeStatic("小树"); // 创建一个小树实例
36 | littleTree.grow(); // 小树在生长
37 | }
38 |
39 | }
40 |
--------------------------------------------------------------------------------
/chapter12/src/com/awt/window/TestFlow.java:
--------------------------------------------------------------------------------
1 | package com.awt.window;
2 |
3 | import java.awt.Button;
4 | import java.awt.Font;
5 | import java.awt.Frame;
6 | import java.awt.Panel;
7 | import java.awt.event.WindowAdapter;
8 | import java.awt.event.WindowEvent;
9 |
10 | //演示多个控件在窗口上的默认布局
11 | public class TestFlow {
12 |
13 | public static void main(String[] args) {
14 | final Frame frame = new Frame("测试默认布局"); // 创建一个窗口对象
15 |
16 | frame.addWindowListener(new WindowAdapter() { // 为窗口注册监听器,实现窗口关闭功能
17 | public void windowClosing(WindowEvent e) { // 单击了窗口右上角的叉号按钮
18 | frame.dispose(); // 关闭窗口
19 | }
20 | });
21 |
22 | frame.setSize(400, 120); // 必须设置宽高,否则没有窗体
23 | frame.setLocationRelativeTo(null); // 将窗口居中。若无该方法,窗口将位于屏幕左上角
24 |
25 | Panel panel = new Panel(); // 创建一个面板
26 | panel.add(getButton("第一个按钮")); // 在面板上添加一个按钮
27 | panel.add(getButton("第二个按钮")); // 在面板上添加一个按钮
28 | panel.add(getButton("第三个按钮")); // 在面板上添加一个按钮
29 | panel.add(getButton("第四个按钮")); // 在面板上添加一个按钮
30 | panel.add(getButton("第五个按钮")); // 在面板上添加一个按钮
31 | frame.add(panel); // 在窗口上添加面板
32 |
33 | // setVisible方法放到最后面,这样才能显示所有控件
34 | frame.setVisible(true); // 必须设置为true,否则看不见
35 | }
36 |
37 | private static Button getButton(String text) {
38 | Button button = new Button(text);
39 | button.setFont(new Font("", Font.PLAIN, 16)); // 设置按钮的字体大小
40 | return button;
41 | }
42 |
43 | }
44 |
--------------------------------------------------------------------------------
/chapter14/src/com/javafx/loan/sample.fxml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/chapter02/src/com/arithmetic/numerical/Convert.java:
--------------------------------------------------------------------------------
1 | package com.arithmetic.numerical;
2 |
3 | //演示基本变量类型之间的转换
4 | public class Convert {
5 |
6 | public static void main(String[] args) {
7 | int changjiang = 6397; // 长江的长度为6397千米
8 | System.out.println("changjiang=" + changjiang);
9 | int longRiver = changjiang; // 把一个整型变量赋值给另一个整型变量
10 | System.out.println("longRiver=" + longRiver);
11 |
12 | // long changjiang = 6397;
13 | // // 若把长整型变量直接赋值给整型变量,编译器会提示错误
14 | // //int longRiver = changjiang; // 把长整型变量赋值给整型变量,注意编译器会报错
15 | // // 不同类型的变量相互赋值,需要在原变量前面添加“(新类型)”表示强制转换类型
16 | // int longRiver = (int) changjiang; // 把长整型数强制转成整型数
17 |
18 | long worldPopulation = 7444443881L; // 截至2018年元旦,世界人口大约有74亿
19 | System.out.println("worldPopulation=" + worldPopulation);
20 | // 把长整型数赋值给整型数会丢失前四个字节
21 | int shijierenkou = (int) worldPopulation; // 把长整型数强制转成整型数
22 | System.out.println("shijierenkou=" + shijierenkou);
23 |
24 | // 3.1415926是中国古代数学家祖冲之求得的圆周率数值,又称祖率
25 | double zulv = 3.1415926;
26 | System.out.println("zulv=" + zulv);
27 | // 把双精度数赋值给浮点数会丢失数值精度
28 | float pai = (float) zulv; // 把双精度数强制转成浮点数
29 | System.out.println("pai=" + pai);
30 | double jiage = 9.9; // 某商品定价为9.9元
31 | System.out.println("jiage=" + jiage);
32 | // 把小数赋值给整型变量,会直接去掉小数点后面部分,不会四舍五入
33 | int price = (int) jiage; // 把双精度数强制转成整型数
34 | System.out.println("price=" + price);
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/chapter03/src/com/control/logic/Priority.java:
--------------------------------------------------------------------------------
1 | package com.control.logic;
2 |
3 | //演示几种运算符类型的优先级顺序
4 | public class Priority {
5 |
6 | public static void main(String[] args) {
7 | // 比较加减乘除以及取余数运算的优先级顺序
8 | int fiveArithmetic = 7 + 5 - 4 * 6 / 3 % 9; // 等价于“7+5-(4*6/3%9)”
9 | System.out.println("fiveArithmetic=" + fiveArithmetic);
10 | // 比较负号与乘除运算的优先级顺序
11 | int negativeArithmetic = -8 / 4 + 2 * -3; // 等价于“(-8)/4+2*(-3)”
12 | System.out.println("negativeArithmetic=" + negativeArithmetic);
13 | // 以下比较算术运算符和关系运算符的优先级顺序
14 | boolean greaterResult = 1 + 2 > 3 + 4; // 等价于“(1+2)>(3+4)”
15 | System.out.println("greaterResult=" + greaterResult);
16 | boolean lessResult = 1 + 2 < 3 + 4; // 等价于“(1+2)<(3+4)”
17 | System.out.println("lessResult=" + lessResult);
18 | // 比较逻辑与运算以及关系运算符的优先级顺序
19 | boolean andResult = 1 > 2 & 3 < 4; // 等价于“(1>2)&(3<4)”
20 | System.out.println("andResult=" + andResult);
21 | // 比较逻辑或运算以及关系运算符的优先级顺序
22 | boolean orResult = 1 > 2 | 3 < 4; // 等价于“(1>2)|(3<4)”
23 | System.out.println("orResult=" + orResult);
24 | // 比较逻辑异或运算以及关系运算符的优先级顺序
25 | boolean xorResult = 1 > 2 ^ 3 < 4; // 等价于“(1>2)^(3<4)”
26 | System.out.println("xorResult=" + xorResult);
27 | // 比较逻辑非运算以及关系运算符的优先级顺序
28 | boolean zhen = true;
29 | boolean jia = false;
30 | boolean notResult = zhen == !jia; // 等价于“zhen==(!jia)”
31 | System.out.println("notResult=" + notResult);
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/chapter12/src/com/awt/widget/TestTextArea.java:
--------------------------------------------------------------------------------
1 | package com.awt.widget;
2 |
3 | import java.awt.Font;
4 | import java.awt.Frame;
5 | import java.awt.Label;
6 | import java.awt.Panel;
7 | import java.awt.TextArea;
8 | import java.awt.event.WindowAdapter;
9 | import java.awt.event.WindowEvent;
10 |
11 | //演示多行输入框的用法
12 | public class TestTextArea {
13 |
14 | public static void main(String[] args) {
15 | final Frame frame = new Frame("测试多行输入框"); // 创建一个窗口对象
16 |
17 | frame.addWindowListener(new WindowAdapter() { // 为窗口注册监听器,实现窗口关闭功能
18 | public void windowClosing(WindowEvent e) { // 单击了窗口右上角的叉号按钮
19 | frame.dispose(); // 关闭窗口
20 | }
21 | });
22 |
23 | frame.setSize(400, 130); // 必须设置宽高,否则没有窗体
24 | frame.setLocationRelativeTo(null);// 将窗口居中。若无该方法,窗口将位于屏幕左上角
25 |
26 | Panel panel = new Panel(); // 创建一个面板
27 |
28 | Label label = new Label("请输入评价内容"); // 创建一个文字标签
29 | label.setFont(new Font("", Font.PLAIN, 16)); // 设置标签的字体大小
30 | panel.add(label); // 在面板上添加文字标签
31 |
32 | TextArea area = new TextArea(); // 创建一个多行输入框
33 | area.setEditable(true); // 设置输入框允许编辑
34 | area.setColumns(20); // 设置输入框的长度为20个字符
35 | area.setRows(3); // 设置输入框的高度为3行字符
36 | area.setFont(new Font("", Font.PLAIN, 16)); // 设置输入框的字体大小
37 | panel.add(area); // 在面板上添加多行输入框
38 |
39 | frame.add(panel); // 在窗口上添加面板
40 |
41 | // setVisible方法放到最后面,这样才能显示所有控件
42 | frame.setVisible(true); // 必须设置为true,否则看不见
43 | }
44 |
45 | }
46 |
--------------------------------------------------------------------------------
/chapter16/src/com/network/parser/XmlSax.java:
--------------------------------------------------------------------------------
1 | package com.network.parser;
2 |
3 | import java.util.HashMap;
4 | import java.util.Map;
5 |
6 | import org.xml.sax.Attributes;
7 | import org.xml.sax.SAXException;
8 | import org.xml.sax.helpers.DefaultHandler;
9 |
10 | //演示通过SAX方式解析XML串
11 | public class XmlSax extends DefaultHandler {
12 | private String mTagName = ""; // 节点名称
13 | // 保存节点名称与节点值的映射对象
14 | private Map item_map = new HashMap();
15 |
16 | // 获取解析之后的节点映射对象
17 | public Map getItemMap() {
18 | return item_map;
19 | }
20 |
21 | @Override
22 | public void startDocument() throws SAXException {}
23 |
24 | @Override
25 | public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
26 | mTagName = qName; // 开始解析某个节点时,先保存该节点的名称
27 | }
28 |
29 | @Override
30 | public void characters(char[] ch, int start, int length) throws SAXException {
31 | if (mTagName!=null && mTagName.length()>0) {
32 | String value = new String(ch, start, length);
33 | value = value.trim(); // 去掉字符串末尾的空格
34 | if (value!=null && value.length()>0) {
35 | // 往节点映射对象中添加新的节点信息
36 | item_map.put(mTagName, value);
37 | //System.out.println("mTagName="+mTagName+",value="+value);
38 | }
39 | }
40 | }
41 |
42 | @Override
43 | public void endElement(String uri, String localName, String qName) throws SAXException {}
44 |
45 | }
46 |
--------------------------------------------------------------------------------
/chapter13/src/com/swing/widget/TestScrollPanel.java:
--------------------------------------------------------------------------------
1 | package com.swing.widget;
2 |
3 | import java.awt.Dimension;
4 | import java.awt.Font;
5 |
6 | import javax.swing.JFrame;
7 | import javax.swing.JPanel;
8 | import javax.swing.JScrollPane;
9 | import javax.swing.JTextArea;
10 |
11 | //演示滚动条的用法
12 | public class TestScrollPanel {
13 |
14 | public static void main(String[] args) {
15 | JFrame frame = new JFrame("测试滚动条的窗口"); // 创建一个窗口对象
16 | frame.setSize(400, 130); // 必须设置宽高,否则没有窗体
17 | frame.setLocationRelativeTo(null);// 将窗口居中。若无该方法,窗口将位于屏幕左上角
18 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置默认的关闭操作:退出程序
19 |
20 | JPanel panel = new JPanel(); // 创建一个面板
21 |
22 | JTextArea area = new JTextArea(); // 创建一个多行输入框
23 | //area.setPreferredSize(new Dimension(180, 30)); // 设置输入框的推荐宽高
24 | area.setFont(new Font("中号", Font.PLAIN, 16)); // 设置输入框的文本字体及其大小
25 | area.setEditable(true); // 设置输入框允许编辑
26 | area.setColumns(14); // 设置输入框的长度为14个字符
27 | area.setRows(3); // 设置输入框的高度为3行字符
28 | area.setLineWrap(true); // 设置每行是否折叠。为true的话,输入字符超过每行宽度就会自动换行
29 |
30 | // 因为下面添加滚动条的时候,滚动条已经关联了JTextArea,所以这里不必单独添加多行输入框
31 | //panel.add(area); // 在面板上添加多行输入框
32 |
33 | JScrollPane scroll = new JScrollPane(area); // 创建一个滚动条
34 | //scroll.setViewportView(area); // 设置滚动条关联的控件
35 | panel.add(scroll); // 在面板上添加滚动条
36 |
37 | frame.add(panel); // 在窗口上添加面板
38 |
39 | frame.setVisible(true); // 必须设置为true,否则看不见
40 | }
41 |
42 | }
43 |
--------------------------------------------------------------------------------
/chapter11/src/com/io/bio/TestFileStream.java:
--------------------------------------------------------------------------------
1 | package com.io.bio;
2 |
3 | import java.io.FileInputStream;
4 | import java.io.FileOutputStream;
5 |
6 | //演示文件输入输出流的用法
7 | public class TestFileStream {
8 | private static String mFileName = "E:/test/aae.txt";
9 |
10 | public static void main(String[] arg) {
11 | writeFile(); // 利用文件输出流写入文件
12 | readFile(); // 利用文件输入流读取文件
13 | }
14 |
15 | // 利用文件输出流写入文件。注意FileOutputStream处理的是字节信息
16 | private static void writeFile() {
17 | String str = "白日依山尽,黄河入海流。\n欲穷千里目,更上一层楼。";
18 | // 根据指定路径构建文件输出流对象
19 | try (FileOutputStream fos = new FileOutputStream(mFileName)) {
20 | fos.write(str.getBytes()); // 把字节数组写入文件输出流
21 | // 在try(...)里面创建的IO流,程序用完会自动关闭,所以下面的close方法不必显式调用
22 | //fos.close(); // 关闭文件输出流
23 | } catch (Exception e) {
24 | e.printStackTrace();
25 | }
26 | }
27 |
28 | // 利用文件输入流读取文件
29 | private static void readFile() {
30 | // 根据指定路径构建文件输入流对象
31 | try (FileInputStream fis = new FileInputStream(mFileName)) {
32 | // 分配长度为文件大小的字节数组。available方法返回当前位置后面的剩余部分大小
33 | byte[] bytes = new byte[fis.available()];
34 | //fis.skip(3); // 字节流的skip方法跳过的是字节数
35 | fis.read(bytes); // 从文件输入流中读取字节数组
36 | String content = new String(bytes); // 把字节数组转换为字符串
37 | System.out.println("content="+content);
38 | // 在try(...)里面创建的的IO流,程序用完会自动关闭,所以下面的close方法不必显式调用
39 | //fis.close(); // 关闭文件输入流
40 | } catch (Exception e) {
41 | e.printStackTrace();
42 | }
43 | }
44 |
45 | }
46 |
--------------------------------------------------------------------------------
/chapter16/src/com/network/im_client/chat.fxml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/chapter03/src/com/control/logic/ShortCircuit.java:
--------------------------------------------------------------------------------
1 | package com.control.logic;
2 |
3 | //演示按位逻辑与短路逻辑之间的区别
4 | public class ShortCircuit {
5 |
6 | public static void main(String[] args) {
7 | // 3的二进制为00000011,7的二进制为00000111
8 | // int andNumber = 3&7; // 对两个数字开展“按位与”运算
9 | // System.out.println("andNumber="+andNumber);
10 | // int orNumber = 3|7; // 对两个数字开展“按位或”运算
11 | // System.out.println("orNumber="+orNumber);
12 | // int xorNumber = 3^7; // 对两个数字开展“按位异或”运算
13 | // System.out.println("xorNumber="+xorNumber);
14 |
15 | // 3的二进制为00000011,7的二进制为00000111
16 | int andNumber = 1 + 2 & 3 + 4; // 等价于“(1+2)&(3+4)”
17 | System.out.println("andNumber=" + andNumber);
18 | int orNumber = 1 + 2 | 3 + 4; // 等价于“(1+2)|(3+4)”
19 | System.out.println("orNumber=" + orNumber);
20 | int xorNumber = 1 + 2 ^ 3 + 4; // 等价于“(1+2)^(3+4)”
21 | System.out.println("xorNumber=" + xorNumber);
22 |
23 | // 下面写法是错误的,因为“与”、“或”、“异或”的优先级低于关系运算符
24 | // boolean logicalResult = 1&2 > 3|4;
25 | // 下面写法是正确的,使用圆括号把需要先计算的语句括起来
26 | boolean logicalResult = (1 & 2) > (3 | 4);
27 | System.out.println("logicalResult=" + logicalResult);
28 | int i = 1, j = 1;
29 | // 对于按位逻辑运算,需要等待左右两边都计算完毕,然后进行按位逻辑判断
30 | boolean result1 = 3 > 4 & ++i < 5;
31 | System.out.println("result1=" + result1 + ", i=" + i);
32 | // 对于短路逻辑运算,一旦左边的计算能够确定结果,就立即返回判断结果,不再进行右边的计算
33 | boolean result2 = 3 > 4 && ++j < 5;
34 | System.out.println("result2=" + result2 + ", j=" + j);
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/chapter10/src/com/addition/annotation/Apple.java:
--------------------------------------------------------------------------------
1 | package com.addition.annotation;
2 |
3 | //定义一个苹果类
4 | public class Apple {
5 | @NotNull // 通过注解声明该字段不可为空
6 | private String name; // 名称
7 | @NotNull // 通过注解声明该字段不可为空
8 | private String color; // 颜色
9 | @NotNull // 通过注解声明该字段不可为空
10 | private Double weight; // 重量
11 | @NotNull // 通过注解声明该字段不可为空
12 | private Double price; // 价格
13 |
14 | public Apple(String name, String color, Double weight, Double price) {
15 | this.name = name;
16 | this.color = color;
17 | this.weight = weight;
18 | this.price = price;
19 | }
20 |
21 | public void setName(String name) {
22 | this.name = name;
23 | }
24 |
25 | public String getName() {
26 | return this.name;
27 | }
28 |
29 | public void setColor(String color) {
30 | this.color = color;
31 | }
32 |
33 | public String getColor() {
34 | return this.color;
35 | }
36 |
37 | public void setWeight(Double weight) {
38 | this.weight = weight;
39 | }
40 |
41 | public Double getWeight() {
42 | return this.weight;
43 | }
44 |
45 | public void setPrice(Double price) {
46 | this.price = price;
47 | }
48 |
49 | public Double getPrice() {
50 | return this.price;
51 | }
52 |
53 | // 获取该苹果的详细描述文字
54 | public String toString() {
55 | return String.format("\n(name=%s,color=%s,weight=%f,price=%f)", name,
56 | color, weight, price);
57 | }
58 |
59 | // 判断是否红苹果
60 | public boolean isRedApple() {
61 | return this.color.toLowerCase().equals("red");
62 | }
63 |
64 | }
65 |
--------------------------------------------------------------------------------
/chapter16/src/com/network/im_client/qq_login.fxml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/chapter03/src/com/control/logic/Bool.java:
--------------------------------------------------------------------------------
1 | package com.control.logic;
2 |
3 | //演示逻辑运算符的用法
4 | public class Bool {
5 |
6 | public static void main(String[] args) {
7 | // boolean表示布尔类型,该类型的变量只允许两个取值,即true和false
8 | boolean zhen = true; // true表示为真
9 | System.out.println("zhen=" + zhen);
10 | boolean jia = false; // false表示为假
11 | System.out.println("jia=" + jia);
12 | // “!”加在布尔变量前面表示开展“非”运算。若原变量值为true则运算结果为false,若原变量值为false则运算结果为true
13 | boolean not = !zhen;
14 | System.out.println("not=" + not);
15 | // “&”放在两个布尔变量之间表示开展“与”运算。只有两个变量都为true时,运算结果才为true,其余情况的运算结果都为false
16 | boolean and = zhen & jia;
17 | System.out.println("and=" + and);
18 | // “|”放在两个布尔变量之间表示开展“或”运算。只要两个变量有一个为true,运算结果就为true。只有两个变量都为false时,运算结果才为false
19 | boolean or = zhen | jia;
20 | System.out.println("or=" + or);
21 | // “^”放在两个布尔变量之间表示开展“异或”运算。当两个变量同为true或者同为false时,运算结果为false。当两个变量一个为true另一个为false时,运算结果为true
22 | boolean xor = zhen ^ jia;
23 | System.out.println("xor=" + xor);
24 | boolean value = true; // 为布尔变量赋初始值
25 | System.out.println("value=" + value);
26 | // 对布尔变量做“与”运算,且运算结果仍旧保存在该变量中,则可使用运算符“&=”
27 | value &= false; // 该行代码等同于 value = value&false;
28 | System.out.println("value=" + value);
29 | // 对布尔变量做“或”运算,且运算结果仍旧保存在该变量中,则可使用运算符“|=”
30 | value |= true; // 该行代码等同于 value = value|true;
31 | System.out.println("value=" + value);
32 | // 对布尔变量做“异或”运算,且运算结果仍旧保存在该变量中,则可使用运算符“^=”
33 | value ^= false; // 该行代码等同于 value = value^false;
34 | System.out.println("value=" + value);
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/chapter08/src/com/special/singleton/TestSingleton.java:
--------------------------------------------------------------------------------
1 | package com.special.singleton;
2 |
3 | //演示几种单例模式的用法
4 | public class TestSingleton {
5 |
6 | public static void main(String[] args) {
7 | testNotSingleton(); // 测试未采取单例模式的普通类
8 | testLazySingleton(); // 测试采取了懒汉方式的单例类
9 | testHungrySingleton(); // 测试采取了饿汉方式的单例类
10 | testNestSingleton(); // 测试采取了嵌套类方式的单例类
11 | }
12 |
13 | // 测试未采取单例模式的普通类
14 | private static void testNotSingleton() {
15 | Plain plain;
16 | for (int i = 0; i < 3; i++) { // 依次创建普通类的三个实例
17 | plain = new Plain();
18 | System.out.println("i="+i+", 普通类的实例创建次数=" + plain.getCount());
19 | }
20 | }
21 |
22 | // 测试采取了懒汉方式的单例类
23 | private static void testLazySingleton() {
24 | SingletonLazy lazy;
25 | for (int i = 0; i < 3; i++) { // 先后获取三次的单例实例
26 | lazy = SingletonLazy.getInstance();
27 | System.out.println("i="+i+", 懒汉方式单例类的实例创建次数=" + lazy.getCount());
28 | }
29 | }
30 |
31 | // 测试采取了饿汉方式的单例类
32 | private static void testHungrySingleton() {
33 | SingletonHungry hungry;
34 | for (int i = 0; i < 3; i++) { // 先后获取三次的单例实例
35 | hungry = SingletonHungry.getInstance();
36 | System.out.println("i="+i+", 饿汉方式单例类的实例创建次数=" + hungry.getCount());
37 | }
38 | }
39 |
40 | // 测试采取了嵌套方式的单例类
41 | private static void testNestSingleton() {
42 | SingletonNest inner;
43 | for (int i = 0; i < 3; i++) { // 先后获取三次的单例实例
44 | inner = SingletonNest.getInstance();
45 | System.out.println("i="+i+", 嵌套方式单例类的实例创建次数=" + inner.getCount());
46 | }
47 | }
48 |
49 | }
50 |
--------------------------------------------------------------------------------