1. 关于依赖注入:
依赖注入能够独立开发各组件,然后根据组件间关系进行组装。
依赖注入提供使用接口编程。
依赖注入指对象在使用时动态注入。
依赖注入的特点就是高内聚,低耦合。
2. 关于PreparedStatement
PreparedStatement是预编译的,使用PreparedStatement有几个好处
1. 在执行可变参数的一条SQL时,PreparedStatement比Statement的效率高,因为DBMS预编译一条SQL当然会比多次编译一条SQL的效率要高。
2. 安全性好,有效防止Sql注入等问题。
3. 对于多次重复执行的语句,使用PreparedStament效率会更高一点,并且在这种情况下也比较适合使用batch;
4. 代码的可读性和可维护性。
3. ArrayList list = new ArrayList(20);中的list扩充几次
虽然在ArrayList中,默认的初始长度是10。但是当我们使用带参数的构造方法时,则不需要扩充,直接分配一个我们定义长度的数组。
参考代码:
public ArrayList(int initialCapacity) {
if (initialCapacity > 0) {
this.elementData = new Object[initialCapacity];
} else if (initialCapacity == 0) {
this.elementData = EMPTY_ELEMENTDATA;
} else {
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
}
}
4. 关于case穿透
Which numbers will cause “Test2” to be printed?
1 2 3 4 5 6 7 8 9 10 11 12 13 | switch (x) { case 1: System.out.println("Test1"); case 2: case 3: System.out.println("Test2"); break; default: System.out.println("Test3"); break; } |
答案是当我们输入123时,都会输出“Test2”。这是为什么呢,很简单。当我们的case语句没有加break时,会出现case穿透而执行下一条case的语句。
5. 在Java中下面Class的声明哪些是错误的
public abstract final class Test {
abstract void method();
}
//-----------------------------
public abstract class Test {
abstract final void method();
}
//---------------------------------
public abstract class Test {
abstract void method() {
}
}
这三种声明方式都是错误的。
第一个: final修饰的类是终态类,这个类无法被继承。而抽象类是一定要被继承的,所以是错误的。因此,final不能用来修饰抽象类。
第二个: final修饰的方法为终态方法,不能被重写。很明显,抽象类中的抽象方法需要重写,所以也是错误的。
第三个: 既然已经声明了是抽象方法,就不能给出方法的实现体。
6. 关于继承的一点小知识
首先写出我写的两个简单的父类与子类
public class Parent {
public void introduce(){
System.out.println("this is the parent");
}
public void introduceAgain(){
System.out.println("introduce again");
introduce();
}
}
public class Son extends Parent{
public void introduce(){
System.out.println("this is the son");
}
}
public class MyTest {
public static void main(String[] args) {
Parent parent0 = new Parent();
parent0.introduceAgain();
Parent parent1 = new Son();
parent1.introduceAgain();
}
}
控制台结果:
introduce again
this is the parent
introduce again
this is the son
那么,在上转型对象的方法引用中,调用了子类的introduce方法。当执行introduceAgain()方法时,需要执行里面的introduce()方法。这时候,就会调用子类重写的父类的同名方法。