第十章 物件導向思考

2022-09-10 20:45:15 字數 4568 閱讀 3770

程式清單10-1

testloanclass.j**a

import j**a.util.scanner;

public class testloanclass

}

程式清單10-2

loan.j**a

public class loan 

/*** construct a loan with specified annual interest rate,

* number of years, and loan amount

*/public loan(double annualinterestrate, int numberofyears, double loanamount)

/*** return annualinterestrate

*/public double getannualinterestrate()

/*** set a new annualinterestrate

*/public void setannualinterestrate(double annualinterestrate)

/*** return numberofyears

*/public int getnumberofyears()

/*** set a new numberofyears

*/public void setnumberofyears(int numberofyears)

/*** return loanamount

*/public double getloanamount()

/*** set a new loanamount

*/public void setloanamount(double loanamount)

/*** find monthly payment

*/public double getmonthlypayment()

/*** find total payment

*/public double gettotalpayment()

/*** return loan data

*/public j**a.util.date getloandate()

}

從現在開始的所有例子,在將注意力放在它的實現上之前,你都可以先在這個類中建立乙個物件,並且嘗試使用它的方法

程式清單10-3

usebmiclass.j**a

public class usebmiclass 

}

程式清單10-4

bmi.j**a

public class bmi 

public bmi(string name , double weight, double height)

public double getbmi()

public string getstatus() else if (bmi < 25) else if (bmi < 30) else

}public string getname()

public int getage()

public double getweight()

public double getheight()

}

程式清單 10-5

testcourse.j**a

public class testcourse 

system.out.println();

system.out.print("number of students in course2: "

+ course2.getnumberofstudents());

}}

程式清單 10-6

course.j**a

public class course 

public void addstudent(string student)

public string getstudents()

public int getnumberofstudents()

public string getcoursename()

public void dropstudent(string student)

}

程式清單 10-7

teststackofintegers.j**a

public class teststackofintegers 

while (!stack.empty())

}}

程式清單 10-8

stackofintegers.j**a

public class stackofintegers 

/*** construct a stack with the specified maximum capacity

*/public stackofintegers(int capacity)

/*** push a new integer to the top of the stack

*/public void push(int value)

elements[size++] = value;

}/**

* return and remove the top element from the stack

*/public int pop()

/*** return the top element from the stack

*/public int peek()

/*** test whether the stack is empty

*/public boolean empty()

/*** return the number of elements in the stack

*/public int getsize()

}

程式清單 10-9

stackofintegers.j**a

import j**a.math.biginteger;

import j**a.util.scanner;

public class largefactorial

public static biginteger factorial(long n)

return result;

}}

例如,下面的語句

stringbuilder stringbuilder = stringbuilder.reverse();

將構建器中的字元倒置並把構建器的引用複製給stringbuilder1。這樣,stringbuilder 和stringbuilder1 都指向同乙個stringbuffer 物件。回顧一下,如果對方法的返回值不感興趣,也可以將帶返回值的方法作為語句呼叫。在這種情況下,j**a 就簡單地忽略掉返回值。例如,下面的語句

stringbuilder.reverse();

它的返回值就被忽略了。返回stringbuilder 的引用可以使得stringbuilder 方法形成呼叫鏈,如下所示:

stringbuilder.reverse().delete(8,11).replace(11,15,"html");

如果乙個字串不需要任何改變,則使用string 而不要使用stringbuilder。string 比stringbuilder 更加高效

完整的程式如程式清單10-10所示

程式清單 10-10

stackofintegers.j**a

package based_on_article.the_textbook_source_code.chapter10;

import j**a.util.scanner;

public class palindromeignorenonalphanumeric

/*** return true if a string is a palindrome

*/public static boolean ispalindrome(string s)

/*** create a new string by eliminating nonalphanumeric chars

*/public static string filter(string s)

}//return a new filtered string

return stringbuilder.tostring();

}/**

* create a new string by reversing a specified string

*/public static string reverse(string s)

}

第十章 物件導向

1.物件與類 在現實世界中,隨處可見的一種事物就是物件,物件是事物存在的實體,如學生 汽車等。人類解決問題的方式總是將複雜的事物簡單化,於是就會思考這些物件都是由哪些部分組成的。通常都會將物件劃分為兩個部分,即靜態部分與動態部分。2.類的定義 python使用class關鍵字來定義類 3.物件的建立...

第十章 物件和類

物件導向是一種程式設計風格,用於任何一種語言。過程性程式設計 考慮遵循的步驟,使用者希望有哪些功能,我如何提供,如何儲存資料。物件導向程式設計 首先考慮使用者,確定乙個物件的屬性和操作這些屬性的方法。使用者介面有哪些。相當於自己定製的乙個新的型別,這個型別的屬性和可以對這些屬性進行的操作 介面就是將...

第十章 物件和類

c 中,類由兩部分組成 1 類宣告。包括成員變數和成員函式。一般方法在標頭檔案中。2 類方法定義。包括成員函式的定義。一般放在實現源 中。控制訪問中有有兩個關鍵字 private和public 直接上圖 類物件可以直接訪問public成員 而private成員只能通過public方法來訪問。priv...