Java 中的建構函式方法 I 建構函式重載
來源:Objectientedprogramming77 什麼是建構子?這是一種特殊類型的方法,其名稱與類別的名稱相同,它決定了物件創建時如何初始化。 和其他方法一樣,我們也可以在Java程式中定義一個Constructor方法,但是與其他方法不同的是,我們不能直接呼叫Constructor;Java 在建立物件時自動呼叫建構函式。當我們使用new關鍵字建立類別的物件時,Java做了三件事:- 為對象分配記憶體。
- 使用初始值或預設值初始化此物件實例變數。
- 呼叫類別的 Method 建構函數。
基本建構函數
根據定義,構造函數看起來像常規方法,但有兩個主要差異。- 建構函數和類別名稱始終相同。
- 它沒有返回類型
class Person
{
String name;
int age;
Person (String n, int a)
{
name = n;
age = a;
}
void printPerson ()
{
System.out.print("Hi, I am " +name);
System.out.println(" I am "+ age + " years old.");
}
public static void main(String args[])
{
Person p;
p = new Person ("Ajab", 20);
p.printPerson();
p = new Person ("Rizwan", 30);
p.printPerson();
我們得到輸出:
嗨,我是阿賈布。我20歲了。嗨,我是裡茲萬。我今年30歲
構造函數重載
與其他方法一樣,建構函式也可以接受不同數量和類型的參數,允許建立具有指定屬性的對象,或允許建構函式根據不同類型的輸入計算屬性。例如,給定表中的MyRectone類別會建立一個MyRectone建構子並傳遞不同的參數,而不是為給定參數建立不同的方法。class MyRectone
{
int x1 = 0;
int y1 = 0;
int x2 = 0;
int y2 = 0;
MyRectone ( int x1, int x2, int x2, int y2)
{
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
MyRectone (Point topLeft, Point bottomRight)
{
x1 = topLeft.x;
y1 = topLeft.y;
x2 = bottomRight.x;
y2 = bottomRight.y;
}
MyRectone ( Point topLeft, int w, int h)
{
x1 = topLeft.x;
y1 = top left.y;
x2 = ( x1 + w);
y2 = (y1 + h);
}
void printRect ()
{
System.out.print ("MyRectone: <" + x1 + ", " + y1);
system.out.println (", " + x2 + " ,"+ y2 + ">");
}
public static void main (String args [] )
{
MyRectone rect;
System.out.println ("Calling MyRectone with coordinates 35,35 70,70");
rect = new MyRectone (35,35,70,70);
rect.printRect();
System.out.println ("Calling MyRectone with coordinates (15,15) (30,30)");
rect = new MyRectone (15,15,30,30);
rect.printRect();
System.out.print (" Calling buildRect w/1 point (10,10),");
System.out.println ("width (50) and height (50)");
rect = new MyRectone ( new Point (10,10), 50, 50);
rect.printRect();
結論:
使用座標35,35 70,70 呼叫MyRectone:MyRectone:<35,35,70,70> 呼叫w/1 點(15,15)、(30,30) 的buildRect:MyRectone:<15,15,30, 30 > 呼叫 buildRect w/1 點 (10,10)、寬度 (50) 和高度 (50):MyRectone:<10,10,50,50>
呼叫另一個構造函數
建構函數可以是類別中定義的另一個建構函數的超集;也就是說,它們的行為可以相同,但還要多一點。與其在類別中的多個建構函式方法中重複相同的行為,不如能夠簡單地從第二個建構函式的主體中呼叫第一個建構函式。Java 為此提供了一種特殊的語法。若要呼叫目前類別中定義的建構函數,請使用下列形式:this (arg1, arg2, arg3… ..);
當然, 這個 參數是建構函數的參數。
抽象 Java 類別和接口
來源:Dev.to 抽象類別是具有一些沒有完整定義的方法並且具有abstract修飾符的類別。- 您不能使用抽象類別的建構函式建立物件。
- 您可以使用抽象類別作為基底類別來定義衍生類別。
- 抽象類別至少有一個抽象方法。
- 抽象方法:與常規方法一樣有一個標頭,但沒有主體,並且需要abstract修飾符和分號。
- 抽象方法不能是私有的。
- 抽象類別可以是一種類型。
public abstract class Example{
private String data1;
private double data2;
public abstract double getPay();
}
介面:定義任何實作此介面的類別都必須具有的一組方法。
- 接口是一種類型。
- 它包含沒有定義和實例變數的方法頭:
public interface Interface1{
public void method1();
public int method2();
}
要實作接口,類別必須做兩件事:
- 包括實作 InterfaceName。
- 該類別必須實作介面中列出的所有方法頭。
public class Implementer implements Interface1 {
@Override
public void method1() {
//definition
}
@Override
public int method2() {
//definition
}
}
- 方法頭聲明為public。
- 抽象類別也可以實作接口,該類別為接口中的一些方法頭提供定義。
- Java介面還可以包含常數,例如:
public interface Constant {
public static final int JANUARY = 1, FEBRUARY = 2, MARCH = 3;
}
- 任何實作Constant介面的類別都會自動擁有這些常數,例如:
public class Constants implements Constant {
public static void main(String[] args) {
System.out.println(JANUARY);
}
}
- 您可以將常數和方法頭包含到單一介面中來混合使用介面。
- Java不支援多重繼承,因此一個類別只能擴展一個基底類別。但是,使用接口,一個類別可以實現多個接口:
public class Implementer implements Interface1, Interface2, .. InterfaceN{
}
- 之所以一個Java類只能擴展一個基類,是因為如果Java允許有兩個基類,那麼兩個類可能有相同的方法頭,但定義不同,導致不一致。
- 如果定義兩個具有相同名稱和不同值的常數,兩個介面可能不相容:
public interface Interface1{
public static final int ANSWEAR = 0;
}
public interface Interface1{
public static final int ANSWEAR = 3;
}
GO TO FULL VERSION