[英]Methods vs Constructors in Java
我剛剛開始使用Java進行編程。 在談論方法和構造函數時,我們缺少使用的文字。 我不確定到底是什么方法或構造函數以及什么使每個方法唯一。 有人可以幫我定義它們並區分兩者嗎?
構造函數與方法之間的重要區別在於,構造函數初始化使用new
運算符創建的對象,而方法對已存在的對象執行操作。
構造函數不能直接調用; 當new
關鍵字創建對象時,將隱式調用它們。 可以直接在已使用new
創建的對象上調用方法。
構造函數和方法的定義在代碼中看起來相似。 它們可以采用參數,可以具有修飾符(例如public
),並且在括號中具有方法主體。
構造函數的名稱必須與類名稱相同。 他們不能返回任何東西,甚至void
(對象本身是隱含的回報)。
盡管必須為void
,但必須聲明方法以返回某些內容。
主要區別是
1. 構造函數用於初始化對象的狀態,其中方法公開對象的行為。
2. 構造函數不得具有返回類型,而as 方法必須具有返回類型。
3. 構造函數名稱與類名稱相同,其中as 方法可能相同,也可能不同。
4. 構造函數隱式調用,而方法顯式調用。
5. 構造函數編譯器提供默認的構造函數,而方法編譯器則不提供。
其他講師和助教有時會告訴我構造函數是專門的方法。 我一直認為Java構造函數不是專門的方法 。
如果構造函數完全是方法,我希望它們具有與方法相同的功能。 至少它們之間的相似之處要多於不同之處。
構造函數必須使用new
運算符調用,而方法不能使用new
運算符調用。 相關:構造函數不能按名稱調用,而方法必須按名稱調用。
構造函數可能沒有返回類型,而方法必須具有返回類型。
如果方法與類具有相同的名稱,則它必須具有返回類型。 否則,它是一個構造函數。 在同一個類定義中可以有兩個MyClass()簽名,這些簽名被不同地對待,這一事實應使所有確信構造函數和方法是不同的實體:
public class MyClass { public MyClass() { } // constructor public String MyClass() { return "MyClass() method"; } // method }
構造函數可以初始化實例常量,而方法則不能。
公共和受保護的構造函數不被繼承,而公共和受保護的方法則被繼承。
構造函數可以調用父類或同一個類的構造函數,而方法不能調用super()或this()。
它們都有參數列表。
它們都具有代碼塊,當該代碼塊被直接調用(方法)或通過new
調用(構造函數)時將執行該代碼塊。
構造函數可以是: 私有的 , 受保護的 , 公共的 。
方法可以是: 私有 , 受保護 , 公共 , 抽象 , 靜態 , 最終 , 同步 , 本機 , strictfp 。
數據字段可以是: 私有 , 受保護 , 公共 , 靜態 , 最終 , 瞬態 , 易失 。
在Java中,構造函數的形式和功能與方法明顯不同。 因此,將它們稱為專用方法實際上會使新程序員更難學習差異。 它們比相似的東西有更多不同,學習不同的實體對於Java至關重要。
我確實認識到Java在這方面與其他語言(即C ++)不同,在C ++中,專用方法的概念起源於語言規則並受其支持。 但是,在Java中,構造函數根本不是方法,更不用說專門的方法了。
甚至javadoc都認識到構造函數和方法之間的差異遠大於相似之處。 並為構造函數提供了單獨的部分。
在Java中,您編寫的類是對象。 構造函數構造這些對象。 例如,如果我有一個像這樣的Apple.class
:
public class Apple {
//instance variables
String type; // macintosh, green, red, ...
/**
* This is the default constructor that gets called when you use
* Apple a = new Apple(); which creates an Apple object named a.
*/
public Apple() {
// in here you initialize instance variables, and sometimes but rarely
// do other functionality (at least with basic objects)
this.type = "macintosh"; // the 'this' keyword refers to 'this' object. so this.type refers to Apple's 'type' instance variable.
}
/**
* this is another constructor with a parameter. You can have more than one
* constructor as long as they have different parameters. It creates an Apple
* object when called using Apple a = new Apple("someAppleType");
*/
public Apple(String t) {
// when the constructor is called (i.e new Apple() ) this code is executed
this.type = t;
}
/**
* methods in a class are functions. They are whatever functionality needed
* for the object
*/
public String someAppleRelatedMethod(){
return "hello, Apple class!";
}
public static void main(String[] args) {
// construct an apple
Apple a = new Apple("green");
// 'a' is now an Apple object and has all the methods and
// variables of the Apple class.
// To use a method from 'a':
String temp = a.someAppleRelatedMethod();
System.out.println(temp);
System.out.println("a's type is " + a.type);
}
}
希望我在代碼的注釋中解釋了所有內容,但這是一個摘要:構造函數“構造”一個類類型的對象。 構造函數的名稱必須與類相同。 它們主要用於初始化實例變量。方法是對象的功能。
構造函數是一種特殊的方法,它允許您創建類的新實例。 它涉及初始化邏輯。
“方法”是“子例程”是“過程”是“函數”是“子程序”是...同樣的概念使用了許多不同的名稱,但基本上是可以命名的代碼段,調用”。 通常,代碼以某種方式整齊地打包,帶有某種“標頭”,給出其名稱和參數,以及一個“正文”,以BEGIN
& END
或{
& }
等開頭。
“構造器”是一種特殊形式的方法,其目的是初始化類或結構的實例。
在Java中,方法的標頭是<qualifiers> <return type> <method name> ( <parameter type 1> <parameter name 1>, <parameter type 2> <parameter name 2>, ...) <exceptions>
和一個方法主體用{}
括起來。
而且您可以從其他方法中得知構造函數,因為該構造函數具有其<method name>
的類名稱,並且沒有聲明的<return type>
。
(當然,在Java中,您可以使用new
運算符new <class name> ( <parameter list> )
創建一個新的類實例。)
差r
:
r
隱式調用while方法。 構造函數是用於初始化數據成員的特殊函數,其中方法是執行特定任務的函數。
構造函數名稱與類名稱相同,方法名稱可以相同也可以不相同,也可以為類名稱。
構造函數不允許任何返回類型,其中方法允許返回類型。
構造函數通常是Method 。
當我們創建使用new運算符的類的對象時,我們將調用一種稱為構造函數的特殊方法。
構造函數,用於執行實例變量的初始化。
public class Diff{
public Diff() { //same as class name so constructor
String A = "Local variable A in Constructor:";
System.out.println(A+ "Contructor Print me");
}
public void Print(){
String B = "Local variable B in Method";
System.out.println(B+ "Method print me");
}
public static void main(String args[]){
Diff ob = new Diff();
}
}
`
輸出:
構造函數:構造函數中的局部變量A
因此,僅在此處顯示構造方法Diff()語句,因為我們創建了Diff類對象。 在這種情況下,構造函數總是最先在此處實例化Class Diff()的Class。
通常,
構造函數設置功能。
一切都從這里開始,當我們在main方法構造函數中調用ob對象時,將使用該類並創建副本,並將其加載到“ Java虛擬機類加載器”中。
這個類加載器將這個副本加載到內存中,因此我們現在可以通過引用來使用它。
構造函數完成其工作,然后Method並完成其實際實現。
在這個程序中,當我們調用
ob.print();
然后方法就會來。
謝謝
阿林丹
主要區別如下:
1:構造函數必須與類名具有相同的名稱,而方法不是這種情況
class Calendar{
int year = 0;
int month= 0;
//constructor
public Calendar(int year, int month){
this.year = year;
this.month = month;
System.out.println("Demo Constructor");
}
//Method
public void Display(){
System.out.println("Demo method");
}
}
2:構造函數初始化一個類的對象,而方法則不初始化。 方法對已經存在的對象執行操作。 換句話說,要調用一個方法,我們需要一個類的對象。
public class Program {
public static void main(String[] args) {
//constructor will be called on object creation
Calendar ins = new Calendar(25, 5);
//Methods will be called on object created
ins.Display();
}
}
3:構造函數沒有返回類型,但是方法必須具有返回類型
class Calendar{
//constructor – no return type
public Calendar(int year, int month){
}
//Method have void return type
public void Display(){
System.out.println("Demo method");
}
}
這是Java中的構造函數和方法之間的一些主要關鍵區別
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.