现在的位置: 首页 > 综合 > 正文

Java学习笔记2

2013年09月16日 ⁄ 综合 ⁄ 共 2554字 ⁄ 字号 评论关闭

注意点:基本类型是值类型(比如int)

类类型是引用类型(reference type)

因此Java中通常是引用传递

比如void print(List<Integer> l),

print(l),这种函数传递参数的方式便是使用的引用传递,而C++不使用指针与引用的时候是使用的值传递

来自该网址

http://docs.oracle.com/javase/tutorial/java/generics/methods.html

http://docs.oracle.com/javase/tutorial/extra/generics/index.html

泛型Generics

泛型类的例子

public class Box<T> {
    // T stands for "Type"
    private T t;

    public void set(T t) { this.t = t; }
    public T get() { return t; }
}

泛型类型的调用

Box<Integer> integerBox;

实例化泛型类对象

Box<Integer> integerBox = new Box<Integer>();

The syntax for a generic method includes a type parameter, inside angle brackets, and appears before the method's return type. For static generic methods, the type parameter section must appear before the
method's return type

泛型方法的类型参数包含在尖括号内,出现在方法的返回类型前

public class Util {
    // Generic static method
    public static <K, V> boolean compare(Pair<K, V> p1, Pair<K, V> p2) {
        return p1.getKey().equals(p2.getKey()) &&
               p1.getValue().equals(p2.getValue());
    }
}

泛型方法的调用

boolean same = Util.<Integer, String>compare(p1, p2);

也可以简化成

boolean same = Util.compare(p1, p2);

编译器会推断泛型方法的参数类型

This feature, known as type inference, allows you to invoke a generic method as an ordinary method,
without specifying a type between angle brackets




wildcard通配符

List<String>不是List<Object>的subtype(子类型),否则类似下面的代码就会造成问题
List<String> ls = new ArrayList<String>(); // 1
List<Object> lo = ls; // 2 
lo.add(new Object()); // 3
String s = ls.get(0); // 4: Attempts to assign an Object to a String!

Java对象的赋值相当于引用传递,lo与ls指向内存中的同一个对象,这样就导致问题

容器中可以使用通配符的方法,即在尖括号中添加?,比如List<?>,?匹配相应的类型
import java.util.*;

public class Test1 {
	static void printList(List<?> l){
		for(Object o:l){
			System.out.println(o);
		}
		
	}
	public static void main(String[] args){
		List<Integer> l=new ArrayList<Integer>();
		l.add(new Integer(5));
		l.add(new Integer(6));
		l.add(new Integer(7));
		printList(l);
		
	}

}

也可以限定通配符所能匹配的类型范围,比如

List<? extends Shape> shapes
便只能匹配从类Shape派生出的类型
Generic methods allow type parameters to be used to express dependencies among the types of one or more arguments to a method and/or its return type. If there isn't such a dependency, a
generic method should not be used.
泛型方法允许类型参数用来表达一个或多个参数之间在方法和返回类型上的依赖性
注:如果有方法的多个参数之间有相互依赖关系,便可以使用泛型方法。或者方法的一个参数与返回类型之间存在依赖性也可以使用泛型方法
例如
src的参数依赖于类型T,所以用泛型比较合适
class Collections {
    public static <T> void copy(List<T> dest, List<? extends T> src) {
    ...
}

若把该方法改为

class Collections {
    public static <T, S extends T> void copy(List<T> dest, List<S> src) {
    ...
}

因为S只在参数src中使用,所以用泛型不合适,可以改为通配符。

本地类local class
Local classes are classes that are defined in a block, which is a group of
zero or more statements between balanced braces. You typically find local classes defined in the body of a method.
本地类是定义在块中的类。通常本地类定义在方法体中。
本地类通常不能定义或者声明静态成员。但可以定义静态常数变量。本地类是非静态的(non-static)





Wanonymous classes are expressions, which means that you define the class in another expression.
匿名类是表达式。

匿名类需要new 操作符,实现的接口或继承的类的名字

抱歉!评论已关闭.