java - 类 - 对象示例

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

在本教程中,我们将在Java编程语言中编写代码以使用类和对象。

如果我们正在阅读本教程,那么假设我们知道以下几点。

  • 如何创建类

  • 如何在类中添加成员变量

  • 如何在类中添加方法

  • 如何创建类的对象

在以下示例中,我们正在使用packagesbox类来创建对象。

PackagingBox类具有以下成员变量。

|访问修改器|数据类型|变量|
| --- - | --- | - - |
| private | double | length |
| private | double | breadth |
| private | double | height |
| public | double | volume |
| | double | weight |
| | double | price |

它具有以下方法。

|访问修改器|返回类型|方法|参数|
| --- - | --- | - - | --- |
| public | void | setLength | double length |
| public | double | getLength | |
| public | void | setBreadth | double breadth |
| public | double | getBreadth | |
| public | void | setHeight | double height |
| public | double | getHeight | |
| public | void | setWeight | double weight |
| public | double | getWeight | |
| public | void | setPrice | double price |
| public | double | getPrice | |
| public | void | computeVolume | |
| public | double | getVolume | |

代码

/**
 * The PackagingBox Class
 */
class PackagingBox {
  //member variables
  private double length;
  private double breadth;
  private double height;
  public double volume;
  double weight;
  double price;
  //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();
    //setting the dimensions
    myBox.setLength(10);
    myBox.setBreadth(20);
    myBox.setHeight(30);
    //setting the weight
    myBox.setWeight(120);
    //setting the price
    myBox.setPrice(299);
    //compute the volume
    myBox.computeVolume();
    //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