發表文章

目前顯示的是 2018的文章

[實測] Kingston DataTraveler Swivl 32G 隨身硬碟

圖片
由於官網並沒有實際提供這支隨身碟的讀取速度,只有非常籠統的告知 傳輸速度 3 : 32GB、64GB、128GB:100MB/s 讀取速度。 但當我實際在使用後,腦好充滿疑惑與問號。畢竟花錢也要花在有價值的地方。不是嗎? 測試規格: 1. 採用ATTO Disk Benchmark軟體來測試。 2. USB 3.1 Gen 1 隨身硬碟對USB 3.0孔 而以下就是我所獲得的讀取速度,僅供參考。   感想:  恩......讀取速度真的非常讓人驚豔。但............... 寫入速度的數據也太難看了......(對我個人而言)。 畢竟我所有的專案資料都放在隨身碟中,每次打開程式及存取檔案時,特別有感。 寫入速度慢,儲存時就會卡卡。 讀取數度慢,程式剛啟動時要讀取那些專案就要多花一點時間。 我知道有些人覺得一支隨身硬碟沒多少錢。不喜歡就換? 壞了再買? 但另一個角度思考,這是自己長久要陪伴在身旁的,要求自然要高一點。不是嗎? 下一篇來發表 創見的 JetFlash 790隨身硬碟測試結果。

「Java」 第二課: 基本架構

Java基本架構 類別 也就是一種藍圖。 藍圖中定義了使用者資料型態,如: 屬性與方法。 宣告關鍵字: class 物件屬性 屬性也就是變數。 宣告物件變數,不採用static。 宣告類別變數,採用static。 變數必須宣告在 class 內 。 物件變數: 由各自物件獨立維護,不受干擾。 類別變數: 屬於類別的變數,可藉由 new 來共享共用。 物件方法 描述類別內部的行為。 也是外部存取物件內部資料的方法。 宣告物件方法: 不採用static。 宣告靜態方法: 採用static。 "方法內不可再定義其他方法,否則會發生編譯錯誤" 建立物件實體 類別內定義許多資料型態與宣告。 依據類別宣告創建出來的實體就是物件。 類別名稱  物件(變數)名稱 = new 類別名稱(); 或者 類別名稱  物件(變數)名稱; 物件(變數)名稱 = new 類別名稱(); "類別就是物件"  Test t = new Test(); 講法: 宣告Test物件,變數名稱 t , 並new 一個物件實體。藉由變數名稱 t 來「呼叫」操作該new物件變數。 Test (前): 物件變數 t 的資料型別 t : 物件變數 t ,對物件實體進行調用與操作 new : 建立實體 Test (後): Test 的物件實體,負責實作執行。 解析: Test類別所定義的屬性與方法都可以藉由 t 變數來呼叫,而呼叫後的實作返回,則由Test物件實體 new Test () ; 來實施調用。 以下範例解析: //Hello.java 儲存檔案名稱 //宣告主類別,必須有public且與檔名相同, public class Hello{ // 欄位 屬性 int i = 5; //物件變數 static int j=10; //類別變數 //主方法 public static void main(String[] args){ // 欄位 屬性 int Inner; // 區域變數 static int k=10; //錯誤:

「Java」 第一課: 基本概念

「物件導向設計」 是一種抽象且擬人化的程式設計。 設計師將一個個程式抽象化且各自獨立的物件。 物件(Object): 屬性(Attribute) : 描述資料 方法(Method) :描述物件內的行為 "物件就是一群相關屬性與方法的組合" 物件導向程式(OOP): 物件導向中的物件就是某一個類別的實體(Instance)。 產生一個實體 可以用 new 關鍵字。 所有類別皆可透過繼承的關係呼叫、引用與傳遞。 物件導向的概念如以下: 物件 (Object) 類別 (Class) 抽象化 (Abstraction) 繼承 (Inheritance) 封裝 (Encapsulation) 多型 (Polymorphism)

「Java」「API」Java.lang.System.getProperties()

針對System.getProperty(String key) The java.lang.System.getProperties() method determines the current system properties. The current set of system properties for use by the getProperty(String key) method is returned as a Properties object. If there is no current set of system properties, a set of system properties is first created and initialized. This set of system properties includes values for the following keys − Key Description of Associated Value java.version Java Runtime Environment version java.vendor Java Runtime Environment vendor java.vendor.url Java vendor URL java.home Java installation directory java.vm.specification.version Java Virtual Machine specification version java.vm.specification.vendor Java Virtual Machine specification vendor java.vm.specification.name Java Virtual Machine specification name java.vm.version Java Virtual Machine implementation version java.vm.vendor Java Virtual Machine implementation vendor java.

「HTML」「DOCTYPE」 HTML文件類型的重要性

