校招笔试之2012金山快盘春季校招java笔试题

1,简述javaStringStringBuffer,StringBuild的区别。

   String:不可变类,线程安全;拼接字符串的时候容易产生大量碎片。

   StringBuffer:字符串缓冲类,可变类,不会产生字符串碎片问题。线程安全。

   StingBuild:字符串缓冲类,可变类,也不会产生字符串碎片问题。线程不安全。

    在不考虑线程安全的情况下,StringBuild的效率最高,StringBuffer次之,String最差。

2,java中要让一个方法不被子类覆盖,应该怎么定义。

    定义方法为final类型:

    定义为静态方法:

class T{
	public static void print(){
		System.out.println("T");
	}
}
class T2 extends T{
	public static void print(){
		System.out.println("T2");
	}
}



3,  找出第二大的数

假设有一个整数数组,请实现一个函数,找出其中第二大的数。(注:请给出你的解题思路和完整实现)

public static int getSecondNum1(int[] array){
		int r=-1;
		int max = array[0];
		if(array.length<2){
			throw new RuntimeException("数组长度不够!");
		}else {
			r = array[0];
			for(int i=0;i<array.length;i++){
				if(array[i]>max){
					r = max;
					max = array[i];
				}
			}
		}
		return r;
	}

4, K阶斐波那契数列第N项的值:略过了。。

5,日志类

我们在软件开发活动,特别是服务端程序中,为了分析程序的一些运行情况,经常需要在代码内插入一些日志记录语句。现在请你设计一个日志类KPLogs,方便开发使用。

该条日志等级Level:

package com.perfecking.log;

public enum Level {
	info,debug,warning,error
}

日志类接口Log:

package com.perfecking.log;

public interface Log {
	public void log(Level level,String info);
	public void info(String info);
	public void debug(String info);
	public void warinig(String info);
	public void error(String info);
}

KPLogs类的实现:

package com.perfecking.log;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Random;

public class KPLogs implements Log{
	private String logName = null;
	private String logPath = null;
	{
		String cPath = System.getProperty("user.dir");
		logPath = cPath+"\\log\\";
		checkFileDir(logPath);
	}
	public KPLogs(){
		
	}
	public KPLogs(String logName){
		if(!logName.endsWith(".txt")){
			logName+=".txt";
		}
		this.logName = logName;
	}
	
	public KPLogs(String logName,String logPath){
		if(!logName.endsWith(".txt")){
			logName+=".txt";
		}
		this.logName = logName;
		if(!logPath.endsWith("\\")){
			logPath+="\\";
		}
		this.logPath = logPath;
	}
	public void setLogName(String logName){
		if(!logName.endsWith(".txt")){
			logName+=".txt";
		}
		this.logName = logName;
	}
	public void setLogPath(String logPath){
		if(!logPath.endsWith("\\")){
			logPath+="\\";
		}
		this.logPath = logPath;
	}
	private void checkFileDir(String path){
		File file = new File(path);
		if(!file.exists()){
			file.mkdirs();
		}
	}
	
	private String getDefaultLogName(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd",Locale.CHINA);
		String dateString = sdf.format(new Date());
		Random random = new Random(System.currentTimeMillis());
		StringBuffer sBuffer = new StringBuffer();
		for(int i=0;i<5;i++){
			sBuffer.append(random.nextInt(10));
		}
		return dateString+" "+sBuffer.toString()+".txt";
	}
	
	private String getDefaultLogFilePath(){
		if(logName==null){
			logName = getDefaultLogName();
		}
		return logPath+logName;
	}
	
	@Override
	public synchronized void log(Level level, String info) {
		// TODO Auto-generated method stub
		String logFilePath = getDefaultLogFilePath();
		StringBuffer sBuffer = new StringBuffer(1024);
		sBuffer.append(getCurrentTimeStamp());
		sBuffer.append("\t"+level+"\t"+info);
		File file = new File(logFilePath);
		try {
			if (!file.exists()) {
				file.createNewFile();
			}
			FileWriter fileWriter = new FileWriter(file, true);
			BufferedWriter bWriter = new BufferedWriter(fileWriter);
			bWriter.write(sBuffer.toString());
			bWriter.newLine();
			bWriter.flush();
			bWriter.close();
		} catch (IOException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	private String getCurrentTimeStamp(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss",Locale.CHINA);
		return sdf.format(new Date());
	}
	@Override
	public void info(String info) {
		// TODO Auto-generated method stub
		log(Level.info, info);
	}
	@Override
	public void debug(String info) {
		// TODO Auto-generated method stub
		log(Level.debug, info);
	}
	@Override
	public void warinig(String info) {
		// TODO Auto-generated method stub
		log(Level.warning, info);
	}
	@Override
	public void error(String info) {
		// TODO Auto-generated method stub
		log(Level.error, info);
	}
}










你可能感兴趣的:(校招,金山校招)