Java 定義類寫法

來源:互聯網
上載者:User

Java 定義類寫法
Java程式的基本單位是類,類是對象的執行個體,或者說對象是類定義的的資料類型的變數。你建立類之後,就可用它來建立許多你需要的對象。Java把每一個可執行檔成分都變成類。
  類的定義形式如下:
  class classname extends superclassname
  {
    .....

  }
執行個體

class Animal {
  public Animal(String aType) {
    type = aType;
  }
  public String toString() {
    return "This is a " + type;
  }
  private String type;
}
public class MainClass {
  public static void main(String[] a) {
    Animal pet = new Animal("a");
    if (pet.getClass() == Animal.class) {
      System.out.println("it is an animal!");
    }
  }
}


  這 裡,classname和superclassname是合法的標識符。關鍵詞extends用來表明classname是superclassname派生的子類。有一個類叫做Object,它是所有Java類的根。如果你想定義Object的直接子類,你可以省略extends子句,編譯器會自動包含它。下面是一個簡單的類的定義。
  在類定義的開始與結束處必須使用花括弧。你也許想建立一個矩形類,那麼可以用如下代碼:
  public class Rectangle
  {
    ......

  }

執行個體

class Sphere {
  double radius; // Radius of a sphere

  Sphere() {

  }

  // Class constructor
  Sphere(double theRadius) {
    radius = theRadius; // Set the radius

  }

}

public class MainClass {
  public static void main(String[] arg){
    Sphere sp = new Sphere();
   
  }

}

類的執行個體建立
  矩形類Rectangle中,也許你想把矩形的相關資訊寫入類,如:width,height,當然你還可以寫入其它資訊,但或許長和寬對簡單的矩形來說已足夠了。現在,類的定義如下所示:
  public class Retangle
  {
     int width,height;
  }
  當你建立了自己的類之後,通常需要使用它來完成某種工作。你可以通過定義類的執行個體--對象來實現這種需求。
  對象是通過new來建立,實現成員函數如下:Rectangle myrect=new Rectangle,當然,此時對象myrect並沒有做任何什麼事;它只儲存了矩形的長和寬的資訊。有了對象以後,我們怎樣使用對象內部的資料呢?下面是幾個例子:
myrect.width=10;
myrect.height=20;

執行個體

class A {
  A(int marker) {
    System.out.println("Bowl(" + marker + ")");
  }

  void f(int marker) {
    System.out.println("f(" + marker + ")");
  }
}

class B {
  static A a = new A(1);

  B() {
    System.out.println("Table()");
    staticA.f(1);
  }

  void f2(int marker) {
    System.out.println("f2(" + marker + ")");
  }

  static A staticA = new A(2);
}

class C {
  A a = new A(3);

  static A staticA = new A(4);

  C() {
    System.out.println("Cupboard()");
    staticA.f(2);
  }

  void f3(int marker) {
    System.out.println("f3(" + marker + ")");
  }

  static A staticA2 = new A(5);
}

public class MainClass {
  public static void main(String[] args) {
    System.out.println("Creating new Cupboard() in main");
    new C();
    System.out.println("Creating new Cupboard() in main");
    new C();
    t2.f2(1);
    t3.f3(1);
  }

  static B t2 = new B();

  static C t3 = new C();
}
Bowl(1)
Bowl(2)
Table()
f(1)
Bowl(4)
Bowl(5)
Bowl(3)
Cupboard()
f(2)
Creating new Cupboard() in main
Bowl(3)
Cupboard()
f(2)
Creating new Cupboard() in main
Bowl(3)
Cupboard()
f(2)
f2(1)
f3(1)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.