1. 要有繼承html
2.父類對象引用子類對象java
3. 要有方法的重寫數組
abstract void f();
包含抽象方法的類叫作抽象類;若是一個類包含一個或多個抽象方法,該類必須被定義爲抽象的interface這個關鍵字產生一個徹底抽象的類,它根本就沒有提供任何具體實現安全
要讓一個類遵循某個特定接口(或一組接口),須要使用implements關鍵字多線程
static
和 final
的。interface
前能夠添加public
修飾符,不加默認是包訪問權限,接口的方法默認都是public
的。public class DotThis { void f() { System.out.println("DotThis.f()"); } public class Inner { public DotThis outer() { return DotThis.this;//經過.this返回外部類對象 } } public Inner inner() {return new Inner();} public static void main(String[] args) { DotThis dotThis = new DotThis(); DotThis.Inner dtInner = dotThis.inner(); dtInner.outer().f(); } }
若是要建立內部類對象,必須使用外部類對象和.new框架
public class DotNew { public class Inner {} public static void main(String[] args) { DotNew dn = new DotNew(); DotNew.Inner dni = dn.new Inner(); } }
public class Example1 { public String getName() { return "llb"; } } public class Example2 { public int getAge() { return 25; } } public class MultiImplementation { private class test1 extends Example1 { public String getName() { return super.getName(); } } private class test2 extends Example2 { public int getAge() { return super.getAge(); } } public String getName() { return new test1.getName(); } public int getAge() { return new test2.getAge(); } public static void main(String[] args) { MultiImplementation my = new MultiImplementation(); System.out.println("姓名: " + my.getName()); System.out.println("年齡: " + my.getAge()); } }
局部內部類是指內部類定義在方法或做用於內ide
局部內部類會跟着其餘類一塊兒經過編譯,可是在定義該局部內部類的方法或做用域以外,該局部內部類是不可用的函數
static
時,再也不包含外圍對象的引用.this
,稱爲嵌套類(與C++嵌套類大體類似,只不過在C++中那些類不能訪問私有成員,而在Java中能夠訪問)。public class OuterClass { private static String address = "Shanghai"; public static class StaticInnerClass { public String getAddress() { return address; } } public static void main(String[] args) { OuterClass.StaticInnerClass sic = new OuterClass.StaticInnerClass(); String address = sic.getAddress(); System.out.println(address); } }
匿名內部類建立方式爲:工具
new 外部類構造器(參數列表)或接口 {}
public interface Contents { int value(); } public class Parcel7 { public Contents contents() { return new Contents() { private int i = 1; public int value() { return i; } }; } //等價於 /* class MyContents implements Contents { private int i = 1; public int value() { return i; } } public Contents contents() { return new MyContents(); } */ public static void main(String[] args) { Parcel7 parcel7 = new Parcel7(); Contents c = parcel7.contents(); } }
給匿名內部類傳遞參數時,若該形參在內部類被使用,那麼該形參必須被聲明爲final測試
public class Parcel9 { //dest是一個在外部定義的對象,必須將其定義爲final參數引用 public Destination destination(final String dest) { return new Destination() { private String label = dest; @Override public String readLabel() { return label; } }; } public static void main(String[] args) { Parcel9 parcel9 = new Parcel9(); Destination destination = parcel9.destination("Shanghai"); System.out.println(destination.readLabel()); } }
每一個類都會產生一個.class文件,其中包含了如何建立該類型的對象的所有信息;內部類也必須生成有個.class文件以包含它們的class對象信息,其命名規則是:
外圍類的名字,加上」$「,再加上內部類的名字,若是時匿名內部類,編譯器會簡單地產生一個數字做爲其標識符,例如:
Outer$Inner.class Outer$1.class
List,Set,Queue接口都是Collection接口的實現
import java.util.*; public class AddingGroups { public static void main(String[] args) { Collection<Integer> collection = new ArrayList<Integer>(Arrays.asList(1, 2, 3, 4, 5)); Integer[] moreInts = {6, 7, 8, 9, 10}; collection.addAll(Arrays.asList(moreInts));//更快,但不夠靈活 Collections.addAll(collection, 11, 12, 13, 14, 15); Collections.addAll(collection, moreInts);//更加靈活 List<Integer> list = Arrays.asList(16, 17, 18, 19, 20); list.set(1, 99); } }
Arrays相似於Collections,是一個工具類
Arrays.asList()返回一個受指定數組支持的固定大小的列表,能夠用來將數組轉換成List
反過來,利用List的toArray()方法,能夠將List轉換成數組
容器的打印
必須使用Arrays.toString()來產生數組的可打印表示
List
ArrayList:隨機訪問,可是在List的中間插入或移除元素時較慢
LinkedList:經過代價較低的在List中間進行的插入和刪除操做,提供了優化的順序訪問;隨機訪問較慢
ArrayList常見方法
contains(Object o):肯定某個對象是否在列表中
remove(int index):移除指定位置上的元素
indexOf(Object o):返回列表中首次出現指定元素的索引,若是不包含該元素,返回-1
add(E e):將制定元素添加到此列表的尾部
add(int index, E e):將指定元素插入到指定位置
迭代器是一個對象,它的工做是遍歷並選擇序列中的對象
Java中的Iterator只能單向移動,只能用來:
LinkedList常見方法
addFirst(E e)/addLast(E e):將元素添加到列表的開頭/結尾
getFirst()/element():返回列表的第一個元素
peek()/peekFirst():獲取但不移除列表的第一個元素
offer(E e)/offerLast(E e):將元素插入到列表末尾
Queue
隊列時一個典型的先進先出(FIFO)的容器,即從容器的一端放入事物,從另外一端取出,而且事物放入容器的順序與取出的順序是同樣的
LinkedList提供了方法以支持隊列的行爲,而且它實現了Queue接口,所以LinkedList能夠用做Queue的一種實現,也能夠將LinkedList向上轉型爲Queue
Set
Set不保存重複的元素;Set最常被使用的是測試歸屬性,咱們能夠很容易地詢問某個對象是否在某個Set中
存儲元素的方式:
HashSet:使用散列函數
LinkedHashSet:使用散列,可是看起來使用了鏈表來維護元素的插入順序
TreeSet:將元素存儲在紅-黑樹結構中
Map:一組成對的「鍵值對」對象,容許使用鍵來查找值;映射表容許咱們使用另外一個對象來查找某個對象,它被稱爲「關聯數組」,由於它將某些對象與另一些對象關聯在了一塊兒,或者被稱爲「字典」
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
更復雜的形式
Map<Integer, List<String>> map = new HashMap<Integer, List<String>>(); map.put(1, rrays.asList("lv", "long", "bao"));
map的鍵是一個Set,值是一個Collection
Map常見方法
get(Object o):返回指定鍵所映射的值,若是不包含該鍵的映射關係,返回null
put(K key, V value):將指定的值與此映射中的指定鍵關聯,若是已經存在映射關係,更新值
hashCode():返回此映射的哈希碼值
Map的三種實現
HashMap:基於「拉鍊法」實現的散列表,通常用於單線程中,不是線程安全的
HashTable:基於「拉鍊法」實現的散列表,通常用於多線程中,是線程安全的
TreeMap:有序的散列表,經過紅黑樹實現的,通常用於單線程中存儲有序的映射