Java要点和《疯狂Java讲义》例程整理(五)

2014-11-24 03:26:37 · 作者: · 浏览: 2
)){ it.remove(); } str = "test"; } System.out.println(strList); } }
a
b
c
d
[a, b, d]

泛型声明

P296
public class Test {
	public static void main(String[] args){
		Apple
  
    a1 = new Apple
   
    ("apple"); System.out.println(a1.getInfo()); Apple
    
      a2 = new Apple
     
      (3.14); System.out.println(a2.getInfo()); } } class Apple
      
        { private T info; public Apple(){} public Apple(T info) { this.info = info; } public void setInfo(T info) { this.info = info; } public T getInfo() { return this.info; } }
      
     
    
   
  

apple
3.14

从泛型类派生子类

P297
public class Test {
	public static void main(String[] args){
		A1 a1 = new A1();
		a1.setInfo("hello");
		System.out.println(a1.getInfo());
		A2 a2 = new A2();
		a2.setInfo(123);
		System.out.println(a2.getInfo());
	}
}

class Apple
  
   
{
	private T info;

	public Apple(){}
	
	public Apple(T info)
	{
		this.info = info;
	}
	
	public void setInfo(T info)
	{
		this.info = info;
	}
	
	public T getInfo()
	{
		return this.info;
	}
}

class A1 extends Apple
   
     { public String getInfo() { return "SubClass" + super.getInfo(); } } class A2 extends Apple { public String getInfo() { return super.getInfo().toString(); } }
   
  

SubClasshello
123

P298
import java.util.ArrayList;
import java.util.List;

public class Test {
	public static void main(String[] args){
		List
  
    l1 = new ArrayList
   
    (); List
    
      l2 = new ArrayList
     
      (); System.out.println(l1.getClass() == l2.getClass()); System.out.println(l1 instanceof ArrayList); } } 
     
    
   
  

true
true

由于系统中不会真正生成泛型类,所以 instanceof 运算符后不能使用泛型类

类型通配符

P300
import java.util.Arrays;
import java.util.List;

public class Test {
	public void test(List
   c)
	{
		for(int i=0; i
  
   
abcd
1
1.23
A

P302
import java.util.List;
import java.util.Arrays;

public class Canvas {
	public void drawAll(List
     shapes)
	{
		for(Shape s : shapes){
			s.draw(this);
		}
	}

	public String toString()
	{
		return "Canvas";
	}
	
	public static void main(String[] args)
	{
		List
    
      circleList = Arrays.asList(new Circle(), new Circle());
		Canvas c = new Canvas();
		c.drawAll(circleList);
	}
}

abstract class Shape
{
	public abstract void draw(Canvas c);
}

class Circle extends Shape
{
	public void draw(Canvas c)
	{
		System.out.println("Draw a circle on " + c);
	}
}

    

Draw a circle on Canvas
Draw a circle on Canvas

甚至可以为类型参数设定多个上限
public class Apple
    
     
{
	...
}

    

也可以设置通配符/泛型的下限
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class Test {
	
	public static
    
       T copy(Collection
      dest, Collection
     
       src) { T last = null; for (T ele : src){ last = ele; dest.add(ele); } return last; } public static void main(String[] args){ List
      
        ln = new ArrayList
       
        (); List
        
          li = new ArrayList
         
          (); li.add(5); Integer last = copy(ln, li); System.out.println(last); } }
         
        
       
      
     
    

5

泛型和数组不能同时使用,比如下面语句就会报错
List
    
     [] lsa = new ArrayList
     
      [10];
     
    

异常处理

Java把所有非正常情况分成两种 Exception 和 Error, 都继承自 Throwable父类try块后最多只有一个catch块会被执行try块后的花括号不能省略所有父类异常的catch都必须在子类异常catch后面不管try块中代码是否异常,finally块中代码总会被执行,所以可以用来回收try块中打开的物理资源子类方法中抛出的异常应该是父类方法声明抛出异常的子类或相同,子类方法中不允许抛出比父类方法声明更多的异常。

throws用法

import java.io.FileInputStream;
import java.io.IOException;

public class Test {
	public static void test() throws IOException
	{
		FileInputStream fils = new FileInputStream("a.txt");
	}
	public static void main(String[] args)
	{
		try{
			te