Java 類

2018-02-20 01:24 更新

Java教程 - Java類


類定義了一種新的數(shù)據(jù)類型。

此新類型可用于創(chuàng)建該類型的對象。

類是對象的模板,對象是類的實例。

語法

類定義的一般形式如下所示:

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)鍵字聲明一個類。

在類中定義的方法和變量稱為類成員。

類中定義的變量稱為實例變量因為類的每個實例都包含這些變量的自己的副本。

一個對象的數(shù)據(jù)是獨立的,并且與另一個對象的數(shù)據(jù)唯一。


例子

這里有一個名為 Box 的類,它定義了三個成員變量: width , height depth 。

 
class Box { 
    int width; 
    int height; 
    int depth; 
}

Java對象

創(chuàng)建類時,將創(chuàng)建一個新的數(shù)據(jù)類型。您可以使用此類型來聲明該類型的對象。

創(chuàng)建類的對象是一個兩步過程。

  • 聲明類類型的變量。
  • 使用new運算符動態(tài)分配對象的內(nèi)存。

以下行用于聲明一個類型為Box的對象:

Box mybox = new Box(); 

這個語句結(jié)合了這兩個步驟。 它可以像這樣重寫來顯示每個步驟更清楚:

Box mybox; // declare reference to object 
mybox = new Box(); // allocate a Box object 

第一行聲明 mybox 作為 Box 類型的對象的引用。此行執(zhí)行后, mybox 包含值 null 。null表示 mybox 尚未指向?qū)嶋H對象。

此時嘗試使用 mybox 會導(dǎo)致錯誤。

下一行分配一個實際對象并為mybox分配一個引用。第二行執(zhí)行后,您可以使用mybox作為Box對象。

mybox 保存實際Box對象的內(nèi)存地址。

類定義了一種新的數(shù)據(jù)類型。 在這種情況下,新類型稱為 Box 。要創(chuàng)建 Box 對象,您將使用如下語句:

 
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 的一個實例。 mybox 包含每個實例變量的自己的副本, width , height depth 由類定義。 要訪問這些變量,您將使用點(.)運算符。

mybox.width = 10;

此語句將寬度從 mybox 對象分配給 10 。這是一個使用 Box 類的完整程序:

任何嘗試使用空的mybox將導(dǎo)致編譯時錯誤。

class Box {
  int width;
  int height;
  int depth;
}

public class Main {
  public static void main(String args[]) {
    Box myBox;
    myBox.width = 10;
  }
}

如果嘗試編譯上面的代碼,您將從Java編譯器獲取以下錯誤消息。

Java instanceof運算符

Java提供了運行時運算符instanceof來檢查對象的類類型。

instanceof運算符具有以下一般形式:

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");
  }
}

此程序的輸出如下所示:

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號