<!DOCTYPE> 會告知你的 瀏覽器 這個文件是用哪個版本的 HTML (或 XML )撰寫。Doctype 是一種宣告,而非 tag 。你也可以把它想作「document type declaration」(文件類型宣告),或是縮寫的 「DTD」。 很久以前,網頁通常有兩種版本:網景(Netscape)的 Navigator 以及微軟(Microsoft)的 Internet Explorer。在 W3C 創立網路標準後,為了不破壞當時既有的網站,瀏覽器不能直接起用這些標準。因此,瀏覽器導入了能分辨符合新規範、或屬於老舊網站的兩種模式。 目前瀏覽器的排版引擎有三種模式:怪異模式(Quirks mode)、接近標準模式(Almost standards mode)、以及標準模式(Standards mode)。在 怪異模式 ,排版會模擬 Navigator 4 與 Internet Explorer 5 的非標準行為。為了支持在網路標準被廣泛採用前,就已經建置好的網站,這麼做是必要的。在 標準模式 ,行為(期待)由 HTML 與 CSS 的規範描述起來。在 接近標準模式 ,有少數的怪異行為被實行。 「標準模式」 常用的四種設定方法如下: <!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”> <!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01//EN” “http://www.w3.org/TR/html4/strict.dtd”> <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”> <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.

「CSS」「div區塊介紹」 單欄式網頁排版設計

圖片
單欄式網頁排版設計 < style type = "text/css" > #Header { width : 360px ; height : 80px ; text-align : center ; line-height : 80px ; font-size : 15px ; color : #fffaf3 ; font-weight : bold ; background-color : #f9c81e ; } #body { width : 360px ; height : 400px ; text-align : center ; line-height : 280px ; font-size : 15px ; color : #f9c81e ; font-weight : bold ; background-color : #1cf027 ; float : left ; } #Footer { width : 360px ; height : 50px ; text-align : center ; line-height : 50px ; font-size : 15px ;

「CSS」「div區塊介紹」 二欄式網頁排版設計

圖片
二欄式網頁排版設計: <! DOCTYPE HTML PUBLIC > < style type = "text/css" > #Header { width : 360px ; height : 80px ; text-align : center ; line-height : 80px ; font-size : 15px ; color : #fffaf3 ; font-weight : bold ; background-color : #f9c81e ; } #Sidebar { width : 120px ; float : left ; height : 280px ; text-align : center ; line-height : 280px ; font-size : 15px ; color : #ffffff ; font-weight : bold ; background-color : #6a54e2 ; } #body { width : 240px ; height : 280px ; text-align : center ; line-height : 280px ; f

「CSS」「div區塊介紹」 三欄式網頁排版設計

圖片
三欄式網頁排版設計: <! DOCTYPE HTML PUBLIC > < html > < head > < title > Free Css Layout 1 </ title > < meta http-equiv = "content-type" content = "text/html; charset=UTF-8" > </ head > < body > < style type = "text/css" > #sitebody { width : 600px ; margin : 0 auto ; font-size : 13px ; } #header { background-color : rgb ( 218 , 230 , 59 ); height : 80px ; text-align : center ; line-height : 80px ; } #sidebar_left { background-color : rgb ( 233 , 92 , 92 ); width : 120px ; height : 400px ;

「電腦硬體裝修」「DIP雙列直插封裝」

圖片
Dual In-line Package ,也稱為 DIP封裝 ,簡稱為 DIP 或 DIL ,是一種 積體電路的封裝方式 ,積體電路的 外形為長方形 ,在其兩側則有 兩排平行的金屬接腳 ,稱為 排針 。DIP包裝的元件可以 焊接在印刷電路板 電鍍的貫穿孔中,或是 插入在DIP插座 (socket)上。 十四針的積體電路即稱為 DIP14 :  Vcc(電源) 第14腳  GND(接地) 第7腳 十六針的積體電路即稱為 DIP16 : Vcc(電源): Vcc(電源) 第16腳  GND(接地) 第8腳 方向和接腳編號 接腳以逆時針的順序編號 如右圖所示,當元件的識別缺口朝上時,左側最上方的接腳為接腳1,其他接腳則以逆時針的順序依序編號。有時接腳1也會以圓點作為標示。 例如DIP14的IC,識別缺口朝上時,左側的接腳由上往下依序為接腳1至7,而右側的接腳由下往上依序為接腳8至14。 資料擷取於Wiki: https://zh.wikipedia.org/wiki/%E9%9B%99%E5%88%97%E7%9B%B4%E6%8F%92%E5%B0%81%E8%A3%9D

「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 object-oriented design: Program to an inter

「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」「Interface」 instanceof來判斷是否有實作interface

