课堂笔记-第三节

时间:2022-09-20 10:00:44
1、抽象类和接口的区别?
答:


他们都不能实例化,抽象类可以有构造方法,而接口没有构造方法,抽象类里可以有抽象方法,也可以没有抽象方法,接口里必须都为抽象方法,抽象类为单继承,接口为多继承
抽象类必须使用关键字abstract,,


接口:定义一个接口关键字为interface,接口里可以定义常量,但必须是fanal和Static的,要实现多重继承的目的


2、数组,array,Vector,LinkedList的内部实现机制?
答:
数组是开辟片新的空间,array,vector是一个数组,LinkedList是链表




3.用LinkedList写一个栈?
public class Stack {
LinkedList<Object>>public void push(Object obj){
linkedList.addLast(obj);

}
public Object pop(){

return linkedList.removeLast();
}
public static void main(String[] args) {
Stack>statck.push("a");
statck.push("b");
statck.push("c");
System.out.println(statck.pop());
System.out.println(statck.pop());
System.out.println(statck.pop());
}
}
4.用LinkedList写一个队列?
import java.util.LinkedList;




public class Stack {
LinkedList<Object>>public void push(Object obj){
linkedList.addLast(obj);-------

}
public Object pop(){

return linkedList.removeFirst();--------
}
public static void main(String[] args) {
Stack>statck.push("a");
statck.push("b");
statck.push("c");
System.out.println(statck.pop());
System.out.println(statck.pop());
System.out.println(statck.pop());
}
}


5.写一个通用的方法,找出数组中的最小值和最大值?
答:
首先定义一个类Pair类;
T代表通用泛型,方可实现通用
public class Pair<T> {


private T frist;//第一个数
private T secord;//第二个数


public Pair(T frist, T secord) {
super();
this.frist = frist;
this.secord = secord;
}


public T getFrist() {
return frist;
}


public void setFrist(T frist) {
this.frist = frist;
}


public T getSecord() {
return secord;
}


public void setSecord(T secord) {
this.secord = secord;
}


}
二、定义一个ArrAlg 类定义一个Pair<T> minmax(T[] ts)方法




public class ArrAlg {


public static<T extends Comparable> Pair<T> minmax(T[] ts){
if(ts==null||ts.length==0){
return null;
}
T>T>for (int>if(min.compareTo(ts[i])>0){
min=ts[i];
}
if(max.compareTo(ts[i])<0){

max = ts[i];
}
}
return new Pair<T>(min,max);
}
}


三、编写测试类:TestArry


import java.util.GregorianCalendar;




public class TestArry {
public static void main(String[] args) {
//int类型
Integer []>Pair<Integer>>System.out.println("最小值是:"+pa.getFrist()+"最大值:"+pa.getSecord());

//double类型
Double []>Pair<Double>>System.out.println("最小值是:"+pd.getFrist()+"最大值:"+pd.getSecord());
//日期类型
GregorianCalendar []>new GregorianCalendar(2013,5,1),
new GregorianCalendar(2012,12,19),
new GregorianCalendar(2011,7,19)
};
Pair<GregorianCalendar>>System.out.println("最小值是:"+grg.getFrist().getTime()+"最大值:"+grg.getSecord().getTime());


}
}


6.注解,


7.自定义Servlet有哪几种方式?
Servlet,HttpServlet,GenericServlet
8.结合Servlet生命周期手写一个Servlet代码结构?
--初始化
public void init(ServletConfig config) throws ServletExecption{

}
--服务
public><web-app version="3.0" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> 
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>


代码:
package>loadOnStartup = 1,initParams={
@WebInitParam(name="name",value="tom"),
@WebInitParam(name="age",value="10")
})loadOnStartup = 1,大于0加载,不能为负数
public class HelloServlet extends HttpServlet {





public void init(){
System.out.println("初始化------");

}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {



}



public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {


response.setContentType("text/html");
PrintWriter>out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println("  <BODY>");
out.print("    This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println("  </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}


}