【本節目标】
通過閱讀本節内容,你将對如何将客觀事物抽象為Java類有更加深刻的了解,并熟練掌握Java類各種屬性、方法的編寫與相關關鍵字的運用。
初期的最可靠的也是最簡單的分析依據:簡單Java類。
案例分析一(Address)
編寫并測試一個代表位址的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 ;
}
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 void getCountry() {
return this.country ;
}
public void getProvince() {
return this.province ;
}
public void getCity() {
return this.city ;
}
public void getStreet() {
return this.street ;
}
public void getZipcode() {
return this.zipcode ;
}
}
public class JavaDemo {
public static void main(String args[]) {
System.out.println(new Address(“中華人民共和國”,”北京”,”北京”,”天安門街道”,”10001”).getInfo()) ;
}
}

圖一 執行結果一
案例分析二(Employee)
定義并測試一個代表員工的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 ;
}
public double salaryIncValue() { //得到薪水增長額度
return this.salary * this.rate ;
}
public double salaryIncResult() {
this.salary = this.salary * (1 + this.rate) ;
rutern salary ;
}
//setter、getter略
public String getInfo () {
return “雇員編号:” + this.empno + “、雇員姓名:” + this.ename + “、基本工資:” + this.salary + “、工資增長率:” +this.rate ;
}
}
public class JavaDemo {
public static void main(String args[]) {
Employee emp = new Employee(7369L, “史密斯”, 3000.0, 0.3) ;
System.out.println(emp.getInfo()) ;
System.out.println(“工資調整額度:” + emp. salaryIncValue()) ;
System.out.println(“上調後的工資:” + emp. salaryIncResult ()) ;
System.out.println(emp.getInfo()) ;
}
}
圖二 執行結果二
案例分析三(Dog)
設計一個Dog類,有顔色、名字、年齡等屬性,定義構造方法來初始化類的這些屬性,定義方法輸出Dog資訊,編寫應用程式使用Dog類。
class Dog {
private String name ;
private String color ;
private int age ;
public Dog() {}
public Dog(String name ,String color ,int age) {
this.name = name ;
this.color = color ;
this.age = age ;
}
//setter、getter略
public String getInfo() {
return “狗的名字:” + this.name + “、狗的顔色:” + this.color + “、狗的年齡:” + this.age ;
}
}
public class JavaDemo {
public static void main(String args[]) {
Dog dog = new Dog(“高高” ,”黑色” ,1) ;
System.out.println(dog.getInfo()) ;
}
}
圖三 執行結果三
案例分析四(Account)
構造一個銀行賬戶類,類的構成包括如下内容:
(1)資料成員使用者的賬戶名稱、使用者的賬戶餘額(private資料類型)。
(2)方法包括開戶(設定賬戶名稱及餘額),利用構造方法完成。
(3)查詢餘額。
class Account {
private String name ;
private double balance ;
public Account() { }
public Account(String name) {
this(name,0.0) ; //調用雙參構造
}
public Account(String name ,double balance) {
this.name = name ;
this.balance = balance ;
}
//setter、getter略
public double getBalance() {
return this.balance ;
}
public String getInfo() {
return “賬戶名稱:” + this.name + ,”、餘額:” + this.balance ;
}
}
public class JavaDemo {
public static void main(String args[]) {
Account account = new Account(“啊嘟嘟” ,9000000.00) ;
System.out.println(account.getInfo()) ;
System.out.println(account.getBalance ()) ;
}
}
圖四 執行結果四
案例分析五(User)
設計一個表示使用者的user類,類中的變量有使用者名、密碼和記錄使用者個數的變量,定義類的3個構造方法(無參、為使用者名指派、為使用者名和密碼指派)、擷取和設定密碼的方法和傳回類資訊的方法。
在簡單Java類的定義裡面追加有static統計操作即可。
class User {
private String uid ;
private String password ;
private static int count = 0 ;
public User() {
this(“NOID” ,”mldn”) ;
}
public User(String uid) {
this(uid ,”mldnjava”) ;
}
public User(String uid ,String password) {
this.uid = uid ;
this.password = password ;
count++ ; //個數追加
}
public static int getCount() { //擷取使用者個數
return count ;
}
//setter、getter略
public String getInfo() {
return “使用者名:” + this.uid + ,”、密碼:” + this.password ;
}
}
public class JavaDemo {
public static void main(String args[]) {
User userA = new User() ;
User userB= new User(“小強”) ;
User userC= new User(“大強” ,”我不行”) ;
System.out.println(userA.getInfo()) ;
System.out.println(userB.getInfo()) ;
System.out.println(userC.getInfo()) ;
System.out.println(“使用者的個數:” + User.getCount()) ;
}
}
圖五 執行結果五
案例分析六(Book)
聲明一個圖書類,其資料成員為書名、編号(利用靜态變量實作自動編号)、書價,并擁有靜态資料成員冊數、記錄圖書總冊數,在構造方法中利用此靜态變量為對象的編号指派,在主方法中定義多個對象,并求出總冊數。
class Book {
private int bid ; //編号
private String title ; //書名
private double price ; //書價
private static int count = 0 ;
public Book(String title ,double price) {
this.bid = count + 1 ; //先指派再進行count的自增
this.title = title ;
this.price = price ;
count ++ ;
}
//setter、getter略
public String getInfo() {
return “圖書編号:” + this.bid + ,”、名稱:”+ this.title + ,”、價格:” +this.price ;
}
public static int getCount() {
return count ;
}
}
public class JavaDemo {
public static void main(String args[]) {
Book b1 = new Book(“Java” ,89.2) ;
Book b2 = new Book(“Oracle” ,79.2) ;
System.out.println(b1.getInfo()) ;
System.out.println(b2.getInfo()) ;
System.out.println(“圖書總冊數:” + Book.getCount()) ;
}
}
圖六 執行結果六
在面向對象最基礎的開發中,簡單Java類是解決先期設計最好的方案。
想學習更多的Java的課程嗎?從小白到大神,從入門到精通,更多精彩不容錯過!免費為您提供更多的學習資源。
本内容視訊來源于
阿裡雲大學 下一篇:初識“資料巨輪”:數組 | 帶你學《Java面向對象程式設計》之十四 更多Java面向對象程式設計文章檢視此處