今天一个朋友给以一些他面试的题,(学校和企业合作的,他说有点水,)
具体的题目就不说了,贴代码:
class A {
public String show(D obj){
return ("A and D");
}
public String show(A obj){
return ("A and A");
}
}
class B extends A{
public String test(){
return "test";
}
public String show(B obj){
return ("B and B");
}
public String show(A obj){
return ("B and A");
}
}
class C extends B{}
class D extends B{}
public class testMain {
public static void main(String ar[]) {
/*编码转换*/
String str = "测试";
try{
byte[] bs = str.getBytes("utf-8");
String str1 = new String(bs, "iso-8859-1");
System.out.println(str1);
String str2 = new String(bs, "gb2312");
System.out.println(str2);
String str3 = new String(bs, "utf-8");
System.out.println(str3);
byte[] bs2 = str.getBytes("iso-8859-1");
String str5 = new String(bs, "gb2312");
System.out.println(str5);
} catch(Exception e) {}
/*多态,不明白的同学多看看重写的定义,B中有一个方法是重写的,一个是自定义的 */
/*A a1 = new A();
A a2 = new B();
B b = new B();
C c = new C();
D d = new D();
System.out.println(a1.show(b));
System.out.println(a1.show(c));
System.out.println(a1.show(d));
System.out.println(a2.show(b));
System.out.println(a2.show(c));
System.out.println(a2.show(d));
System.out.println(b.show(b));
System.out.println(b.show(c));
System.out.println(b.show(d));
b.show
/*文件
* */
/*System.out.println(new File("aaaa").exists());
System.out.println(new File("aaa"));
String path="E:";
File file = new File(path);
System.out.println(file.isDirectory());
System.out.println(file.exists());
if(file.isDirectory()) {
File[] filelist = file.listFiles();
for(int i = 0; i<filelist.length; i++) {
if(filelist[i].isDirectory())
System.out.println(filelist[i].getName());
if(filelist[i].isFile()) {
System.out.println("文件:" + filelist[i].getName());
}
}
}
/*数组和集合的区别
* 看博客
* http://www.cnblogs.com/summers/p/4094260.html
* http://blog.csdn.net/liu1695347/article/details/4627292
* */
/*List l = new ArrayList<>();
l.add("132");
l.add(1);
l.add(1.0);
System.out.println(l.get(0) instanceof String);
System.out.println(l.get(1) instanceof Integer);
System.out.println(l);
/*时间*/
/*Date d = new Date();
System.out.println(d);
System.out.println(d.getTimezoneOffset());
System.out.println(d.getTime());
System.out.println(d.getDate());
System.out.println(d.getYear());
System.out.println(d.getMonth());
System.out.println(d.getDay());
System.out.println(d.getHours());
System.out.println(d.getMinutes());
System.out.println(d.getSeconds());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateNowStr = sdf.format(d);
System.out.println("转换日期格式" + dateNowStr);
String str = "2012-1-13 17:26:33";
try {
Date today = sdf.parse(str);
System.out.println("时间转换成String" + today);
} catch(Exception e) {}
Calendar now = Calendar.getInstance();
System.out.println();
System.out.println("年: " + now.get(Calendar.YEAR));
System.out.println("月: " + (now.get(Calendar.MONTH)+1));
System.out.println("日: " + now.get(Calendar.DAY_OF_MONTH));
System.out.println("时: " + now.get(Calendar.HOUR_OF_DAY));
System.out.println("分: " + now.get(Calendar.MINUTE));
System.out.println("秒: " + now.get(Calendar.SECOND));
System.out.println("1970" + now.getTimeInMillis());
System.out.println(now.getTime());
now.set(Calendar.DATE, 1);
now.roll(Calendar.DATE, -1);
int MaxDate=now.get(Calendar.DATE);
System.out.println("本月的最后一天是:"+MaxDate);
/*取小数点前两位,四舍五入*/
/*double m=7237.3589;
DecimalFormat df = new DecimalFormat("#.00");
System.out.println(df.format(m));
System.out.println(String.format("%.2f", m));
/*String转换int double*/
/*String str = "123";
Integer i = Integer.parseInt(str);
System.out.println(i);
Double d = Double.parseDouble(str);
System.out.println(d);*/
/*字符串逆序*/
//StringBuilder sb = new StringBuilder("abc");
//System.out.println(sb.reverse());
}
}
编码转换的时候,手贱了,改了一下.java的编码格式,我以为能变回来,结果eclipse让我失望了,汉字又敲了一遍,不对的地方希望同志们指出。
今晚是写不完了。有空更新…..基础很重要,基础很重要,基础很重要,