2017-08-03 166 views
-3
public class Person { 
    private int age;  

    public Person(int initialAge) { 
    if (initialAge<= 0) { 
     System.out.println("Age is not valid, setting age to 0."); 
    } 
     else { 
      age = initialAge; 
     } 
    // Add some more code to run some checks on initialAge 
    } 

    public void amIOld() { 
    if (age < 13) { 
     System.out.print("You are young."); 

    } 
    else if (age >= 13 && age < 18) { 
     System.out.print("You are a teenager."); 
    } 
    else { 
     System.out.print("You are old."); 
    } 
    // Write code determining if this person's age is old and print the correct statement: 
    System.out.println(/*Insert correct print statement here*/); 
    } 

    public void yearPasses() { 
    age += 1; 

    } 




    public static void main(String[] args { 

    Scanner sc = new Scanner(System.in); 
    int T = sc.nextInt(); 
    for (int i = 0; i < T; i++) { 
     int age = sc.nextInt(); 
     Person p = new Person(age); 
     p.amIOld(); 
     for (int j = 0; j < 3; j++) { 
      p.yearPasses(); 
     } 
     p.amIOld(); 
     System.out.println(); 
    } 
    sc.close(); 
    } 
} 

在上面的代碼中,當使用參數創建person類的實例時,它會自動調用類中的Person方法嗎?名稱與其類相同的方法?

是代碼 Person p = new Person(age); 構造函數或方法調用?這是兩個嗎? 使用與班級同名的方法的目的是什麼? 它的功能如何?

+7

查找 「構造」。這就是這個「方法具有相同的名稱作爲類」是。 –

+0

從概念上講,構造函數不是方法,即使它看起來很像一個。想想一個構造函數的代碼塊特殊的被稱爲初始化一個新的對象。 – Jesper

回答

0

我個人不喜歡用相同的南與類它是一種方法,但:

public class Person { 
    public Person() {} // constructor 
    public void Person {} // method 
    public static void main(String... args) { 
    Person p = new Person(); 
    p.Person(); 
    } 
} 

一般來說,一個類的名字應該是一個名詞,而方法的名稱應是動詞

0

這就是所謂的構造函數。當你創建一個新的實例這樣它axecuted:Person me = new Person(99); 可以有多個構造函數,如:

enter code here 
public Person(int initialAge) { 
    if (initialAge<= 0) { 
     System.out.println("Age is not valid, setting age to 0."); 
    } 
    else { 
     age = initialAge; 
    } 
} 

public Person() { 
    age = 0; 
} 
在同一類

。在這種情況下,如果你創建這樣一個實例:是這樣的:Person me = new Person(99);只有第一個構造函數被調用,如果你創建一個實例是這樣的:Person me = new Person();只有第二個(除非你有一個引用其他課程)。

-1

當創建一個類的一個對象,則執行

  1. 靜態塊第一
  2. 構造如果沒有定義,則默認的構造
  3. 稱爲與該對象,該對象的方法之後。

構造並不像它沒有type.If的類,那麼默認的構造函數會被自動調用沒有定義構造函數任何回報的方法。 否則特定的構造函數將在創建對象時被調用。

0

命名,如類和沒有空隙或任何returntyp一個mehtode被稱爲構造。

class MyClass { 
    public MyClass() { 
     //constructor code here 
    } 
} 

每個CALSS有默認構造(以上showen)女巫不必被expilcitly寫出到被使用(由編譯器產生的),但它可以被overritten和重載更多functunallity。 每次生成類的對象時都會調用構造函數。

class Person { 
    pulbic int age; 

    public Person (int age) { 
     this.age = age; 
    } 


    } 

class Main { 
    public static void main(String args[]) { 
     //creating 2 objects of person 
     Person p1 = new Person(23) 
     Person p2 = new Person(51) 
    } 
} 

這PROGRAMM現在產生的人2個物體objct P1的年齡變量包含值23和objct P2的年齡變量持有51 的年齡被稱爲與構造函數設置的值new Person(int age)陳述。

如果您還沒有初始化的類vriable(在這種情況下int age),但在構造函數中設置的值,它是由班上的其他處理,就好像它是用特定值初始化。

你可以強制與構造程序員在其創建設置一個類的具體attributs,可以therfor確保這個值設置。

如果你重載構造函數可以調用另一個構造有:

this(/*Overloaded parameters*/); 

構造函數也不能調用類的任何methodes(exept其他構造函數),因爲該類不完全產生的時間構造在跑。

相關問題