Java-类-构造函数

时间:2020-02-23 14:36:25  来源:igfitidea点击:

在本教程中,我们将学习Java编程语言中的类的构造函数。

到目前为止,我们已经学习了如何创建类,如何在类内部添加成员变量以及如何在类中添加方法。

现在让我们谈谈构造函数。

什么是构造函数?

构造函数是类内部的一种特殊方法,其名称与类相同,并且有助于初始化类的对象。

构造函数没有返回类型,甚至没有" void"。
构造函数的隐式返回类型是类类型本身。

如果我们没有明确向类添加构造函数,则Java编译器会将默认构造函数添加到该类。

构造函数语法

class NameOfTheClass {

  //some member variables
  
  //constructor
  NameOfTheClass() {
    //some code goes here...
  }

  //some methods of the class
}

在下面的示例中,我们将创建一个Happy类,该类具有一个构造函数,该构造函数在创建该类的对象时会打印一条消息。

class Happy {

  //constructor
  Happy() {
    System.out.println("Hello from the Happy constructor.");
  }
}

//Our main class
class Example {
  public static void main(String[] args) {
    Happy obj = new Happy();
  }
}
Hello from the Happy constructor.

带参数的构造函数

class NameOfTheClass {
  
  //constructor with parameters
  NameOfTheClass(list_of_params) {
    //some code...
  }
}

在以下示例中,我们修改了上面的Happy类以采用参数。

class Happy {

  //constructor
  Happy(boolean isHappy) {
    if (isHappy == true) {
      System.out.println("I am happy :)");
    } else {
      System.out.println("I am sad :(");
    }
  }
}

//Our main class
class Example {
  public static void main(String[] args) {

    //code is not working...
    boolean amIHappy = false;
    System.out.println("First run: when code is not working...");
    Happy obj = new Happy(amIHappy);

    //after sometime... code is working...
    System.out.println("After sometime second run: when code is working...");
    amIHappy = true;
    Happy obj2 = new Happy(amIHappy);
  }
}
First run: when code is not working...
I am sad :(
After sometime second run: when code is working...
I am happy :)

因此,在上面的代码中,我们创建了带有参数的构造函数" Happy"。

然后,我们实例化Happy类的一个对象,并传递一个布尔型的amIHappy变量。

在下面的示例中,我们将修改先前教程中一直在使用的PackagingBox类。
我们将使用构造函数初始化对象。

/**
 * The PackagingBox Class
 */
class PackagingBox {

  //member variables
  private double length;
  private double breadth;
  private double height;
  public double volume;
  double weight;
  double price;
  
  //constructor
  PackagingBox(double length, double breadth, double height, double weight, double price) {
    
    //we are setting the length, breadth, height, weight, price of the box
    this.length = length;
    this.breadth = breadth;
    this.height = height;
    this.volume = volume;
    this.weight = weight;
    this.price = price;
    
    //compute the volume
    this.computeVolume();
  }

  //methods

  //---- get and set length

  public void setLength(double length) {
    this.length = length;
  }
  
  public double getLength() {
    return this.length;
  }

  //---- get and set breadth
  
  public void setBreadth(double breadth) {
    this.breadth = breadth;
  }
  
  public double getBreadth() {
    return this.breadth;
  }

  //---- get and set height
  
  public void setHeight(double height) {
    this.height = height;
  }
  
  public double getHeight() {
    return this.height;
  }

  //---- get and set weight
  
  public void setWeight(double weight) {
    this.weight = weight;
  }
  
  public double getWeight() {
    return this.weight;
  }

  //---- get and set price
  
  public void setPrice(double price) {
    this.price = price;
  }
  
  public double getPrice() {
    return this.price;
  }

  //---- compute and get volume

  public void computeVolume() {
    this.volume = this.length * this.breadth * this.height;
  }

  public double getVolume() {
    return this.volume;
  }
}

/**
 * The main class.
 */
class ObjectExample {
  
  public static void main(String[] args) {

    //creating an object of the class
    PackagingBox myBox = new PackagingBox(10, 20, 30, 120, 299);

    //get the values
    System.out.println("Dimension of the box:");
    System.out.println("Length: " + myBox.getLength());
    System.out.println("Breadth: " + myBox.getBreadth());
    System.out.println("Height: " + myBox.getHeight());

    System.out.println("Weight, Volume and Price of the box:");
    System.out.println("Weight: " + myBox.getWeight());
    System.out.println("Volume: " + myBox.getVolume());
    System.out.println("Price: " + myBox.getPrice());
  }
}
Dimension of the box:
Length: 10.0
Breadth: 20.0
Height: 30.0
Weight, Volume and Price of the box:
Weight: 120.0
Volume: 6000.0
Price: 299.0