Java第五次作業--面向對象進階特性(抽象類和接口)
(一)學習總結
1.在上周完成的思維導圖基礎上,補充本周的學習内容,對Java面向對象程式設計的知識點做一個全面的總結。
2.汽車租賃公司,出租汽車種類有客車、貨車和皮卡三種,每輛汽車除了具有編号、名稱、租金三個基本屬性之外,客車有載客量,貨車有載貨量,皮卡則同時具有載客量和載貨量。用面向對象程式設計思想分析上述問題,将其表示成合适的類、抽象類或接口,說明設計思路并畫出類圖。
-
設計思路:
設計兩個接口:分别為載客(載客量方法)和載貨(載貨量方法)
設計一個汽車類的抽象方法,具有編号,名稱,租金三種屬性。
設計客車,貨車,皮卡三種類繼承抽象類
接口:客車->載客 貨車->載貨 皮卡->載客和載貨
- 類圖:
3.閱讀下面程式,分析代碼是否能編譯通過,如果不能,說明原因,并進行改正。如果能,列出運作結果
interface Animal{
void breathe();
void run();
void eat();
}
class Dog implements Animal{
public void breathe(){
System.out.println("I\'m breathing");
}
void eat(){
System.out.println("I\'m eating");
}
}
public class Test{
public static void main(String[] args){
Dog dog = new Dog();
dog.breathe();
dog.eat();
}
}
不能通過,Dog類繼承Animal接口,Dog類必須實作接口的抽象所有方法
修改後的代碼為:
interface Animal{
void breathe();
void run();
void eat();
}
class Dog implements Animal{
public void breathe(){
System.out.println("I\'m breathing");
}
public void eat(){
System.out.println("I\'m eating");
}
public void run() {
System.out.println("I\'m running");
}
}
public class test04{
public static void main(String[] args){
Dog dog = new Dog();
dog.breathe();
dog.eat();
dog.run();
}
}
運作結果為:
4.運作下面的程式
import java.util.Arrays;
public class Test{
public static void main(String[] args){
String[] fruits = {"peach","banana","orange","apple"};
Arrays.sort(fruits);
for(int i = 0;i < fruits.length;i++)
{
System.out.println(fruits[i]);
}
}
}
程式輸出的結果是升序排序的。檢視String 類的源碼,說明是如何實作的?如果現在希望對輸出的結果進行降序排序,該如何處理?修改上述代碼,實作按照字母順序逆序排序。、
String類的源代碼:
public final class String
implements java.io.Serializable, Comparable<String>, CharSequence
{
/** The value is used for character storage. */
private final char value[];
/** The offset is the first index of the storage that is used. */
private final int offset;
/** The count is the number of characters in the String. */
private final int count;
/** Cache the hash code for the string */
private int hash; // Default to 0
/** use serialVersionUID from JDK 1.0.2 for interoperability */
private static final long serialVersionUID = -6849794470754667710L;
/**
* Class String is special cased within the Serialization Stream Protocol.
*
* A String instance is written initially into an ObjectOutputStream in the
* following format:
* <pre>
* <code>TC_STRING</code> (utf String)
* </pre>
* The String is written by method <code>DataOutput.writeUTF</code>.
* A new handle is generated to refer to all future references to the
* string instance within the stream.
*/
private static final ObjectStreamField[] serialPersistentFields =
new ObjectStreamField[0];
/**
* Initializes a newly created {@code String} object so that it represents
* an empty character sequence. Note that use of this constructor is
* unnecessary since Strings are immutable.
*/
public String() {
this.offset = 0;
this.count = 0;
this.value = new char[0];
}
/**
* Initializes a newly created {@code String} object so that it represents
* the same sequence of characters as the argument; in other words, the
* newly created string is a copy of the argument string. Unless an
* explicit copy of {@code original} is needed, use of this constructor is
* unnecessary since Strings are immutable.
*
* @param original
* A {@code String}
*/
public String(String original) {
int size = original.count;
char[] originalValue = original.value;
char[] v;
if (originalValue.length > size) {
// The array representing the String is bigger than the new
// String itself. Perhaps this constructor is being called
// in order to trim the baggage, so make a copy of the array.
int off = original.offset;
v = Arrays.copyOfRange(originalValue, off, off+size);
} else {
// The array representing the String is the same
// size as the String, so no point in making a copy.
v = originalValue;
}
this.offset = 0;
this.count = size;
this.value = v;
}
逆序排序:
import java.util.Arrays;
import java.util.Comparator;
public class Test {
public static void main(String[] args) {
String[] fruits = { "peach", "banana", "orange", "apple" };
Arrays.sort(fruits, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
char a = o1.charAt(0);
char b = o2.charAt(0);
if(o1.equals(o2)){
return 0;
}else if(a>b){
return -1;
}else{
return 1;
}
}
});
for (int i = 0; i < fruits.length; i++) {
System.out.println(fruits[i]);
}
}
}
(二)實驗總結
1.某工廠生産各種音樂盒,客戶無需知道音樂盒的制作過程,隻需知道如何播放音樂盒即可。用簡單工廠設計模式實作該過程:接口MusicBox具有方法play(),兩個音樂盒類PianoBox,ViolinBox,MusicBoxFactory 産生MusicBox的執行個體。
-
程式設計思路:
建立MusicBox接口
建立PianoBox和ViolinBox兩個類,繼承MusicBox接口
建立Factory實作功能
-
實驗問題分析:
問題1:如何用工廠方法優化代碼
解決方案:
class Factory { public static MusicBox getInstance (String className){ MusicBox f = null ; if("PianoBox".equals(className)){ f = new PianoBox(); } if("ViolinBox".equals(className)){ f = new ViolinBox(); } return f ; } }
2.修改第三次作業的第一題,使用java.util.Date類表示職工的生日和參加工作時間,并将職工資訊按照生日大小排序後輸出。(分别用comparable和comparator實作)
-
程式設計思路:
員工自動配置設定部門,是以排序時應該在部門中進行排序。
解決方法:将員工分成兩個數組,再對其進行分别排序
實作代碼:
Employee [] E=new Employee [5];
Employee [] e=new Employee [5];
Scanner in = new Scanner(System.in);
for(int i=0;i<E.length;i++){
System.out.println("請輸入第"+(i+1)+"員工的資訊");
System.out.println("輸入員工号:");
int number=in.nextInt();
System.out.println("輸入員工姓名");
String name=in.next();
System.out.println("輸入員工性别");
String sex=in.next();
System.out.println("輸入員工生日");
String str1=In.next();
birthday1[i]=sdf1.parse(str1);
System.out.println("輸入員工工作日期");
String str2=In.next();
worktime1[i]=sdf1.parse(str2);
E[i]=new Employee(number,name,sex,birthday1[i],worktime1[i]);
}
Arrays.sort(E);
for(int i=0;i<e.length;i++){
System.out.println("請輸入第"+(i+6)+"員工的資訊");
System.out.println("輸入員工号:");
int number=in.nextInt();
System.out.println("輸入員工姓名");
String name=in.next();
System.out.println("輸入員工性别");
String sex=in.next();
System.out.println("輸入員工生日");
String str1=In.next();
birthday2[i]=sdf1.parse(str1);
System.out.println("輸入員工工作日期");
String str2=In.next();
worktime2[i]=sdf1.parse(str2);
e[i]=new Employee(number,name,sex,birthday2[i],worktime2[i]);
}
Arrays.sort(e);
3.在案例寵物商店的基礎上,實作以下功能:
(1)展示所有寵物
(2)購買寵物
(3)顯示購買清單
-
程式設計思路:
建立Pet接口
建立cat和dog兩個類,繼承Pet接口
使用者選購的時候,根據編号,運用get方法的到相關寵物的資訊
-
實驗問題分析:
問題1:最後輸出購物清單,無法一起輸出
解決方案:設定一個數組接受使用者購買的所有寵物的編号,在進行周遊,對其輸出
實作代碼:
System.out.println("***************************************"); System.out.println(" 購物清單"); for(int i=0;i<5;i++){ findcat(C[i],c); } for(int i=0;i<5;i++){ finddog(D[i],d); } System.out.println("共選購貓"+flagc+"隻狗"+flagd+"隻"); System.out.println("您購買寵物貓共花費了:"+catmoney+" 您購買寵物狗共花費了:"+dogmoney+" 您總共花費了:"+(dogmoney+catmoney));
(三)代碼托管
- 碼雲commit曆史截圖