J2SE
1.類:具有相同的特征和行為的一個抽象的集合。對象:類的實(shí)例化
2.Java命名規(guī)則a.類名:首字母大寫,若由多個單詞構(gòu)成,每個單詞首字母大寫
b.變量名:首字母小寫,從第二個單詞字母開始大寫,如intArray
c. 常量:所有字母都大寫,CLASS_LOAD_PATH
d.關(guān)鍵字:所有字母都小寫,if,int
e.方法:首字母小寫,從第二個單詞字母開始大寫,()是方法的標(biāo)志,如changColour( );
3.構(gòu)造方法的規(guī)則:a.與類同名,首字母大寫;
b.無返回值也不加void,而且絕對不會有返回值;
c.在new的時候調(diào)用,是對對象進(jìn)行初始化的;
e.若無自定義的構(gòu)造方法,則自動生成一個default空構(gòu)造方法;若有,則default自動失效.
4.a(chǎn).成員變量:直接在類里面定義的變量,作用域是整個范圍
b.局部變量:在方法的內(nèi)部或方法的參數(shù)定義,是在定義他的{ }范圍內(nèi)有效。
c.若局部變量與成員變量同名,因就近原則直接用成員名調(diào)用的是局部變量,用this.變量名調(diào)用的是成員變量。class Person{ private String name;private int age;
Person(String name,int age){this.name=name; this.age=age;}}
5.外附類(wapper):把基礎(chǔ)類型包裝成類,如Integer,String.
Eg: class integer{int i; integer(int i){ this.i=i; } }
6.equals:在外附類里是判斷類值是否相同,在普通類里面等價(jià)于”= =”,判斷是否是同一個對象。
= =:對于基本數(shù)據(jù)類型,是判斷值是否相等,對于類類型判斷是否是同一個對象。
Eg:public static void main(String args[]){Integer n1=new Integer(47);
Integer n2=new Integer(47);System.out.print(n1.equals(n2));//true
System.out.print(n1==n2);//false
7.=:基本數(shù)據(jù)類型表示賦值,對于類類型表示內(nèi)存空間引用的傳遞。
class Number{int i;}
public class Test{ public static void main(String args[]){ Number n1=new Number();
Number n2=new Number();n1.i=9;n2.i=10; n1=n2;//n1和n2的值都為10;
n1.i=20;// 此時n1和n2都為20 }}
8.常見控制語句:public class XDay{public static void main(String args[]){
int yearis=2100;int monthis=12;DayCount db=new DayCount();
int s= db.countDays(monthis,yearis); }}
class DayCount{ int countDays(int month,int year){ int count=-1;switch(month){
case 1: case 3: case 5: case 7: case 8: case 10: case 12: count=31;break;
case 4: case 6: case 9: case 11: count=3;break;
case 2: if(year%4==0)count=29;else count=28;
if((year%100==0)&&(year%400!=0)) count=28;} return count;}}
9.從客戶端輸入:public class MonthCounter{public static void main(String args[]){
Integer intYearMonth1=Integer.parseInt(args[0]);
Integer intYearMonth2=Integer.parseInt(args[1]);//輸入并轉(zhuǎn)換為整型類
int intYear1, intYear2,; intYear1=intYearMonth1/100;intYear2=intYearMonth2/100; }}
轉(zhuǎn)換為字符串類:String intYear2=String.valueof(intYearMon1);
10.一維數(shù)組:a.數(shù)據(jù)類型是一致的 b.數(shù)組大小是確定的 c.初始化:靜態(tài)初始化int a[]={3,4};
動態(tài)初始化:聲明:int a[];創(chuàng)建:a=new int[5];賦值:a[0]=1;a[1]=2;
聲明+創(chuàng)建: Integer a[]=new Integer(2);//另開辟內(nèi)存指向新空間
public class Array{public static void main(String args[]){
String a[]={"奧林","花園","在哪里"};String b[]=new String[5];
Integer c[]=new Integer[3]; c[0]=1;c[1]=2;c[2]=3;
b[3]="誰";b[4]="知道";
System.arraycopy(a,0,b,0,3);//源,頭,目標(biāo),頭,長度,大小寫特殊
for(int i=0;i<b.length;i++) System.out.print(b[i]);
for(int j=0;j<c.length;j++)System.out.print(c[j]); }}
11.二維數(shù)組:靜態(tài)初始化:int a[][]={{1,2},{2,3}};
動態(tài)初始化:聲明:int a[][];創(chuàng)建:a=new int[2][];具體行:a[0]=new int[2];a[1]=new int[3];
賦值:a[0][0]=2;a[0][1]=3;a[1][0]=1;a[1][1]=4;a[1][2]=5;
12. Java的類的成員的初始化順序: 首先初始化所有的成員變量,然后初始化構(gòu)造方法.最后初始化普通方法,而且普通方法只有在“對象名.方法名”中才會被初始化,這就是緩式初始化.
13. 封裝:a.把功能和屬性包裝成類.通過類的實(shí)例來實(shí)現(xiàn);b.隱藏實(shí)現(xiàn)的細(xì)節(jié);
c.通過包把類組織起來;d.訪問權(quán)限的控制.
打包:package pac1;
public class Pac{public void display(){System.out.println("正在打包");}}
調(diào)試包:javac –d .\ Pac.java 使用包:import pac1.Pac
14.訪問權(quán)限:private:只能在類的內(nèi)部調(diào)用,默認(rèn)friend:在類的內(nèi)部和同一個包中被調(diào)用
public:類的內(nèi)部,同一個包中其他類,包外的其他類都可以調(diào)用
protected: 在類的內(nèi)部和同一個包中被調(diào)用,但可以在包外被繼承,通過.調(diào)用內(nèi)部成員
eg:class Robot{String name="liming";
protected String display(){return(name);}} //換成private就出錯
class Person extends Robot{public void display2(){
System.out.println(display());}}
public class UseRobot1{public static void main(String args[]){
Person n1=new Person(); n1.display2(); }}
15.方法重載:同一個類中,方法名相同,參數(shù)不同,方法體不同;
方法重寫:不同的類中,方法體不同,參數(shù)相同,方法名也相同.
方法重載的作用: 在有多個同名參數(shù)的類中,在調(diào)用時通過不同的參數(shù)來調(diào)用不同的方法.
方法重寫的作用: 在繼承中,子類可用方法重寫,對父類進(jìn)行擴(kuò)充或改造.
class Flower{int petalCount = 0; String s=new String("null");
Flower(int petal){ petalCount=petal;System.out.println(petalCount);}
Flower(int petal,String ss){this(petal);s=ss; System.out.println(s);}
public static void main(String args[]) {
Flower n1=new Flower(4,"dd"); Flower n2=new Flower(5);} }
this:可以實(shí)現(xiàn)一個構(gòu)造方法調(diào)用另一個構(gòu)造方法,this(參數(shù));普通方法不能調(diào)用構(gòu)造方法,對this的調(diào)用必須是該構(gòu)造方法的第一個語句(本類的)。
16.名稱遮蔽:子類中若對父類進(jìn)行方法重寫,出現(xiàn)與父類同名的屬性,則會將父類中與其同名屬
性遮蔽掉. 在子類中,如果要調(diào)用父類的構(gòu)造方法,必須使用super(參數(shù))調(diào)用,
若要調(diào)用父類的普通方法則用super.方法名;調(diào)用.且子類中對super的調(diào)用必須
是子類構(gòu)造方法所做的第一件事.這樣便可以繞過名稱遮蔽。
class Test3{ int value=8; int display4(){ return value; }}
class UseTest extends Test3{int value=9;int display5(){return value;}
int display6(){return(super.value); }}
class NiHao{ public static void main(String args[]){ UseTest n1=new UseTest();
System.out.println(n1.display5());System.out.println(n1.display6());}}
若父類沒有空構(gòu)造方法,若子類想初始化則子類必須有非空構(gòu)造方法;則在子類構(gòu)造方
法中用super(參數(shù))對父類初始化。
class Person{ public String name;public int age;
Person(String name,int age){this.name=name;this.age=age; }}
class Student extends Person{public String school;
Student(String name,int age,String s){super(name,age); school=s;}}
public class UsePerson{public static void main(String args[]){
Student n1=new Student("nihao",18,"keda");//new的時候?qū)?gòu)造函數(shù)初始化
System.out.println(n1.name); System.out.println(n1.school);}}
17.多態(tài)多態(tài):相同的事物調(diào)用的是相同的方法,參數(shù)也相同,表現(xiàn)形式去不同
a.子類能以父類的身份出 現(xiàn)
b.不同的子類以共同的父類身份出現(xiàn),但做事情時以自己的方式來實(shí)現(xiàn)
c.子類以父類身份出現(xiàn),只有與父類共有的屬性和功能才能被調(diào)用,自己個性化的功能就會失效。(方法可以多態(tài)進(jìn)行重寫,變量則不可多態(tài),不能重寫)
class Instrument{public void play(){System.out.println("instrument.play");} }
class Wind extends Instrument{ public void play(){System.out.println("wind.play");}}
class Ehu extends Instrument{public void play(){System.out.println("Ehu.play");}}
public class Music3{static void tune(Instrument i){i.play();}//類為參數(shù),多態(tài)參數(shù)
static void tuneAll(Instrument e[]){for(int i=0;i<e.length;i++)tune(e[i]);}
public static void main(String[] args){ Instrument[] a=new Instrument[2];
int i=0; a[i++]=new Wind();a[i++]=new Ehu();tuneAll(a); } }//異類集合
18.繼承:a.子類自動擁有父類非private的屬性和功能;
b.子類還可以擁有自己所特有的屬性和功能;
c.子類可以繼承父類的屬性和功能,并可以修改,以自己的方式來實(shí)現(xiàn).
class Person{String name;}
class Worker extends Person{String copration="witbridge";
public String getCorp(){return copration;}}
class Student extends Person{String school="witbridge";
public String getSchool(){return school;}}
public class Display{public void method(Person e){
if(e instanceof Student){//執(zhí)行期型別辯識。測試屬于哪個類
Student me=(Student)e;System.out.println("this is student "+me.getSchool());}
else if(e instanceof Worker){ Worker me2=(Worker)e;//轉(zhuǎn)化為類類型
System.out.println("this is worker "+me2.getCorp()); }
else{ Person me3=(Person)e;System.out.println("this is person ");}}
public static void main(String args[]){ Display t=new Display();
Worker w1=new Worker();t.method(w1);Student w2=new Student(); t.method(w2);
Person w3=new Person();t.method(w3);}}
19.final: 最終的,不可改變的.
a. 修飾變量:為常量,值不可變; b.修飾對象,數(shù)組:值可變,引用不變;
c. 修飾方法:方法不可重寫,但不代表內(nèi)部成員也是 final; d.修飾類:不可以被繼承,更不可能被重寫,也就是方法是 final的。
20.static:a. 修飾的成員為類成員,不需要實(shí)例化,就可以用類名.成員名;調(diào)用;
b. static的成員,不管產(chǎn)生多少實(shí)例,都指向同一個內(nèi)存空間;
c. static語句塊中定義的語句總會在第一次初始化時執(zhí)行一次。
d. 在static的語句塊中的變量在成員中也必須是static的,否則類型不匹配;要想調(diào)用成員中非static變量和方法必須采用New的方式。
class Value{int i=1;}
public class FinalData{final int i1=9;
static final int VAL_TWO=99; public static final int VAL_THREE=39;
final int i4=(int)(Math.random()*20);//值只存在一個運(yùn)行周期不變
static final int i5=(int)(Math.random()*20);//值將不改變,由創(chuàng)建第一次實(shí)例確定
Value v1=new Value();final Value v2=new Value();static final Value v3=new Value();
final int[] a={1,2,3};public void print(String id){System.out.println(id+i4+i5);}
public static void main(String args[]){FinalData fd1=new FinalData();
fd1.v2.i++;fd1.v1=new Value();for(int i=0;i<fd1.a.length;i++)fd1.a[i]++;
fd1.print("fd1");System.out.println("creating new FinalData");
FinalData fd2=new FinalData();fd1.print("fd1");fd2.print("fd2");}}
//(this不能出現(xiàn)在static方法調(diào)用中)eg:class Person{public static int total=10;
Static{ int i=10;int total=100;this.total=total;}}//錯誤,static體中為局部變量
21.接口:特殊的抽象類.只包含常量和方法的定義,無變量和方法的實(shí)現(xiàn),用implements繼承;
a.接口中所有方法都是抽象的,而且默認(rèn)都是public訪問權(quán)限的(interface)
b.一個類要實(shí)現(xiàn)某接口就一定要實(shí)現(xiàn)此接口中所有抽象方法,否則此類必為抽象類
抽象類:a.用abstract來修飾.抽象類無方法體;但是抽象類不一定有抽象方法;但有抽象方法的一定是抽象類;只能通過子類的實(shí)例,以父類的身份出現(xiàn);
b..抽象類是不可以New的
c.非抽象類的子類繼承時,必須重寫其所有的抽象方法,否則自己就為抽象類.
區(qū)別: 一個具象類只能繼承一個抽象類,但能繼承多個接口.
接口中的成員都是static & final類型的.
Java中沒有多重繼承,但可以通過接口,來實(shí)現(xiàn)多重繼承機(jī)制
interface Interface{public void f();void g();}
abstract class First implements Interface{public void f(){
System.out.println("first.f();");}}//通過繼承都重寫了,所以Second就不用abstract;
class Second extends First{public void g(){System.out.println("second.g();");}}
public class Demo{public static void main(String args[]){//子類訪問權(quán)限不能小于父類
Interface f=new Second();f.f();f.g();}}//抽象類不能呢new,只能以父類身份出現(xiàn)
22. 利用參數(shù)查找類:import java.lang.*; interface Common{ int time(int a,int b,int c);}
class Car implements Common{ public int time(int a,int b,int c){return(a*b/c);}}
class Plane implements Common{ public int time(int a,int b,int c){return(a+b+c);}}
public class ComputeTime{ public static void main(String args[]){
try{ int A = Integer.parseInt(args[1]);int B = Integer.parseInt(args[2]);
int C = Integer.parseInt(args[3]);int v,t;
Common d = (Common)Class.forName(args[0]).newInstance();//向上轉(zhuǎn)型,是不安全的
v = d.time(A,B,C);t= 1000/v;
System.out.println("運(yùn)行速度:"+v);System.out.println("時間:"+t+"小"); }
catch(Exception e){System.out.println("class not found");}}}
23.集合:把性質(zhì)相似的事物匯集成一個整體,集合框架:用來操作集合的標(biāo)準(zhǔn)體系結(jié)構(gòu)。
--ArrayList(有序的數(shù)組類) --HashMap(非同步)
--List接口 --Vector(線程同步)
Collection --LinkList Map(散列表) --Hashtable
--Set接口 -- WeakHashMap
24.ArrayList:a.可以添加重復(fù)的對象b.可以有null. c.線程非同步(可同時被多個調(diào)用)
import java.util.*;
public class ArrayListDemo {List a1=new ArrayList();public ArrayListDemo(){
a1.add("a");a1.add("b");a1.remove("a");}//添加對象和移出對象
public void display(){int n=a1.size();System.out.println("list大小為"+n);
for(int i=0;i<n;i++){System.out.print(a1.get(i).toString());}}//為objec自動調(diào)用
public void insertElement(int index,Object obj){a1.add(index, obj);}
public void removeElement(int index){ a1.remove(index);}
public void setElement(int index,Object obj){ a1.set(index,obj);}
public static void main(String args[]){ArrayListDemo ald=new ArrayListDemo();
ald.insertElement(2, "xx");ald.display();ald.removeElement(3);ald.display();
ald.setElement(1, "toto");ald.display();}}
25.HashMap:a.可以有空鍵也可以有空值,但是空鍵只能有一個,空值可以多個;線程非同步;
b.不能通過get找出鍵和值的一一映射,只能通過containsKey判斷值是否存在;
import java.util.*; class Counter{ int i=1;
public String toString(){ return Integer.toString(i); }}//Object被String等類繼承
public class TestHashMap {public static void main(String args[]){
HashMap hm=new HashMap();for(int i=0;i<10;i++){
Integer r=new Integer((int)(Math.random()*20));//random產(chǎn)生的是double型的
if(hm.containsKey(r)) ((Counter)hm.get(r)).i++;
else hm.put(r,new Counter());} System.out.println(hm);}}
26.Hashtable:a.任何非空對象b. 線程同步;import java.util.HashMap;
public class DemoHashTable {public static void main(String args[]){
Hashtable number=new Hashtable ();//表都是通過put添加值的
number.put("one",1);number.put("two",2);number.put("two",new Integer(3));
Integer n=(Integer)number.get("two"); System.out.println("two="+n);}}
27.迭代器Iterator:Collection有迭代器可以實(shí)現(xiàn)顯示全部循環(huán)編列集合,而Map沒有迭代器,通過values方法將Map轉(zhuǎn)為Collection接口。import java.util.*;
public class TestIterator {public static void main(String args[]){
ArrayList h=new ArrayList();h.add("1st");h.add(new Double(4.0));
Iterator it=h.iterator();//iterator為List接口下的方法,返回類型為Iterator;
while(it.hasNext()){System.out.println(it.next());}//hasNext,next為迭代器方法
Set s=new HashSet();s.add("1st");s.add(new Integer(3));//Set接口輸出無序且無重復(fù)值;
Iterator is=s.iterator();while(is.hasNext()){System.out.println(is.next());}}}
28.線程:進(jìn)程中的一個單一的連續(xù)控制流程。a.Java至少有兩個線程(主線程+垃圾回收線程)
b.進(jìn)程:多個線程,使用系統(tǒng)中的運(yùn)行資源。
public class Thread extends Object implements Runnable(Runnable只有一個run()方法)。
class MyThread extends Thread{ public void run(){System.out.println(getName());}}
public class MultiThread{public static void main(String args[]){
MyThread mt=new MyThread();mt.start();//線程自動運(yùn)行
System.out.println(“main”+Thread.currentThread().getName());}}
29.具備線程能力:繼承Runnable接口,還得傳遞給Thread,不過可以方便實(shí)行多接口繼承。
class Runner1 implements Runnable{public void run(){
for(int i=0;i<30;i++){ System.out.println("no."+i);}}}
public class TestThread1 {public static void main(String[] args) {
Runner1 r=new Runner1();Thread t=new Thread(r); t.start();}}
30.多個線程同時啟動,交替搶占CPU運(yùn)行,具體運(yùn)行由當(dāng)前CPU效率來決定。
Sleep方法可以讓線程休眠,synchronized關(guān)鍵字可以使線程變成同步。
class One{synchronized void display(int num){ System.out.println("one同步方法"+num);
try{Thread.sleep(2000); System.out.println("線程完成");}
catch(Exception ex){System.out.println("線程中斷");}}}
class Two implements Runnable{int number; One one; Thread t;
public Two(One one_num,int n){one=one_num;number=n;t=new Thread(this);t.start();}
public void run(){ one.display(number); } }
public class TestSynchronized {public static void main(String args[]){
One one=new One();int tag=10;Two s1=new Two(one,tag++);Two s2=new Two(one,tag++);
try{s1.t.join();s2.t.join();}//Thread.join();等待該線程終止。
catch(Exception ex){System.out.println("中斷");}}}
31.輸入輸出流:a.Stream 字節(jié)流處理,InputStream輸入流,read()方法讀入字節(jié),實(shí)現(xiàn)類FileInputStream 處理文件;OutputStream輸出流,write()方法寫出字節(jié)
b.Reader字符流的處理,被InputStreamReader和OutputStreamReader繼承。
import java.io.*; public class CopyFile {public static void main(String args[]){
try{ FileInputStream fis=new FileInputStream("CopyFile.java");
FileOutputStream fos=new FileOutputStream("temp.txt"); int read=fis.read();
while(read!=-1){ fos.write(read); read=fis.read();}//實(shí)現(xiàn)循環(huán)走動
fis.close();fos.close();}//輸出不關(guān)閉可以無限輸入,輸出不關(guān)閉不能刷新
catch(IOException e){System.out.println(e);}}}
32.public class CopyFile1 {public static void main(String args[]){
try{ File f=new File("filename.txt");//File文件和目錄路徑名的抽象表現(xiàn)形式
FileReader fr=new FileReader(f);//繼承了InputStreamReader,用于讀取字符流
BufferedReader br=new BufferedReader(fr);//繼承Reader從字符輸入流中讀取文本緩沖字符
String line=br.readLine();//讀取文本行,返回String,read(返回int)讀取單個字符
while(line!=null){System.out.println("讀的數(shù)據(jù)"+line);line=br.readLine();}
br.close();}catch(Exception ex){ex.printStackTrace();}}}
33.高速讀取,高速寫入:public class InputOutput {public static void main(String[] args){
try{File fname=new File("newfile.txt");FileReader fr=new FileReader(fname);
BufferedReader br=new BufferedReader(fr);//可以通過new InputStreamReader(System.in)
FileWriter fw=new FileWriter("copynewfine.txt");
BufferedWriter bw=new BufferedWriter(fw); String line=br.readLine();
while(line!=null){bw.write(line);bw.newLine();line=br.readLine();}控制關(guān)閉
br.close();bw.close();} catch(Exception ex){ex.printStackTrace();}}}
34.如果用read讀,int i=in.read();必須System.out.print((char)i);否則輸出16位字節(jié)內(nèi)存地址。
35.異常:程序在正常情況下運(yùn)行,出現(xiàn)正常的情況而使程序終止。捕獲異常使程序可正常進(jìn)行
a.Error JVM系統(tǒng)內(nèi)部錯誤;b.Exception:程序內(nèi)部錯誤。
c.語法結(jié)構(gòu)try{ } catch{ } finally{ 無條件執(zhí)行,通常情況下用于釋放資源 close }
36.catch(Exception e)信息:e.printStackTrace();打印異常路徑,名稱
e.toString();打印異常名和信息;e.getMessage();打印異常信息。
37.自定義異常:throws為拋出給誰去處理異常,throw為產(chǎn)生異常。
class UseException extends Exception{public UseException(String s) { super(s);}}
public class Test{ public static void main(String args[]) throws UseException{
try {String a[]={"a","b","c"};for(int i=0;i<4;i++){System.out.println(a[i]);}}
catch(Exception e){throw new UseException("數(shù)組超過界限了!");}}}
38.JDK 5.0新特性
a.自動拆裝箱:拆:對象—>基本數(shù)據(jù)類型;裝:基本數(shù)據(jù)類型-à對象
public class AutoBoxDemo { //避免把值類型數(shù)據(jù)保存到集合中 需要裝箱(add()只接受對象)
public static void Show(Integer intObject){ System.out.println(intObject);}
public static void show1(){Show(new Integer(8));}public static void show2(){Show(90);}
public static void main(String args[]){show1();show2();}}
b.增強(qiáng)的循環(huán):有利于防止數(shù)組下標(biāo)越界;確保安全性,因?yàn)槭箶?shù)組內(nèi)數(shù)據(jù)類型一致
public class For5 {public static void main(String args[]){
int a[]={1,2,3,4};for(int aa:a){System.out.println("數(shù)組值是"+aa);}}}
c. 泛型:本質(zhì)就是參數(shù)化類型。即允許創(chuàng)建一個類,接口,方法時所操作的數(shù)據(jù)類型是一個參數(shù)。操作參數(shù)化類型的類,接口,方法被泛型.
c.1:類型安全:泛型的主要目標(biāo)是提高 Java類型安全 ,在編譯期間對容器內(nèi)的對象進(jìn)行類型檢查,在運(yùn)行期不必進(jìn)行類型的轉(zhuǎn)換。而在之前必須在運(yùn)行期動態(tài)進(jìn)行容器內(nèi)對象的檢查及轉(zhuǎn)換;
c.2:消除強(qiáng)制類型轉(zhuǎn)換:消除源代碼中的許多強(qiáng)制類型轉(zhuǎn)換。這使得代碼更加可讀,減少出錯;
c.3:潛在的性能收益:編譯器將強(qiáng)制類型轉(zhuǎn)換(沒有泛型的話,程序員會指定這些強(qiáng)制類型轉(zhuǎn)換)插入生成的字節(jié)碼中。
c.4:擦除: Java 語言中的泛型基本上完全在編譯器中實(shí)現(xiàn),由編譯器執(zhí)行類型檢查和類型推斷,然后生成普通的非泛型的字節(jié)碼。這種實(shí)現(xiàn)技術(shù)稱為擦除(erasure)(編譯器使用泛型類型信息保證類型安全,然后在生成字節(jié)碼之前將其清除)
public class GenDemo { public static void main(String args){
Gen<Integer> iob; iob=new Gen<Integer>(33);
iob.showType();int v=iob.getOb(); System.out.println("value"+v);
Gen<String> strOb=new Gen<String>("test");
strOb.showType();String str=strOb.getOb(); System.out.println("value"+str);}}
class Gen<T>{T ob;Gen(T o){ob=o;}T getOb(){return ob;}// 泛型類聲明,由參數(shù)決定
void showType(){System.out.println("type of t is"+ob.getClass().getName());}}
不需要類型轉(zhuǎn)化:import java.awt.*; import java.util.*;
public class TestList {public static void main(String args[]){test();test1();}
public static void test(){List list=new ArrayList();list.add("中國");
list.add("上海");for(Iterator item=list.iterator();item.hasNext;){
String s=(String)item.next();System.out.println(s);}}
public static void test1(){List<String> list=new ArrayList<String>();
list.add("你好");list.add("hello");for(String s:list){System.out.println(s);}}}
d. 通配符 ?變量的泛型聲明和方法的參數(shù)的泛型聲明有很大差別!import java.util.*;
public class Type {public static void main(String args[]){
List<String> list=new ArrayList<String>();list.add("1");list.add("2");
for(String s:list){System.out.println(s);}
List<Number> list2=new ArrayList<Number>();list2.add(new Integer(1));
list2.add(225);prt1(list2);prt2(list2);
List<Integer> list3=new ArrayList<Integer>();list3.add(new Integer(1));
list3.add(9); prt2(list3); }// prt1(list3);錯誤,類型不匹配
private static void prt1(List<Number> list){for(Number n:list){打印n;}}
private static void prt2(List<? extends Number>list){for(Number n:list){ 打印n;}}}
39.GUI程序設(shè)計(jì):import java.awt.*;
public class TestFrame {public static void main(String args[])
{ Frame f=new Frame("第一個窗口");f.setSize(170,100); f.setVisible(true);}}
利用適配器創(chuàng)建GUI:import java.awt.*;import java.awt.event.*;
public class TestAdapter {public static void main(String args[]){
Frame f=new Frame("gui");f.setSize(170,100);MyLis m=new MyLis ();
f.addWindowListener(m);f.setVisible(true);}}
class MyLis extends WindowAdapter{public void wClo(WindowEvent e){System.exit(0);}}
40.NetBeans中 適配器WindowAdapter類:
總接口:EventListener-à子接口:WindowListener,WindowFocusListener等——>
被WindowAdapter類全部繼承,其中的所有關(guān)閉,激活等方法需要的重寫就可以。
GUI設(shè)計(jì)原理:容器(JFrame,Jwindow等)-à使用add()添加控件類(JLable,JTextField)
-à調(diào)用Event事件響應(yīng)相應(yīng)的方法—>在IDE內(nèi)部由Listener負(fù)責(zé)監(jiān)聽Event
Eg:在NetBeans中構(gòu)建Swing:
private void jListSportValueChanged(javax.swing.event.ListSelectionEvent evt) {
String sport=(String)this.jListSport.getSelectedValue();
this.jLabelMessage.setText("你選擇了"+sport);}
private void jTextFieldAddressActionPerformed(java.awt.event.ActionEvent evt) {
String address=this.jTextFieldAddress.getText();
if(address!=null&&!address.equals("")){
this.jLabelMessage.setText("你輸入了"+address);// TODO 將在此處添加您的處理代碼:
}else{this.jLabelMessage.setText("你沒有輸入任何內(nèi)容!");}}
private void jRadioButtonDislikeItemStateChanged(java.awt.event.ItemEvent evt) {
this.jLabelMessage.setText("你選擇了討厭單選按鈕!");}
private void jCheckBoxBaggioItemStateChanged(java.awt.event.ItemEvent evt) {
if(this.jCheckBoxBaggio.isSelected()){this.jLabelMessage.setText("你喜歡巴橋了!");}
else{this.jLabelMessage.setText("你不喜歡巴橋了!");}}
private void jButtonEnterActionPerformed(java.awt.event.ActionEvent evt) {
this.jLabelMessage.setText("你按下了確定按紐!"); }
總結(jié):JLable主要方法:setText();JTextField通過Action事件主要方法:getText();
JRadioButton和CheckBox通過Item事件主要方法:isSelected();
JList通過ListSelection事件主要方法:getSelectedValue();
JSplitPane類將容器分成幾部分,擁有HORIZONTAL_SPLIT和VERTIVAL_SPLIT等屬性值。