簡體   English   中英

對Java構造函數感到困惑

[英]Confused about java constructors

所以我正在學習Java 我要負責constructors ,並處理classes 我在確切了解他們的用途/目的時遇到一些問題? 我相信它們的用法類似於函數調用,在調用時將參數傳遞給函數?

我的想法正確嗎?

因此,例如:

class test{
    void durp(String input){
        System.out.print(input);
    }
}

如果我要像這樣在我的主類中創建一個對象:

test object = new test("hey");

它會將hey作為字符串傳遞給durp()

這個對嗎?

如果要像這樣在我的主類中創建一個對象,請執行以下操作:test object = new test(“ hey”); 它將“ hey”作為字符串傳遞給durp()吧?

不可以,因為您的方法durp()不是構造函數。 它只是屬於該類的方法,可以從創建的活動對象中調用。

public class Test {
    /** this is a constructor */
    public Test() {
    } 

    /** this is also a constructor with a parameter */
    public Test(String arg1) { 
        System.out.println(arg1); 
    } 

    /** this is a method of Test */
    public void derp() {
    }
}

您可以從oracle閱讀有關構造函數的本教程

class Test{

     // this is a constructor (name is same as class and no return type)
    public Test(String input){
      // some code here
   }

  // this is a method 
  public void durp(String input){
   // some code
  } 

 public static void main(String[] args){
       Test test = new Test("hey"); // calls constructor
  }

}

Java構造函數僅看起來像函數* ,但實際上它們是非常不同的:

  • 當對象完全初始化時,您將調用一個方法。 當對象尚不存在時,您調用構造函數
  • 方法不能改變類的final變量; 對於構造函數,這是他們目標的一部分
  • 方法可能會返回不同的結果; 構造函數什么也不返回
  • 方法調用可用於所有類型的表達式中。 構造函數只能作為new表達式的一部分來調用。

構造函數必須遵循特殊的命名約定:它們的名稱必須與類的名稱匹配。 就您而言,這將是test 通常,構造函數的任務是設置類的成員變量。 假設您的test類沒有此類變量,則不需要構造器** :像這樣的簡單調用就足夠了:

new test().drup("hello");


*在Java中,“函數調用”的合適術語是“方法調用”,盡管具有其他編程語言背景的程序員經常互換使用這兩個術語。

**當該類未定義自定義構造函數時,將為您提供一個不帶任何參數的默認構造函數。

在Java中,對象是被構造的。 每次創建新對象時,至少都會調用一個構造函數。 每個類都有一個構造函數,盡管如果您未明確創建一個構造函數,則編譯器將為您構建一個。 關於構造函數有很多規則,讓我們關注基本的聲明規則。 這是一個簡單的例子:

 class Tets{
   protected Test() { } // this is Test's constructor
   protected void Test() { } // this is a badly named,
   // but legal, method
 }

首先要注意的是,構造函數看起來非常像方法。 一個關鍵的區別是,構造函數永遠永遠不會擁有返回類型……永遠! 但是,構造函數聲明可以具有所有普通的訪問修飾符,並且可以像方法一樣接受參數(包括var-args)。 了解構造函數的另一大規則是,它們必須與聲明它們的類具有相同的名稱。 構造函數不能被標記為靜態(畢竟它們與對象實例化相關聯),它們也不能被標記為final或abstract(因為它們不能被覆蓋)。 以下是一些合法和非法的構造函數聲明:

 class Foo2 {
    // legal constructors
    Foo2() { }
    private Foo2(byte b) { }
    Foo2(int x) { }
    Foo2(int x, int... y) { }
    // illegal constructors
    void Foo2() { } // it's a method, not a constructor
    Foo() { } // not a method or a constructor
    Foo2(short s); // looks like an abstract method
    static Foo2(float f) { } // can't be static
    final Foo2(long x) { } // can't be final
    abstract Foo2(char c) { } // can't be abstract
    Foo2(int... x, int t) { } // bad var-arg syntax
}

訪問https://java.net/downloads/jfjug/SCJP%20Sun%20Certified%20Programmer%20for%20Java%206-0071591060.pdf

您需要學習核心的Java基礎知識,它可以告訴您有關帶參數的構造函數的信息。

class test{

public test(String s)
{
   durp(s);
}

 void durp(String input){

  System.out.print(input);

} 
  public static void main(String args[])
  {
   test obj=new test("hey");
 }
 }

Java中的構造函數用於構造對象,例如,您要設置durp的值(而不是durp(),因為這是一種方法),因此可以將構造函數用於此類任務。

我想你想要這樣的東西:

class Test {
    private String durp;

    public Test(String durp) {
        //set value of durp
        this.durp = durp;
    }

    //function for getting durp string.
    //Use getDurp() rather than durp() in java.
    public String getDurp() {
        return durp;
    }
}

在OOP中, 構造函數是將生命帶入類的東西。 方法 (在OOP中,它們是方法,而不是函數 )只是使用已經存在的對象(以前使用構造函數創建)執行某些操作。 每個類都有其默認構造函數(無參數構造函數),除非您重新定義自己的構造函數。 在您的情況下:

class Test{
   String durp;
   public Test(String anInput)
   {
      this.durp = anInput;
   }

   void durp(){
      System.out.print(input);
   }
}

此時,在代碼的其他地方,您應該具有一個用於構造類的方法,例如:

Test test = new Test("hey");
test.durp() // prints "hey" (without quote)

注意:作為命名約定,類名始終以大寫字母和駝峰符號(即ThisIsMyClass)開頭,在這種情況下為Test 爭論真的很廣泛,您需要學習很多新東西。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM