1. 要有继承
2.父类对象引用子类对象
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());
}
}
局部内部类是指内部类定义在方法或作用于内
局部内部类会跟着其他类一起通过编译,但是在定义该局部内部类的方法或作用域之外,该局部内部类是不可用的
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:有序的散列表,通过红黑树实现的,一般用于单线程中存储有序的映射

原文:https://www.cnblogs.com/mcq1999/p/12083193.html