Given: interface Interface{ //Interface inter1 = new Super();//錯誤,Cannot convert Super to Interface //Super sup1 = new Interface();//錯誤,Cannot convert Interface to Super } class Super{ //Interface inter1 = new Super();//正確,假如父類別繼承Interface //Super sup1 = new Interface();//錯誤,Cannot convert Interface to Super } public class Sub extends Super implements Interface{ public static void main(String[] args) { Interface inter = new Sub(); System.out.println(inter instanceof Object); System.out.println(inter instanceof Interface); System.out.println(inter instanceof Super); System.out.println(inter instanceof Sub); Super sup = new Sub(); System.out.println(sup instanceof Object); System.out.println(sup instanceof Interface); System.out.println(sup instanceof Super); System.out.println(sup instanceof Sub); Sub sub = new Sub(); System.out.println(sub instanceof Object); System.out.println(sub instanceof Interface); System.out.println(sub instanceof Super); S

「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

「Java」「abstract」 Chapter4 Question1

Given: abstract class Account{ abstract void deposit(double atm); public abstract boolean withdraw(double atm); } class SubAccount extends Account{ // @Override // void deposit(double atm) { // } // // @Override // public boolean withdraw(double atm) { // return false; // } } What two changes, made independently,will enable the code to compile? Change the signature of Account to: public class Account. Change the signature of SubAccount to:public abstract class SubAccount. Implement private methods for deposit and withdraw in SubAccount. Implement public methods for deposit and withdraw in SubAccount. Change Signature of SubAccount to: SubAccount implements Account. Make Account an interface. 中文翻譯: 以下哪兩個選項能獨自變更並完成編譯? 更改Account簽章成: public class Account ( 錯誤,因Account裡有abstract方法 ) 更改SubAccount簽章成: public abstract class SubAccount ( 正確,因子類別SubAccount改成abstract,也就不需要覆寫方法 ) 實作 修飾子為private 的 deposit 與 withdraw 方法( 錯誤,因Account最高為default,而private權限更高於default。子類別的覆寫方法

「Java」Static 方法與欄位

圖片
Given: class StaticDemo{ private int Field;//物件欄位 private static int StaticField;//static欄位 public void Method() { //物件方法 Field=2; //可。物件欄位 StaticField=3;//可。static欄位 Method();//可。物件方法 CallStatic();//可。static方法 } public static void CallStatic() { StaticField=5;//可。static欄位 } public static void StaticMethod() { Field = 1;//不可。物件欄位 StaticField=3;//可。static欄位 Method();//可。物件方法 CallStatic();//不可。static方法 } } 解說:  在Static方法中,是無法呼叫物件欄位及物件方法。 但物件方法中,是可以呼叫所有的欄位及方法。 子類別中若有相同的簽名static方法,是無法被覆寫的。

「Java」「super & modifier」 Chapter3 Question6

Given: public class Test4 { public static void main(String[] args) { } } abstract class Shape{ //抽象類別 Shape(){}//建構子 protected void area() { //無回傳直的物件方法 System.out.println("Shape"); } } class Square extends Shape{ int a; Square(int a){ this.a=a; } public void area() { System.out.println("Square"); } } class Rectangle extends Square{ int b, c; Rectangle(int b, int c){ /*insert code*/ //缺少 super(b) 或 super(c)。super呼叫父類別的Square(int a) //因 b 與 c 是 int型別 this.b=b; this.c=c; } void area() { //由於Square的area物件方法修飾子是public,所以這子類別的area物件方法,也必須使用相同或更低權限。 //答案: public void area() { System.out.println("Rectangle"); } } Answer: super(b)的呼叫 父子類別的修飾子權限

「Java」「access level modifier修飾子」 Chapter3 Question5

圖片
Given: public class Test4 { public static void main(String[] args) { Cat[] cats = {new Cat(),new Lion()}; for(Cat c: cats) { System.out.println(c.bark()); } } } class Cat { //private String bark() { //造成System.out.println(c.bark());無法呼叫到此物件方法 //String bark() { //Lion的String bark物件方法修飾子: default,protected,public //protected String bark() { //Lion的STring bark物件方法修飾子: protected public public String bark() { //Lion的STring bark物件方法修飾子: public return "Meow"; } } class Lion extends Cat { public String bark() { return "Growl"; } } Answer: Meow Growl ------------------------------------------------------------------ 要覆寫繼承的物件方法: 子類別的修飾子不可小於父類別,或相同修飾子。 也就是父類別的修飾子永遠是比子類別更高,或相同修飾子。 另外一種講法: 父類別就是最高機密,子類別的機密比父類別低或相同權限,並以此推類。 另外,假如方法修飾子是private ,將無法被呼叫到