---
# System prepended metadata

title: 096  Pen.java + PenTest.java

---

# 096  Pen.java + PenTest.java
Pen.java
package ch07;
/*
 * 設計一個筆的類別，搭配PenTest.java一起參考使用
 * 此為物件導向設計之基礎
 */
public class Pen {
	//實體變數，需透過物件參考變數進行操作
	String brand;
	double price;
	//方法成員，需透過物件參考變數進行呼叫
	public void showInfo() {
		System.out.println("牌子為：" + brand);
		System.out.println("價格為：" + price);
	}
}

PenTest.java
package ch07;
/*
 * 此範例需搭配Pen.java一起參考使用
 * 暸解如何建立物件參考變數，並使用物件參考變數操作資料
 */
public class PenTest {

	public static void main(String[] args) {
		Pen myPen = new Pen();
		myPen.brand = "SKB";
		myPen.price = 10;
		
		Pen yourPen = new Pen();
		yourPen.brand = "MontBlanc";
		yourPen.price = 12000;
		
		myPen.showInfo();
		yourPen.showInfo();
	}

}
