學習筆記
初期最可靠也是最簡單的分析依據:簡單Java類;
案例一
編寫并測試一個代表位址的address類,位址由國家、省份、城市、街道、郵編組成,并傳回完整資訊。
class Address{
private String country ;
private String province ;
private String city ;
private String street ;
private String zipcode ;
// 構造方法
public Address(){}
public Address(String country, String province, String city, String street, String zipcode){
this.country = country ;
this.province = province ;
this.city = city ;
this.street = street ;
this.zipcode = zipcode ;
}
// stter getter
public String getInfo(){
return "國家:" + this.country +
"、省份:" + this.province +
"、城市:" + this.city +
"、街道:" + this.street +
"、郵編:" + this.zipcode ;
}
public void setCountry(String country){
this.country = country ;
}
public void setProvince(String province){
this.province = province ;
}
public void setCity(String city){
this.city = city ;
}
public void setStreet(String street){
this.street = street ;
}
public void setZipcode(String zipcode){
this.zipcode = zipcode ;
}
public String getCountry(){
return this.country ;
}
public String getProvince(){
return this.province ;
}
public String getCity(){
return this.city ;
}
public String getStreet(){
return this.street ;
}
public String getZipcode(){
return this.zipcode ;
}
}
public class JavaDemo{ // 主類
public static void main(String arges[]){
Address address = new Address("中國", "四川", "成都", "電子科大", "000001");
System.out.println(address.getInfo()) ;
}
}
案例二
定義并測試一個代表員工的Employee類。員工屬性包括“編号”、“姓名”、“基本工資”、“薪水增長率”,包括計算薪水增長額以及計算增長後的工資總額的操作方法。
這個程式的功能已經超過簡單Java類的範疇,因為簡單Java類中不包括複雜的邏輯,但是思考依然從簡單Java類開始。
class Employee{
private long empno ;
private String ename ;
private double salary;
private double rate ;
public Employee(){}
public Employee(long empno, String ename, double salary, double rate){
this.empno = empno ;
this.ename = ename ;
this.salary = salary ;
this.rate = rate ;
}
// setter getter略
public String getInfo(){
return "編号:" + empno +
"、姓名:" + ename +
"、工資:" + salary +
"、增長率:" + rate ;
}
public double salaryIncValue(){ //的到薪水增長額度
return this.salary * this.rate ;
}
public double salaryIncResult(){ // 薪水總額
this.salary = this.salary * (1 + this.rate) ;
return this.salary ;
}
}
public class JavaDemo{ // 主類
public static void main(String arges[]){
Employee employee = new Employee(001,"張三", 12000.00, 0.01) ;
System.out.println(employee.getInfo()) ;
System.out.println(employee.salaryIncValue()) ;
System.out.println(employee.salaryIncResult()) ;
System.out.println(employee.getInfo()) ;
}
}
案例三
構造一個銀行賬戶類,類的構成包含如下如下:
- 資料成員使用者的賬戶名稱、使用者賬戶餘額(private資料類型)。
- 方法包括開戶(設定賬戶名稱及餘額),利用構造方法完成。
- 查詢餘額。
class Acount{
private String ename ;
private double balance ;
public Acount(){}
public Acount(String ename){
this(ename, 0.0) ;
}
public Acount(String ename, double balance){
this.ename = ename ;
this.balance = balance ;
}
//setter getter略
public String checkBalance(){
return "賬戶姓名:" + this.ename +
"、賬戶餘額:" + this.balance ;
}
}
public class JavaDemo{ // 主類
public static void main(String arges[]){
Acount acount = new Acount("張三") ;
System.out.println(acount.checkBalance()) ;
}
}
案例四
設計一個表示使用者的user類,類中有使用者名、密碼、記錄使用者個數的變量,定義類的三個構造,擷取和設定密碼的方法和傳回類資訊。
方法一:使用靜态代碼塊
class User{
private String uid ;
private long password ;
private static int count = 1 ;
static{
count ++ ;
}
public User(){}
public User(String uid){
this(uid, 0);
}
public User(String uid, long password){
this.uid = uid ;
this.password = password ;
}
// setter getter 略
public static int getCount(){
return count ;
}
public String getInfo(){
return "使用者名:" + this.uid +
"、密碼:" + this.password +
"、已有使用者數目:" + count ;
}
public void setComand(long password){
this.password = password ;
}
}
public class JavaDemo{ // 主類
public static void main(String arges[]){
User user1 = new User() ;
User user2 = new User() ;
System.out.println(user2.getInfo() ) ;
}
}
方法二
class User{
private String uid ;
private String password ;
private static int count = 0 ;
public User(){
this("無名氏", "0000") ;
}
public User(String uid){
this(uid, "00000") ;
}
public User(String uid, String password){
this.uid = uid ;
this.password = password ;
count ++ ;
}
// setter getter 略
public static int getCount(){
return count ;
}
public String getInfo(){
return "使用者名:" + this.uid +
"、密碼:" + this.password +
"、已有使用者數目:" + count ;
}
public void setComand(String password){
this.password = password ;
}
}
public class JavaDemo{ // 主類
public static void main(String arges[]){
User user1 = new User() ;
User user2 = new User() ;
System.out.println(User.getCount() ) ;
}
}
案例五
聲明一個圖書類,其資料成員為書名、編号(使用靜态變量自動編号)、書價,并擁有靜态資料成員冊數、圖書總冊數,在構造方法中利用次靜态變量為對象指派,在主方法中定義多個對象,并求出總冊數。
class Book{
private int bid ; // 編号
private String title ; //書名
private double price ; // 價格
private static int count = 0 ;
public Book(){
this("未知", 0.0) ;
}
public Book(String title){
this(title, 0.0) ;
}
public Book(String title, double price){
this.bid = count ;
this.title = title ;
this.price = price ;
count ++ ;
}
// setter getter 略
public String getInfo(){
return "書名:" + this.title +
"、價格:" + this.price +
"、編号:" + this.bid ;
}
public static int getCount(){
return count ;
}
}
public class JavaDemo{ // 主類
public static void main(String arges[]){
Book book1 = new Book();
Book book2 = new Book("java",100.00) ;
Book book3 = new Book("JSP") ;
System.out.println(book1.getInfo()) ;
System.out.println(book2.getInfo()) ;
System.out.println(book3.getInfo()) ;
System.out.println("圖書總冊數:" + Book.getCount()) ;
}
}
在面向對象的基礎開發中,簡單Java類是解決先期設計的最好解決方案。