package mytest;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
class Test {
public void test(Collection<?> c) {
System.out.println("collection");
}
}
public class SubTest extends Test{
private SubTest(){}
public void test(List<?> l) {
System.out.println("list");
}
public static void main(String[] args) {
List<String> l = new ArrayList<String>();
// Test t = new SubTest();//collection,重载方法,根据参数类型,编译期绑定,与运行时具体类型无关
SubTest t = new SubTest();//list,重载方法,根据参数类型,编译期绑定
t.test(l);
Collection<String> c = new ArrayList<String>();
// Test t2 = new SubTest();//collection,重载方法,根据参数类型,与运行时具体类型无关
SubTest t2 = new SubTest();//collection,重载方法,根据参数类型,编译期绑定
t2.test(c);
}
}
package mytest;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
class Test {
public void test(Collection<?> c) {
System.out.println("collection");
}
public void test(List<?> l) {
System.out.println("super_list");
}
}
public class SubTest2 extends Test{
private SubTest2(){}
public void test(List<?> l) {
System.out.println("list");
}
public static void main(String[] args) {
List<String> l = new ArrayList<String>();
// Test t = new SubTest();//list,子类重写父类方法,根据运行时对象的具体类型调用
SubTest2 t = new SubTest2();//list,子类重写父类方法,根据运行时对象的具体类型调用
t.test(l);
Collection<String> c = new ArrayList<String>();
// Test t2 = new SubTest();//collection,重载方法,根据参数类型,与运行时具体类型无关
SubTest2 t2 = new SubTest2();//collection,重载方法,根据参数类型,编译期绑定
t2.test(c);
}
}