概念部分
1 包
本质上是一个文件夹
用
package定义,打包命令:javac
-d
.类名
.java
导入包用
import 包
.类
通配符
*常用来表示所需类
系统常用包:
java
.lang
:常用基础类包
javalang
.reflect
:反射编程包
java
.net
:网络编程开发包
java
.sql
:数据库开发支持包
java
.util
:工具程序包(集合类等)
java
.io
:IO编程开发包
2 权限
private<default<protected<public
权限选择:
关于封装的描述
90%使用
private,只有
10%会用
protected
属性都用
private,方法都用
public
3 jar命令
对文件进行打包:
jar
-c:创建新档案;
-f:指定档案文件名;
-v:在标准输出中生成详细输出
使用jar文件必须配置在classpath中
4 单例设计模式
单例设计:指一个类只允许产生一个实例化对象
饿汉式:不管你是否使用Singleton类的对象,只要该类加载,就一定会自动创建好一个公共的instance对象。
特点:构造方法私有化,外部无法产生新的实例化对象,只能通过
static方法取得实例化对象。
懒汉式:当第一次使用Singleton对象时,才会为其产生实例化对象的操作。
特点:存在多线程安全问题。
面试:如何解决懒汉式的线程安全问题?(双重加速单例模式)
5 多例设计模式
多例比单例追加了更多个内部实例化对象产生。
多例与单例的共同特点:
构造方法私有化;类内部一定会提供一个
static方法用于取得实例化对象。
6 异常体系
Throwable
-Error Java运行时内部或资源耗尽错误
-Exception
-IOException I
/O错误导致
-RuntimeException 程序错误导致
规定:派生于Error类或RuntimeException类的称为非受查异常,其他的称为受查异常。
7 异常影响
异常是导致程序中断执行的一种指令流。
8 异常处理
try{
可能出现异常的语句;
}catch(异常类 对象
){
e
.printStackTrace();
}finally{
异常的出口
}
注意:无论是否产生异常,
finally中的程序都要执行!
9 throws关键字
进行方法定义时用
throws声明调用此方法可能产生哪些异常。
如果该方法出现问题后不希望处理,就用
throws抛出。
10 throw关键字
throw产生异常类对象;
一般
throw和
break、
continue、
break都要和
if结合用。
11 throw和
throws区别:
throw用于方法内部,主要表示手工异常抛出;
throws在方法声明上用,明确告诉用户可能产生的异常,且可能不处理。
12 RuntimeException类
在异常体系设计时,考虑到有些是简单问题,这类称RuntimeException
,使用它定义的异常类可以不强制性进行异常处理。
面试:解释Exception与RUNtimeException的区别,举例。
(
1)前者是后者的父类,使用前者定义的异常都必须进行处理,而后者可由用户选择性的进行异常处理;
(
2)常见的RUNtimeException:ClassCastException、NullPointerException
13 自定义异常类
根据要求的异常信息,继承Exception类来完成。
14 链表
保存多个任意对象,一般想到的是Object型的数组;但数组是定长的线型结构。
为了不空间浪费,可定义一个非固定长度的数组,有多少数据就存多少,用火车车厢的设计模式,动态进行车厢挂载。
Java链表实现
class Node{
private Object data
;
private Node next
;
public Node(Object data
){
this.data
=data
;
}
public void setData(Object data
){
this.data
=data
;
}
public Object
getData(){
return this.data
;
}
public Node
getNext() {
return next
;
}
public void setNext(Node next
) {
this.next
= next
;
}
}
public class Test{
public static void main(String
[] args
) throws Exception
{
Node root
= new Node("火车头");
Node n1
= new Node("车厢1");
Node n2
= new Node("车厢2");
Node n3
= new Node("车厢3");
root
.setNext(n1
);
n1
.setNext(n2
);
n2
.setNext(n3
);
getNodeData(root
);
}
public static void getNodeData(Node node
){
if (node
!=null
){
System
.out
.println(node
.getData());
getNodeData(node
.getNext());
}
}
}
interface Link{
void add(Object obj
);
boolean remove(int index
);
boolean set(int index
,Object obj
);
Object
get(int index
);
int contains(Object obj
);
void clear();
void printLink();
int length();
Object
[] toArray();
}
class LinkImpl implements Link{
private Node first
;
private Node last
;
private int size
= 0;
private class Node{
private Node prev
;
private Object data
;
private Node next
;
public Node(Node prev
,Object data
,Node next
){
super();
this.prev
= prev
;
this.data
= data
;
this.next
= next
;
}
}
@Override
public void add(Object obj
) {
Node temp
= this.last
;
Node newNode
= new Node(temp
,obj
,null
);
this.last
= newNode
;
if (this.first
==null
){
this.first
= newNode
;
}else {
temp
.next
= newNode
;
}
this.size
++;
}
@Override
public boolean remove(int index
) {
if (!isLinkElement(index
)){
return false;
}
Node node
= node(index
);
if (node
== this.first
){
if (node
==this.last
){
node
= null
;
this.size
--;
return true;
}else {
Node temp
= this.first
;
this.first
= node
.next
;
temp
.next
= null
;
this.first
.prev
= null
;
this.size
--;
return true;
}
}else if(node
== this.last
){
Node temp
= this.last
;
this.last
= node
.prev
;
temp
.prev
= null
;
this.last
.prev
= null
;
this.size
--;
return true;
}
node
.next
.prev
= node
.prev
;
node
.prev
.next
= node
.next
;
node
.prev
= node
.next
=null
;
this.size
--;
return true;
}
@Override
public boolean set(int index
, Object obj
) {
if (!isLinkElement(index
)){
return false;
}
Node node
= node(index
);
node
.data
= obj
;
return true;
}
@Override
public Object
get(int index
) {
if (!isLinkElement(index
)){
return null
;
}
return node(index
).data
;
}
@Override
public int contains(Object obj
) {
if (obj
== null
) {
int index
= 0;
for (Node x
= this.first
; x
!= null
; x
= x
.next
) {
if (x
.data
== null
) {
return index
;
}
index
++;
}
return -1;
}else {
int index
= 0;
for (Node x
= this.first
; x
!= null
; x
= x
.next
) {
if (obj
.equals(x
.data
)) {
return index
;
}
index
++;
}
return -1;
}
}
@Override
public void clear() {
for(Node x
= this.first
; x
!= null
; ) {
Node temp
= x
.next
;
x
.prev
= x
.next
= null
;
x
= temp
;
}
this.first
= this.last
= null
;
this.size
= 0;
}
@Override
public void printLink() {
Object
[] result
= this.toArray();
for (Object object
: result
) {
System
.out
.println(object
);
}
}
@Override
public int length() {
return this.size
;
}
@Override
public Object
[] toArray() {
Object
[] result
= new Object[size
];
int i
=0;
for (Node temp
= first
;temp
!=null
;temp
= temp
.next
){
result
[i
++] = temp
.data
;
}
return result
;
}
private Node
node(int index
) {
if (index
< (size
>> 1)) {
Node result
= this.first
;
for(int i
= 0 ; i
< index
; i
++) {
result
= result
.next
;
}
return result
;
}
Node result
= this.last
;
for(int i
= size
-1 ; i
> index
; i
--) {
result
= result
.prev
;
}
return result
;
}
private boolean isLinkElement(int index
) {
return index
>=0 && index
<size
;
}
}
class Factory{
private Factory(){}
public static Link
getLinkInstance(){
return new LinkImpl();
}
}
public class Test{
public static void main(String
[] args
) {
Link link
= Factory
.getLinkInstance();
link
.add("火车头");
link
.add("车厢1");
link
.add("车厢2");
System
.out
.println(link
.contains("车厢1"));
System
.out
.println(link
.contains("test"));
System
.out
.println(link
.length());
}
}