二刷 Java 官方教程
第二次阅读 Java 官方教程,有些不一样的理解。内容比较散乱,个人意识流笔记。
第二次阅读 Java 官方教程,有些不一样的理解。内容比较散乱,个人意识流笔记。
本书作者:测试开发刚哥,文章版权归个人所有,未经授权请勿转载。 Processing Data in Memory The Stream API is probably the second most important feature added to Java SE 8, after the lambda expressions. In a nutshell, the Stream API is about providing an implementation of the well known map-filter-reduce algorithm to the JDK. map-filter-reduce: List<Sale> sales = ...; // this is the list of all the sales int amountSoldInMarch = 0; for (Sale sale: sales) { if (sale.getDate().getMonth() == Month.MARCH) { amountSoldInMarch += sale.getAmount(); } } System.out.println("Amount sold in March: " + amountSoldInMarch); map:通过 get 取值,将部分字段映射到新数据(select 字段) ...
本书作者:测试开发刚哥,文章版权归个人所有,未经授权请勿转载。 四种参数 Python 函数 func 定义如下: def func(first, *args, second="Hello World", **kwargs): print(first) print(args) print(second) print(kwargs) func("dongfanger", "san", py="good") 运行后会输出: dongfanger ('san',) Hello World {'py': 'good'} 它有四种参数: first 是定位参数,positional parameter,不可省略。 *args是可变参数,arguments,存入元组。 second 是默认值参数,default argument values,可以省略。 **args是关键字参数,keyword arguments,存入字典。 func 函数的调用方式有以下这些: ①传入单个定位参数。 func("dongfanger") dongfanger () Hello World {} ②第一个参数后的任意个参数会被*args捕获,存入一个元组。 func("dongfanger", "a", "b", "c") dongfanger ('a', 'b', 'c') Hello World {} ③没有明确指定名称的关键字参数会被**kwargs捕获,存入一个字典。 func("dongfanger", j="1", k="2") dongfanger () Hello World {'j': '1', 'k': '2'} ④second 只能作为关键字参数传入。 func("dongfanger", second="cool") dongfanger () cool {} ⑤定位函数也能作为关键字参数传入。 ...
本书作者:测试开发刚哥,文章版权归个人所有,未经授权请勿转载。 Storing Data The Collections Framework is the most widely used API of the JDK. 集合不是数据类型,它是 JDK 的 API,可以用来存储数据等,相当于数据结构。 the Collections Framework is a set of interfaces that models different way of storing data in different types of containers. Then the Framework provides at least one implementation for each interface. There are two main categories of interfaces in the Collections Framework: collections and maps.(所以我猜想 Python 也是基于这样的考虑,设计了 List 和 Dict,并作为 Python 基本数据类型,实际编码中要用到的也主要是这两类) ...
写 Python 越多,写函数越多,于是乎有人觉得 Python 是函数式语言,其实不然,Python 只是从函数式语言中借鉴了一些好的想法而已。
本书作者:测试开发刚哥,文章版权归个人所有,未经授权请勿转载。 Exception Definition: An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program’s instructions. the checked exception 比如,java.io.FileNotFoundException the error 比如,java.io.IOError the runtime exception 比如,NullPointerException error 和 runtime exception 又叫做 unchecked exception Catching and Handling Exceptions try { code } catch and finally blocks . . . try { } catch (ExceptionType name) { } catch (ExceptionType name) { } 使用|catch 多个: ...
本书作者:测试开发刚哥,文章版权归个人所有,未经授权请勿转载。 集合 集合的字面量是{1}、{1, 2},和字典有点像,不同的是集合只有值没有键。 set 构造方法 set 构造方法如下: my_set = {1, 2, 3} my_set = set([1, 2, 3]) 前者比后者更快更易读,因为对于前者,Python 会利用一个专门的叫做 BUILD_SET 的字节码来创建集合。而对于后者,Python 必须先从 set 这个名字来查询构造方法,然后新建一个列表,最后再把这个列表传入到构造方法里。 {}是空字典,空集合必须用set()。 集合也有集合推导: >>> my_set = {x for x in range(1, 4)} >>> my_set {1, 2, 3} >>> type(my_set) <class 'set'> 集合还有个不可变类型叫做 frozenset,如同元组之于列表,它的构造函数如下: >>> frozenset(range(10)) frozenset({0, 1, 2, 3, 4, 5, 6, 7, 8, 9}) 只有这一种方法,没有字面量。 集合的操作 集合的本质是许多唯一对象的聚集,它最常见的操作是用来去重: >>> my_list = [1, 2, 2, 3] >>> set(my_list) {1, 2, 3} >>> list(set(my_list)) [1, 2, 3] 除此之外,集合还能进行数学运算,比如求交集、并集、差集等。示例,求 needles 的元素在 haystack 里出现的次数,暴力解法: found = 0 for n in needles: if n in haystack: found += 1 集合解法: found = len(needles & haystack) 即使不是 set 也能先转换为 set: found = len(set(needles) & set(haystack)) ## 另一种写法 found = len(set(needles).intersection(haystack)) 集合支持的操作如下: ...
本书作者:测试开发刚哥,文章版权归个人所有,未经授权请勿转载。 MyBatis 是什么 MyBatis 是 Java 的持久层框架,GitHub 的 star 数高达 15.8k,是 Java 技术栈中最热门的 ORM 框架之一。它支持自定义 SQL、存储过程以及高级映射,可以通过 XML 或注解来配置和映射原始类型、接口和 Java POJOs 为数据库中的记录。 POJOs,Plain Old Java Objects,纯的传统意义的 Java 对象,最基本的 Java Bean 只有属性加上属性的 get 和 set 方法。 安装 Maven pom.xml: <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>x.x.x</version> </dependency> 版本查询:https://mvnrepository.com/artifact/org.mybatis/mybatis SqlSessionFactory SqlSessionFactory 的实例是每个 MyBatis 应用的核心,通过 SqlSessionFactoryBuilder 创建,能基于 XML 配置,也能使用 Configuration 类。 基于 XML 配置: String resource = "org/mybatis/example/mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 实际项目中不会手动设置 XML 文件路径,而是直接读取 classpath 去找 XML。 ...
本书作者:测试开发刚哥,文章版权归个人所有,未经授权请勿转载。 Packages Definition: A package is a grouping of related types providing access protection and name space management. Note that types refers to classes, interfaces, enumerations, and annotation types. Enumerations and annotation types are special kinds of classes and interfaces, respectively, so types are often referred to in this section simply as classes and interfaces. 创建 //in the Draggable.java file package graphics; public interface Draggable { . . . } //in the Graphic.java file package graphics; public abstract class Graphic { . . . } //in the Circle.java file package graphics; public class Circle extends Graphic implements Draggable { . . . } //in the Rectangle.java file package graphics; public class Rectangle extends Graphic implements Draggable { . . . } //in the Point.java file package graphics; public class Point extends Graphic implements Draggable { . . . } //in the Line.java file package graphics; public class Line extends Graphic implements Draggable { . . . } 文件顶部第一行,每个文件只能有 1 个package关键字。 ...
“字典这个数据结构活跃在所有 Python 程序的背后,即便你的源码里并没有直接用到它”,摘抄自《代码之美》第 18 章 Python 的字典类:如何打造全能战士。字典是 Python 语言的基石!在函数的关键字参数、实例的属性和模块的命名空间都能够看到它的身影,我们自己写代码时也经常会用到。