類定義了一種新的數(shù)據(jù)類型。
此新類型可用于創(chuàng)建該類型的對(duì)象。
類是對(duì)象的模板,對(duì)象是類的實(shí)例。
類定義的一般形式如下所示:
class classname { type instance-variable1; type instance-variable2; // ... type instance-variableN; type methodname1(parameter-list) { // body of method } type methodname2(parameter-list) { // body of method } // ... type methodnameN(parameter-list) { // body of method } }
使用class關(guān)鍵字聲明一個(gè)類。
在類中定義的方法和變量稱為類成員。
類中定義的變量稱為實(shí)例變量因?yàn)轭惖拿總€(gè)實(shí)例都包含這些變量的自己的副本。
一個(gè)對(duì)象的數(shù)據(jù)是獨(dú)立的,并且與另一個(gè)對(duì)象的數(shù)據(jù)唯一。
這里有一個(gè)名為 Box
的類,它定義了三個(gè)成員變量: width
, height
和 depth
。
class Box { int width; int height; int depth; }
創(chuàng)建類時(shí),將創(chuàng)建一個(gè)新的數(shù)據(jù)類型。您可以使用此類型來(lái)聲明該類型的對(duì)象。
創(chuàng)建類的對(duì)象是一個(gè)兩步過(guò)程。
以下行用于聲明一個(gè)類型為Box的對(duì)象:
Box mybox = new Box();
這個(gè)語(yǔ)句結(jié)合了這兩個(gè)步驟。 它可以像這樣重寫來(lái)顯示每個(gè)步驟更清楚:
Box mybox; // declare reference to object mybox = new Box(); // allocate a Box object
第一行聲明 mybox
作為 Box
類型的對(duì)象的引用。此行執(zhí)行后, mybox
包含值 null
。null表示 mybox
尚未指向?qū)嶋H對(duì)象。
此時(shí)嘗試使用 mybox
會(huì)導(dǎo)致錯(cuò)誤。
下一行分配一個(gè)實(shí)際對(duì)象并為mybox分配一個(gè)引用。第二行執(zhí)行后,您可以使用mybox作為Box對(duì)象。
mybox
保存實(shí)際Box對(duì)象的內(nèi)存地址。
類定義了一種新的數(shù)據(jù)類型。 在這種情況下,新類型稱為 Box
。要?jiǎng)?chuàng)建 Box
對(duì)象,您將使用如下語(yǔ)句:
class Box { int width; int height; int depth; } public class Main { public static void main(String args[]) { Box myBox = new Box(); myBox.width = 10; System.out.println("myBox.width:"+myBox.width); } }
輸出:
myBox
是 Box
的一個(gè)實(shí)例。 mybox
包含每個(gè)實(shí)例變量的自己的副本, width
, height
和 depth
由類定義。 要訪問(wèn)這些變量,您將使用點(diǎn)(.)運(yùn)算符。
mybox.width = 10;
此語(yǔ)句將寬度從 mybox
對(duì)象分配給 10
。這是一個(gè)使用 Box
類的完整程序:
任何嘗試使用空的mybox將導(dǎo)致編譯時(shí)錯(cuò)誤。
class Box { int width; int height; int depth; } public class Main { public static void main(String args[]) { Box myBox; myBox.width = 10; } }
如果嘗試編譯上面的代碼,您將從Java編譯器獲取以下錯(cuò)誤消息。
Java提供了運(yùn)行時(shí)運(yùn)算符instanceof來(lái)檢查對(duì)象的類類型。
instanceof運(yùn)算符具有以下一般形式:
object instanceof type
以下程序演示 instanceof
:
class A { } class B { } class C extends A { } class D extends A { } public class Main{ public static void main(String args[]) { A a = new A(); B b = new B(); C c = new C(); D d = new D(); if (a instanceof A) System.out.println("a is instance of A"); if (b instanceof B) System.out.println("b is instance of B"); if (c instanceof C) System.out.println("c is instance of C"); if (c instanceof A) System.out.println("c can be cast to A"); if (a instanceof C) System.out.println("a can be cast to C"); A ob; ob = d; // A reference to d System.out.println("ob now refers to d"); if (ob instanceof D) System.out.println("ob is instance of D"); ob = c; // A reference to c System.out.println("ob now refers to c"); if (ob instanceof D) System.out.println("ob can be cast to D"); else System.out.println("ob cannot be cast to D"); if (ob instanceof A) System.out.println("ob can be cast to A"); // all objects can be cast to Object if (a instanceof Object) System.out.println("a may be cast to Object"); if (b instanceof Object) System.out.println("b may be cast to Object"); if (c instanceof Object) System.out.println("c may be cast to Object"); if (d instanceof Object) System.out.println("d may be cast to Object"); } }
此程序的輸出如下所示:
更多建議: