- class Text_ShouJi { //定義類名為Text_ShouJi的主函數
- public static void main(String[] args) {
- ShouJi s = new ShouJi(); //創建新的物件,並把位址賦給s
- s.setBrand("三星"); //賦值
- s.setPrice(998); //賦值
- s.show();
- ShouJi s1 = new ShouJi("iphone6s",5588);
- s1.show();
- }
- }
- class ShouJi {
- private String brand; //定義兩個私有成員變數屬性
- private int price;
- public ShouJi() { //創建無參構造函數
- System.out.println("無參構造");
- }
-
- public ShouJi(String brand,int price) { //創建有參構造函數
- this.brand =brand;
- this.price = price ;
- }
-
-
- public void setBrand (String brand) { //設置品牌
- this.brand = brand;
- }
- public String getBrand () { //獲取品牌
- return brand;
- }
- public void setPrice (int price) { //設置價格
- if (price > 0) {
- this.price = price;
- } else {
- System.out.println("這樣價格的手機你給我一個唄");
- }
- }
- public int getPrice () { //獲取價格
- return price;
- }
- public void call() { //創建行為
- System.out.println("可以打電話");
- }
- public void massage() {
- System.out.println("可以發短信");
- }
- public void picther() {
- System.out.println("可以拍照片");
- }
-
- public void show(){
- System.out.println("手機型號為:"+brand+"..價格為: "+price);
- }
- }
复制代码 |