今天一個朋友給以一些他面試的題,(學校和企業合作的,他說有點水,)
具體的題目就不說了,貼代碼:
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讓我失望了,漢字又敲了一遍,不對的地方希望同志們指出。
今晚是寫不完了。有空更新…..基礎很重要,基礎很重要,基礎很重要,