發表文章

目前顯示的是有「JAVA範例」標籤的文章

「Java」「abstract類別可以不實作介面方法」 Chapter5 Question5

Given: interface Car{ void brand(String s); } abstract class Test4 implements Car{ // insert code } Answer: 可以選擇不輸入 public void brand(String s){ } void brand() { } 但不能與實作介面的方法相同,以下會造成錯誤: void brand(String s){ }

「Java」「interface欄位與方法」 Chapter5 Question4

問題: which two forms of abstraction can a programmer use in java? 哪兩種抽象形式能在 Java程式中使用?? 選擇: enums 列舉 primitives 基本資料型別 abstract classes 抽象類別 concrete classes 實體類別 primitive wrappers 外包類別 interfaces 介面 Answer: 3 , 6 提要: Abstract classes :父類別在設計的時候,其主要目的若是要讓其他類別繼承,則我們通常稱這樣的祖先類別為abstract classes。關於這一點雖然課本上這樣解釋,但是實務上我們會說一個無法產生instance的class就稱之為abstract class。  Concrete class :和abstract classes相反,一個類別其設計之主要目的就是要讓人產生instance,則稱此類別為concrete class。實務上只要是能產生instance的類別都稱之為concrete classes。       Interfaces:     Clients remain unaware of the specific types of objects they use, as long as the objects adhere to the interface that clients expect.     Clients remain unaware of the classes that implement these objects. Clients only know about the abstract class(es) defining the interface.   This so greatly reduces implementation dependencies between subsystems that it leads to the following principle of reusable o...

「Java」「interface欄位與父類別欄位同名」 Chapter5 Question3

Given: interface Car{ String a = "Car"; public void detail(); } class SuperCar{ static String a = "SuperCar"; } public class Test4 extends SuperCar implements Car{ @Override public void detail() { System.out.println("轎車"); } public static void main(String[] args) { new Test4().detail(); System.out.println("a:"+a);//a欄位值模擬兩可 field a is ambiguous. System.out.println("a:"+SuperCar.a); System.out.println("a:"+Car.a); } } Answer: System.out.println("a:"+a); 造成編譯錯誤 原因: 介面與父類別有相同的變數名稱,會造成不知該抓取哪個值。 所以需要告知要抓取哪個變數。

「Java」「interface欄位與方法」 Chapter5 Question2

Given: interface Interface{ public abstract String AA; //錯誤,無給初始值 public abstract String getAA(); public static String BB; //錯誤,無給初始值 public String getBB(); private String CC="CCC"; //錯誤,修飾子只能是 public,static和final public static String getCC(); //錯誤,方法只能有public 或 abstract public String DD="DDD"; public abstract String getDD(); } Answer: public String DD="DDD"; public abstract String getDD(); 總整理: interface 欄位: 只能是 public, static 和 final interface 方法: 只能是 public 與 abstract

「Java」「abstract 與 interface」 Chapter5 Question1

Given these facts about java types in an application: Tips: -Type x is a template for other types in the application. ( X是模板並提供給其他使用。) -Type x implements doStuff(). (X可實作 doStuff()方法。 判斷: X不會是介面interface) -Type x declares, but does NOT implement  doIt(). (宣告X,但無法 實作 doIt() 方法。 判斷: X可能是 abstract 或 interface) -Type y declares doOther(). (Y 宣告 doOther() 方法。 判斷: X可能是抽象類別或介面) Which three are true? 1. Type y must be an interface. (Y必定是Interface介面) 2. Type x must be an abstract class. (X必定是抽象類別) 3. Type y could be an abstract class or an interface. (Y可以是抽象類別或介面) 4. Type x could be an abstract class or an interface. (X可以是抽象類別或介面) 5. Type x could implement or extend from Type y. (X可在 實作 或 繼承 Y) 6. Type y must be an abstract class. (Y必定是抽象類別) Answer: 2 , 3 , 5 Hint: 1. X 只有可能是 abstract 2. Y 有可能是 abstract 與 interface 3. abstract 可以在 繼承 abstract 與 實作 interface

「Java」「Inner Class」 Chapter4 Question17

有一台光陽機車,從A點到B點,總長度80公里。共花了10小時。請問機車的每小時速率多少? Given: class Bike{ int totalKm; Bike(int km){ this.totalKm=km; } public void Time(int hour) {//花費的時間 int hr = hour; class kymco{ int velocity=0; public void speedup() { velocity = totalKm/hr; System.out.println("(每小時多少公里)速率:"+velocity+"km/hr"); } } new kymco().speedup(); } } public class Test5 { public static void main(String[] args) { Bike bk = new Bike(80);//總長80公里 bk.Time(10);//花費10小時 } } Answer: 編譯成功,每小時速率 8 公里

「Java」「enum」 Chapter4 Question16

Given: enum TWMoney{ Ten(10),Hundred(100),Thousand(1000); private int worth; public TWMoney(int i) { this.worth = i; } public int getWorth() { return worth; } } public class Test5 { public static void main(String[] args) { TWMoney curr = new TWMoney.Hundred; System.out.println(curr.getWorth()); } } Answer: enum列舉中,建構子必須是private。 所以修改為: public TWMoney(int i) 要實作列舉中的欄位,不可用new 修改為:  TWMoney curr = TWMoney.Hundred;

「Java」「Interface」 Chapter4 Question15

Given: interface Actable{ public void doThis(String s); } Answer: 1. 成立,抽象類別繼承介面,並覆寫方法。 abstract class Task implements Actable{ public void doThat(String s) {} } 2. 錯誤,抽象方法後方不可有內容,結尾為 ;。 abstract class Work implements Actable{ public abstract void doThis(String s) {} public void doYourThing(Boolean b) {} } 3. 錯誤,Job繼承介面Actable,但覆寫方法的型別錯誤,Integer改成String。 class Job implements Actable{ public void doThis(Integer i) {} } 4. 錯誤,Action內的方法改成 public void doThis(String i){} 與 public String do this(Integer j){return null;}。 class Action implements Actable{ public void doThis(Integer i) {} public String doThis(Integer j) {} } 5. 成立,Do繼承介面Actable,複寫的doThis(String s)正確,也建立其他方法。 class Do implements Actable{ public void doThis(Integer i) {} public void doThis(String s) {} public void doThat(String s) {} }

「Java」「繼承與覆寫」 Chapter4 Question13

Given: interface Paintable{ public abstract void paint(); } abstract class Canvas implements Paintable{ public void paint() { } } abstract class WhiteBoard extends Canvas{ } class Paper extends Canvas{ protected void paint (char color) { } } class Frame extends Canvas implements Paintable{ public void changeSize() { } } Answer: 所有通過編譯。 解析: Interface Paintable : 可以有abstract 方法與物件方法 class Canvas implements Paintable:可以有abstract類別。但內容不可有abstract方法 abstract class WhiteBoard extends Canvas: abstract類別可無內容。 class Paper extends Canvas: 子類別的方法可覆寫父類別 class Frame extends Canvas implements Paintable: 編譯沒問題,即使沒任何覆寫方法

「Java」「final」 Chapter4 Question12

Given: final class Drink{ public void temp() { } } class Cola{ public final void ship(int time,int heat) { } public void cold() { } } class ColaCompany{ private Cola c = new Cola(); public void create() { c.ship(15,20); } } public class Test3 extends Cola{ public static void main(String[] args) { public void ship(int minutes, int temperature) { } public void addBerry() { } } } Answer: 會造成最大編譯錯誤的是 Test3 類別。 final的方法不能在子類別覆寫 父類別: public final void ship(int time,int heat) 子類別: public void ship(int minutes, int temperature)

「Java」「Singleton」 Chapter4 Question11

Given: abstract class Bird{ String fly() { return "fly"; } abstract void dock(); } public class Test2 extends Bird{ public static void main(String[] args) { Bird b = new Test2(); Bird b2 = new Bird(); } String fly() { return "flyfly"; } void dock() {} } Answer: Bird b2 = new Bird(); 導致無法通過編譯,是必須移除 而 在 Test2 類別中 String fly()方法是可以移除的。

「Java」「abstract」 Chapter4 Question10

Question: A valid reason to declare a class as abstract is to: Mutiple: 1. define methods within a parent class, which may not be overridden in a child class. 2. define common method signatures in a class, while forcing child classes to contain unique method implementations. 3. prevent instance variable from being accessed. 4. prevent a class from being extended. 5. define a class that prevent variable state from being stored when object Instances are serialized. 6. define a class with methods that cannot be concurrently called by multiple threads. Answer: 2. reason: 抽象類別的抽象方法,會迫使子類別提供實作。

「Java」「final」 Chapter4 Question9

Given: final class FinalTest{ final String location; FinalTest(final String loc){ location = loc; } FinalTest(String loc,String s){ location = loc; loc = "unknow"; } } Answer: 編譯成功。 final欄位最晚必須在建構子裡完成初始化。 final的方法參數是不能修改。

「Java」「Inner」 Chapter4 Question8

 哪裡會造成編譯錯誤? Given: class Test1{ private void Show() {} class Test2{ private void Inner() { Show(); } } public static void main(String[] args) { Test2 t2 = new Test1().new Test2(); Inner(); t2.Inner(); new Test1().new Test2().Inner(); } } Answer: Inner(); 造成編譯錯誤。因為Inner()是內部類別。 正確宣告方法: t2.Inner(); new Test1().new Test2().Inner();

「Java」「Singleton」 Chapter4 Question7

Given: A class SingletonA{ private static SingletonA instance; private SingletonA() {} public static synchronized SingletonA getInstance() { return instance; } } ------------------------------------ Given: B class SingletonB{ private static SingletonB instance = new SingletonB(); protected SingletonB() {} public static SingletonB getInstance() { return instance; } } ------------------------------------ Given: C class SingletonC{ SingletonC(){} private static class SingletonHolder{ private static final SingletonC INSTANCE=new SingletonC(); } public static SingletonC getInstance() { return SingletonHolder.INSTANCE; } } ------------------------------------ Given: C enum SingletonD{ INSTANCE; } ------------------------------------ Answer: A. 是private建構子: private SingletonA(){} B. 是protected建構子: protected SingletonB(){} C. 是預設建構子: SingletonC(){} D. 列舉,宣告private建構子 說明: A. 無法使用SingletonA a = new SingletonA();,必須使用 SingletonA a = SingletonA.getInst...

「Java」「enum列舉」 Chapter4 Question6

Which two are true about Singletons? 1. A Singleton must implement Serializable. 2. A Singleton has only the default constructor. 3. A Singleton implements a factory method. 4. A Singleton improves a class's cohesion. 5. Singletons can be designed to be thread-safe. 答案: 3 與 5 解析: 1. 單一實作必須繼承序列化。 //錯,單一實作與序列化無關 2. 單一實作有唯一的default建構子。  //錯。單一實作的建構子是private 3. 單一實作繼承factory方法。  //對,可使用getInstance()方法 4. 單一實作改善 內聚性。  //錯,只有多型才能改善內聚性 5. 單一實作可設計成執行續安全。  //對。單一實作,用於確保執行環境或JVM裡只有一個物件實例。 重點: 建構子必須是private,加強封裝以及封鎖外部類別使用new來建構物件。能保證單一物件。 由於類別外部無法建構物件,就必須依賴類別內部建構物件,來提供一個出口管道。 加上static關鍵字,更能確保 JVM 中只有一份物件。 加上final關鍵字,能要求變數永遠指向該物件。 要提供一個出口管道,就必須用到 getter 方法。 為了回傳static欄位變數,所以get方法也必須使用static。這也是「靜態工廠方法」。

「Java」「Nested Classes巢狀類別」 Chapter4 Question5

圖片
Given: public class Test1 { static class Cal{ //靜態類別方法 int num; void Calculate(){//內部類別方法 num++; } } public static void main(String[] args) { /* insert ?? */ } } Answer: new Test1.Cal().Calculate(); 解析:

「Java」「enum列舉」 Chapter4 Question4

Given: interface Alarm{ void alarm(); } enum Direction implements Alarm{ UP("Direction UP"){ public void alarm() { System.out.println("Go Up"); } }, DOWN("Direction DOWN"); public void alarm() { System.out.println("Go Down"); } private String side; private Direction(String d) { side = d; } @Override public String toString() { return side; } } public class Test1 { public static void main(String[] args) { System.out.println(Direction.UP.toString()); System.out.println(Direction.DOWN); Direction.UP.alarm(); Direction.DOWN.alarm(); } } Answer: ... Question: Which four are ture about enums? Choose: 1. An enum is typesafe. 2. An enum cannot have public methods or fields. 3. An enum can declare a private constructor. 4. All Enums implicitly implement Comparable. 5. An enum can subclass another enum. 6. An enum can implement an interface. Answer: 解析: 1. 對,列舉是一種 型態安全(type-safe) 的概念,與泛型理念相似。 2. 錯...

「Java」「static方法」 Chapter4 Question3

Given: public class Directio { public static String name = "Static Name"; public void show() { //這地方錯誤 System.out.println(name); } public static void main(String[] args) { name="Print Name"; show(); } } Answer: Static 是共同維護 同一個類別內,只能被同樣的static方法調用。 所以 public void show() 必須改成 public static void show()。 否則會造成 compilation fails

「Java」「abstract」 Chapter4 Question2

Given: enum Car { Toyota, Honda, Suzuki, Nissan; } public class Brand { public static void main(String[] args) { for(Car d: Car.values()) {//正確,印出 ToyotaHondaSuzukiNissan System.out.print(d); } for(Car d1: Car.asList()) { //The method asList() is undefined for the type Car System.out.print(d1); } for(Car d2: Car.iterator()) {//The method iterator() is undefined for the type Car System.out.print(d2); } for(Car d3: Car.asArray()) {//The method asArray() is undefined for the type Car System.out.print(d3); } } } Answer: 參考網址:  Where is the documentation for the values() method of Enum? You can't see this method in javadoc because it's added by the compiler. Documented in three places : Enum Types , The Java Tutorials The compiler automatically adds some special methods when it creates an enum. For example, they have a static values method that returns an array containing all of the values of the enum in the order they are ...