Java實驗報告
?
實驗二 Java簡單類與對象
一、?實驗目的
(1)?掌握類的定義,熟悉屬性、構造函數、方法的作用,掌握用類作為類型聲明變量和方法返回值;
(2)?理解類和對象的區別,掌握構造函數的使用,熟悉通過對象名引用實例的方法和屬性;
(3)?理解static修飾付對類、類成員變量及類方法的影響。
二、?實驗內容
- 寫一個名為Rectangle的類表示矩形。其屬性包括寬width、高height和顏色color,width和height都是double型的,而color則是String類型的。要求該類具有:
(1) 使用構造函數完成各屬性的初始賦值
(2) 使用get…()和set…()的形式完成屬性的訪問及修改
(3) 提供計算面積的getArea()方法和計算周長的getLength()方法
- 銀行的賬戶記錄Account有賬戶的唯一性標識(11個長度的字符和數字的組合),用戶的姓名,開戶日期,賬戶密碼(六位的數字,可以用0開頭),當前的余額。銀行規定新開一個賬戶時,銀行方面提供一個標識符、賬戶初始密碼123456,客戶提供姓名,開戶時客戶可以直接存入一筆初始賬戶金額,不提供時初始余額為0。定義該類,并要求該類提供如下方法:存款、取款、變更密碼、可以分別查詢賬戶的標識、姓名、開戶日期、當前余額等信息。
?
三、?實驗過程(請自己調整格式)
?
1 class Main 2 { 3 private double width,heigh; 4 private String color; 5 6 public Rectangle(double width,double heigh,String color) 7 { 8 this.setWidth(width); 9 this.setHeigh(heigh); 10 this.setColor(color); 11 } 12 public double getWidth() 13 { 14 return width; 15 } 16 public void setWidth(double width) 17 { 18 this.width=width; 19 } 20 public double getHeigh() 21 { 22 return heigh; 23 } 24 public void setHeigh(double heigh) 25 { 26 this.heigh=heigh; 27 } 28 public String getColor() 29 { 30 return color; 31 } 32 public void setColor(String color) 33 { 34 this.color=color; 35 } 36 public double getArea() 37 { 38 return this.width*this.heigh; 39 } 40 public double getLength() 41 { 42 return 2*this.width+2*this.heigh; 43 } 44 } 45 public class Rectangle 46 { 47 public static void main(String args[]) 48 { 49 Rectangle i = null; 50 i = new Rectangle(3,4,"red"); 51 System.out.println("矩形的顏色為:"+i.getColor()); 52 System.out.println("矩陣的周長為:"+i.getLength()); 53 System.out.println("矩陣的面積為:"+i.getArea()); 54 } 55 }
?
?
?
?
?
?第二題我盡力了,只能寫到這了,真心不會了
?
1 public class Account 2 { 3 private String id; 4 private String name; 5 private Date date; 6 private int password; 7 private int money; 8 9 public Account(String id, String name, int money) 10 { 11 super(); 12 this.id = id; 13 this.name = name; 14 this.money = money; 15 this.date = new Date(); 16 this.password = 123456; 17 } 18 19 public String getId() 20 { 21 return id; 22 } 23 24 public void setId(String id) 25 { 26 this.id = id; 27 } 28 29 public String getName() 30 { 31 return name; 32 } 33 34 public void setName(String name) 35 { 36 this.name = name; 37 } 38 39 public void jin(int num) 40 { 41 this.money = this.money+num; 42 } 43 44 public void chu(int num) 45 { 46 this.money = this.money-num; 47 } 48 49 public Date getDate() 50 { 51 return date; 52 } 53 54 public void setDate(Date date) 55 { 56 this.date = date; 57 } 58 59 public int getMoney() 60 { 61 return money; 62 } 63 64 public void setMoney(int money) 65 { 66 this.money = money; 67 } 68 69 public int getPassword() 70 { 71 return password; 72 } 73 74 public void setPassword(int password) 75 { 76 this.password = password; 77 }
?
?
?
?
?
四、?總結:
String 類兩種實例方法區別:
?
(1)直接賦值:如String name = "lichen";
。
?
(2)利用new
開辟一個新的空間:如String name = new String("lichen");
。
一個字符串就是一個String類的匿名對象。
匿名對象就是已經開辟了堆內存空間的并可以直接使用的對象
對象數組在使用前必須注意:
數組一定要先開辟空間;并且數組里面的每一個對象都是null值,所以在使用的時候數組中的每一個對象必須分別進行實例化。
包的概念:
包的概念,包是在使用多個類或接口時,為了避免名稱重復而采用的一種措施。如果使用,直接在程序中加入package
關鍵字即可。
import語句:
?
(1)定義,如果將幾個類存放在不同的包中,則在使用類的時候就必須通過import
語句導入。
(2)格式,import 包名稱.子包名稱.類名稱;
(手工導入所需要的類),import 包名稱.子包名稱.*;
(由JVM自動加載所需要的類)。
?