發表文章

目前顯示的是 7月, 2018的文章

「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 ,將無法被呼叫到

「Java」「extends & method」 Chapter3 Question4

Given: public class Test4 { public static void main(String[] args) { Employee e1 = new Employee(); System.out.println(e1.getDepartment());// stuff Employee e2 = new Manager(); System.out.println(e2.getDepartment());// stuff Employee e3 = new GeneralManager(); System.out.println(e3.getDepartment());// GeneralManager Employee e4 = new CEO(); System.out.println(e4.getDepartment());// CEO // Manager m1 = new Employee();//編譯錯誤: 子=父 // System.out.println(m1.getDepartment()); Manager m2 = new Manager(); System.out.println(m2.getDepartment());// stuff Manager m3 = new GeneralManager(); System.out.println(m3.getDepartment());// GeneralManager Manager m4 = new CEO(); System.out.println(m4.getDepartment());// CEO // GeneralManager g1 = new Employee();//編譯錯誤: 子=父 // System.out.println(m1.getDepartment()); //GeneralManager g2 = new Manager();////編譯錯誤: 子=父 //System.out.println(m2.getDepartment()); GeneralManager g3 = new GeneralManager(); Syste

「Java」「abstract」 Chapter3 Question3

Given: public class Test4 { public static void main(String[] args) { Beef b = new Beef(); Beef b1 = new Buffulo(); System.out.println(b+" "+b1); } } class Meat{ //錯誤必須改成 abstract class Meat{ abstract String colour();//該abstract方法,子類別必須實作,抽象方法是沒有body String size(){ //該物件方法不須abstract,子類別依需求實作。有內容的不可為抽象方法。 return "big"; } class Beef extends Meat{ String colour() { return "red-Beef"; } } class Buffulo extends Beef{ String colour() { return "red-Buffulo"; } } Answer: Compliation fails 因具備abstract的方法,其類別也必須是abstract類別。

「Java」「equals」 Chapter3 Question2

Given: public class Test2 { public static void main(String [] args) { Test t1 = new Test(10); Test t2 = new Test(10); Test t3 = new Test(30); System.out.println(t1.equals(t2)); System.out.println(t2.equals(t3)); System.out.println(t2.equals(t2)); } } class Test{ private int a ; public Test(int aa) { this.a=aa; } public int hashcode() { return a+42; } public boolean equals(Object obj) { return (this==obj)?true:super.equals(obj); } } Answer: false false true 當改寫 equlas 也須覆寫 hashcode, 此為必要條件. 在java裡,只有【傳值】,沒有【傳址】。不論基本型態變數或參考變數,都是複製它們的位元樣式 給另一個變數 。 所以當參考變數在使用==做比較時,是指兩物件是否有同時參考(指向)在heap(堆積)內的物件,也就是兩個參考變數是否存放相同的記憶體位址(位元樣式)。 如下: return (this==obj)?true:super.equals(obj); ---------------------------------------------------------------------- equals(Object obj) equals()是用在兩物件比較是否儲存相同的內容值,以 Integer i1 = new Integer(6); Integer i2 = new Integer(6); i1.equals(i2); 無疑地,一定是true。 因為wrapper class有實作equals,所以可用來比較兩wrapper object是否存放相同的內容值; 而S

「Java」「Overriding」 Chapter3 Question1

圖片
Given: public class Test4 { public static void main(String[] args) { new FlyFish().result(); } } class Fish{ int num = 3; static int total=30; String jump() { return "jump"; } static String Speed() { return "normal"; } } class FlyFish extends Fish{ int num = 10; static int total=100; String jump() { return "Fly"; } public static String Speed() { return "fast"; } void result() { Fish f = new FlyFish(); System.out.println(f.num+" "+f.total+" "+f.jump()+" "+f.Speed()+" "); FlyFish f1 = new FlyFish(); System.out.println(f1.num+" "+f1.total+" "+f1.jump()+" "+f1.Speed()+" "); } } Answer: