【Core Java】读书笔记 Part 2

本文总结的是书中的:第 5 章 继承 前几章的总结在:ZYF.IM-【Core Java】读书笔记 Part1 5 继承 5.1 类、超类和子类 1、有些人认为 super 与 this 引用是类似的概念,实际上,这样比较并不太恰当。这是因为 super 不是一个对象的引用,不能将 super 赋予另一个对象变量,它只是一个指示编译器调用超类方法的特殊关键字。 2、使用 super 或 this 调用构造器的语句必须时子类构造器的第一条语句。也就是说 super 和 this 不能同时出现在一个构造器中。 3、this 关键字。 引用隐式参数 调用该类其他的构造器 4、super 关键字 调用超类的方法 调用超类的构造器 5、调用构造器的语句只能作为另一个构造器的第一条语句出现。 6、一个对象变量可以指示多种实际类型的现象被称为多态。在运行时能够自动的选择则调用哪个方法的现象称为动态绑定。 7、Manager[] managers = new Manager[10]; 将它转换成 Employee[] 数组是完全合法的: Employee[] staff = managers;// OK 毕竟如果manager[i]是一个 Manager,也一定是一个 Employee,然而,实际上,将会发生一些令人惊讶的事情。要切记 managers 和 staff 引用的时同一个数组。现在请看: staff[0] = new Employee("Harry Hacker", ..); 编译器竟然接纳了这个赋值操作。staff[0]与manager[0]引用的是同一个对象,似乎我们把一个普通雇员擅自归入了经理行列中了。这时一个种很忌讳发生的情形,当调用managers[0].setBonus(1000)的时候,将会导致调用一个不存在的实例域,今儿搅乱相邻存储空间的内容。 为了确保不发生这类错误,所有数组都要牢记创建它们的元素类型,并负责监督仅将类型兼容的引用存储到数组中。 ...

September 23, 2016 · 2 min · 284 words · Me

LeetCode Shell 解题集合

LeetCode Shell 的试题多为文本操作,195. Tenth Line、193. Valid Phone Numbers、192. Word Frequency、194. Transpose File 暂时只有 4 道题,就整合在这一起了 Shell 中文本处理的事情基本 awk sed grep sort uniq tail head 几个命令组合组合就搞定了 195. Tenth Line 大体意思 How would you print just the 10th line of a file? For example, assume that file.txt has the following content: Line 1 Line 2 Line 3 Line 4 Line 5 Line 6 Line 7 Line 8 Line 9 Line 10 Your script should output the tenth line, which is: ...

September 19, 2016 · 3 min · 574 words · Me

Ubuntu 下使用 sysv-rc-conf 管理开机启动服务

sysv-rc-conf gives an easy to use interface for managing “/etc/rc{runlevel}.d/” symlinks. The interface comes in two different flavors, one that simply allows turning services on or off and another that allows for more fine tuned management of the symlinks. 安装 sysv-rc-conf sudo apt-get install sysv-rc-conf 使用 sysv-rc-conf sudo sysv-rc-conf 操作界面十分简洁,你可以用鼠标点击,也可以用键盘方向键定位,用空格键选择,用 Ctrl+n 翻下一页,用 Ctrl+p 翻上一页,用 q 退出。 相关知识 Ubuntu 运行级别 Linux 系统任何时候都运行在一个指定的运行级上,并且不同的运行级的程序和服务都不同,所要完成的工作和要达到的目的都不同,系统可以在这些运行级之间进行切换,以完成不同的工作。 Ubuntu 的系统运行级别: 0 停机 1 单用户,Does not configure network interfaces, start daemons, or allow non-root logins 2 多用户,无网络连接 Does not configure network interfaces or start daemons 3 多用户,启动网络连接 Starts the system normally. 4 用户自定义 5 多用户带图形界面 6 重启 查看当前运行级别,执行命令: ...

September 19, 2016 · 2 min · 381 words · Me

Eclipse Java 注释模板

自己总结的比较规范的 Eclipse Java 注释模板 Eclipse Java 注释模板设置 Window -> Preference -> Java -> CodeStyle -> Code Template 然后展开 Comments 节点就是所有需设置注释的元素 各项注释模板 Files /** * Copyright © ${year}. All rights reserved. * * @Title: ${file_name} * @Prject: ${project_name} * @Package: ${package_name} * @Description: ${todo} * @author: ${user} * @date: ${date} ${time} */ Types /** * * * @ClassName: ${type_name} * @Description: ${todo} * @author: ${user} * @date: ${date} ${time} * ${tags} */ Fields /** * @fieldName: ${field} * @fieldType: ${field_type} * @Description: ${todo} */ Constructors /** * @Title:${enclosing_type} * @Description:${todo} * ${tags} */ Methods /** * * * @Title: ${enclosing_method} * @Description: ${todo} * @author: ${user} * ${tags} * @return: ${return_type} */ Overriding methods /* (no Javadoc) * <p>Title: ${enclosing_method}</p> * <p>Description: </p> * ${tags} * ${see_to_overridden} */ 定制 ${user} 显示内容 找到你的 Eclipse 安装路径,打开 eclipse.ini 文件,在 -vmargs 下面添加 ...

September 18, 2016 · 1 min · 142 words · Me

MySQL LIMIT 查询优化

最近常在 SQL 中使用到 LIMIT ? ?,在执行 LIMIT 0, 1000 与 LIMIT 100000, 1000 时,查询速度明显有很大的区别,而且随着 LIMIT 的偏移量的增加,查询速度越来越慢。是否有办法对 SQL 中 LIMIT 查询进行优化呢? LIMIT 速度慢的原因 LIMIT 100000, 1000 的意思扫描满足条件的 101000 行,扔掉前面的 100000 行,返回最后的 1000 行,问题就在这里。 LIMIT 优化思路 1、尽可能从索引中直接获取数据,避免或减少直接扫描行数据的频率 2、尽可能减少扫描的记录数,也就是先确定起始的范围,再往后取 N 条记录即可 LIMIT 优化示例 原始 SQL -- 含条件 SELECT * FROM `t1` WHERE ftype=1 ORDER BY id DESC LIMIT 100, 10; -- 不含 WHERE SELECT * FROM `t1` ORDER BY id DESC LIMIT 100, 10; 子查询优化 SQL -- 采用子查询的方式优化,在子查询里先从索引获取到最大 id,然后倒序排,再取 10 行结果集 -- 注意这里采用了 2 次倒序排,因此在取 LIMIT 的 start 值时,比原来的值加了 10,即 935510,否则结果将和原来的不一致 SELECT * FROM (SELECT * FROM `t1` WHERE id > ( SELECT id FROM `t1` WHERE ftype=1 ORDER BY id DESC LIMIT 935510, 1) LIMIT 10) T ORDER BY id DESC; INNER 优化 SQL -- 采用 INNER JOIN 优化,JOIN 子句里也优先从索引获取 ID 列表,然后直接关联查询获得最终结果,这里不需要加 10 SELECT * FROM `t1` INNER JOIN ( SELECT id FROM `t1` WHERE ftype=1 ORDER BY id DESC LIMIT 935500,10) t2 USING (id); 1、要学着使用 EXPLAIN 对 SQL 进行优化调整 2、推荐使用 INNER 优化 SQL 3、发现了一个 ubuntu 中监控 MySQL 的 mytop 命令,刚刚安装还没细研究 ...

September 18, 2016 · 1 min · 200 words · Me

Eclipse Maven 配置使用国内镜像库

Windows 下因为 Eclipse 自带了 Maven 插件,还算够用就懒得安装 Maven 了。在不使用代理的情况下,用 Maven 的下载库不是一般的慢。Eclipse Maven 的插件怎么配置国内的镜像库呢?其实很简单。 创建 settings.xml 文件 注意两个地方: 1、<localRepository>C:\Users\Lenovo\.m2\repository</localRepository> 本地仓库位置 2、镜像配置,选取的是 aliyun 的 <mirrors> <mirror> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror> </mirrors> 完整的 settings.xml 文件 <?xml version="1.0" encoding="UTF-8"?> <!-- | This is the configuration file for Maven. It can be specified at two levels: | | 1. User Level. This settings.xml file provides configuration for a single user, | and is normally provided in ${user.home}/.m2/settings.xml. | | NOTE: This location can be overridden with the CLI option: | | -s /path/to/user/settings.xml | | 2. Global Level. This settings.xml file provides configuration for all Maven | users on a machine (assuming they're all using the same Maven | installation). It's normally provided in | ${maven.home}/conf/settings.xml. | | NOTE: This location can be overridden with the CLI option: | | -gs /path/to/global/settings.xml | | |--> <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> <!-- Change in below line --> <localRepository>C:\Users\Lenovo\.m2\repository</localRepository> <interactiveMode>true</interactiveMode> <offline>false</offline> <pluginGroups> <!-- pluginGroup | Specifies a further group identifier to use for plugin lookup. <pluginGroup>com.your.plugins</pluginGroup> --> </pluginGroups> <proxies> <!-- <proxy> <id>optional</id> <active>true</active> <protocol>http</protocol> <username>proxyuser</username> <password>proxypass</password> <host>proxy.host.net</host> <port>80</port> <nonProxyHosts>local.net|some.host.com</nonProxyHosts> </proxy> --> </proxies> <servers> <!-- <server> <id>deploymentRepo</id> <username>crunchify</username> <password>crunchify</password> </server> --> </servers> <mirrors> <mirror> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror> </mirrors> <profiles> </profiles> </settings> 设置 Eclipse Maven Plugin Window -> Preferences -> Maven -> User Settings -> User settings Browse 选择上面创建的 setting.xml -> ok ...

September 14, 2016 · 2 min · 227 words · Me

LeetCode Database Trips and Users 262

262. Trips and Users The Trips table holds all taxi trips. Each trip has a unique Id, while Client_Id and Driver_Id are both foreign keys to the Users_Id at the Users table. Status is an ENUM type of (‘completed’, ‘cancelled_by_driver’, ‘cancelled_by_client’). +----+-----------+-----------+---------+--------------------+----------+ | Id | Client_Id | Driver_Id | City_Id | Status |Request_at| +----+-----------+-----------+---------+--------------------+----------+ | 1 | 1 | 10 | 1 | completed |2013-10-01| | 2 | 2 | 11 | 1 | cancelled_by_driver|2013-10-01| | 3 | 3 | 12 | 6 | completed |2013-10-01| | 4 | 4 | 13 | 6 | cancelled_by_client|2013-10-01| | 5 | 1 | 10 | 1 | completed |2013-10-02| | 6 | 2 | 11 | 6 | completed |2013-10-02| | 7 | 3 | 12 | 6 | completed |2013-10-02| | 8 | 2 | 12 | 12 | completed |2013-10-03| | 9 | 3 | 10 | 12 | completed |2013-10-03| | 10 | 4 | 13 | 12 | cancelled_by_driver|2013-10-03| +----+-----------+-----------+---------+--------------------+----------+ The Users table holds all users. Each user has an unique Users_Id, and Role is an ENUM type of (‘client’, ‘driver’, ‘partner’). ...

September 14, 2016 · 3 min · 436 words · Me

LeetCode Database Department Highest Salary 184

184. Department Highest Salary The Employee table holds all employees. Every employee has an Id, a salary, and there is also a column for the department Id. +----+-------+--------+--------------+ | Id | Name | Salary | DepartmentId | +----+-------+--------+--------------+ | 1 | Joe | 70000 | 1 | | 2 | Henry | 80000 | 2 | | 3 | Sam | 60000 | 2 | | 4 | Max | 90000 | 1 | +----+-------+--------+--------------+ The Department table holds all departments of the company. ...

September 12, 2016 · 2 min · 281 words · Me

LeetCode Database Consecutive Numbers 180

180. Consecutive Numbers Write a SQL query to find all numbers that appear at least three times consecutively. +----+-----+ | Id | Num | +----+-----+ | 1 | 1 | | 2 | 1 | | 3 | 1 | | 4 | 2 | | 5 | 1 | | 6 | 2 | | 7 | 2 | +----+-----+ For example, given the above Logs table, 1 is the only number that appears consecutively for at least three times. ...

September 11, 2016 · 1 min · 173 words · Me

LeetCode Database Second Highest Salary 176

176. Second Highest Salary Write a SQL query to get the second highest salary from the Employee table. +----+--------+ | Id | Salary | +----+--------+ | 1 | 100 | | 2 | 200 | | 3 | 300 | +----+--------+ For example, given the above Employee table, the second highest salary is 200. If there is no second highest salary, then the query should return null. 大体意思 写 SQL 查询出第二高薪水的 Id。如何没有第二高,则返回 null ...

September 11, 2016 · 2 min · 257 words · Me