概念部分
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());
}
}