※J.Y Chen 的個人部落格 ※

Just Follow Our Heart And We will shine!

43 瀏覽人次

【程式語言】JAVA的第七堂課 |物件導向程式設計 (課堂練習)

Published: in JAVA by .

inclass.java

public class inclass
{
  public static void main(String[] arr)
  {
     box box1 = new box();
     box1.set(2,3,4);  //set w,l,h
     System.out.println("Box is "+box1.getW()+"X"+box1.getL()+"X"+box1.getH());
     System.out.println("the volume is "+box1.vol());
  }
}

box.java

class box {
	//data mamber
	private int height , width , length;
	//member function
	void set(int w , int l ,int h ){
		this.width = w;
		this.length = l;
		this.height = h;
	}
	int getW() {
		return width;
	}
	int getL() {
		return length;
	}
	int getH() {
		return height;
	}
	
	int vol() {
		return width*length*height;
	}
	
}

Result:

©2019 - 2024 Henry Chen