天天看點

JAVA程式設計語言的基礎知識(六)

JAVA代碼查錯:

1.

  1. abstract class Name {
  2. private String name;
  3. public abstract boolean isStupidName(String name) {}
  4. }

大俠們,這有何錯誤?

答案: 錯。abstract method必須以分号結尾,且不帶花括号。

2.

  1. public class Something {
  2. void doSomething () {
  3. private String s = "";
  4. int l = s.length();
  5. }
  6. }

有錯嗎?

答案: 錯。局部變量前不能放置任何通路修飾符 (private,public,和protected)。final可以用來修飾局部變量

(final如同abstract和strictfp,都是非通路修飾符,strictfp隻能修飾class和method而非variable)。

3.

  1. abstract class Something {
  2. private abstract String doSomething ();
  3. }

這好像沒什麼錯吧?

答案: 錯。abstract的methods不能以private修飾。abstract的methods就是讓子類implement(實作)具體細節的,怎麼可以用private把abstract

method封鎖起來呢? (同理,abstract method前不能加final)。

4.

  1. public class Something {
  2. public int addOne(final int x) {
  3. return ++x;
  4. }
  5. }

這個比較明顯。

答案: 錯。int x被修飾成final,意味着x不能在addOne method中被修改。

5.

  1. public class Something {
  2. public static void main(String[] args) {
  3. Other o = new Other();
  4. new Something().addOne(o);
  5. }
  6. public void addOne(final Other o) {
  7. o.i++;
  8. }
  9. }
  10. class Other {
  11. public int i;
  12. }

和上面的很相似,都是關于final的問題,這有錯嗎?

答案: 正确。在addOne method中,參數o被修飾成final。如果在addOne method裡我們修改了o的reference (比如: o = new Other();),那麼如同上例這題也是錯的。但這裡修改的是o的member vairable (成員變量),而o的reference并沒有改變。

6.

  1. class Something {
  2. int i;
  3. public void doSomething() {
  4. System.out.println("i = " + i);
  5. }
  6. }

有什麼錯呢? 看不出來啊。

答案: 正确。輸出的是"i = 0"。int i屬於instant variable (執行個體變量,或叫成員變量)。instant variable有default value。int的default value是0。

7.

  1. class Something {
  2. final int i;
  3. public void doSomething() {
  4. System.out.println("i = " + i);
  5. }
  6. }

和上面一題隻有一個地方不同,就是多了一個final。這難道就錯了嗎?

答案: 錯。final int i是個final的instant variable (執行個體變量,或叫成員變量)。final的instant variable沒有default value,必須在constructor (構造器)結束之前被賦予一個明确的值。可以修改為"final int i = 0;"。

8.

  1. public class Something {
  2. public static void main(String[] args) {
  3. Something s = new Something();
  4. System.out.println("s.doSomething() returns " + doSomething());
  5. }
  6. public String doSomething() {
  7. return "Do something ...";
  8. }
  9. }

看上去很完美。

答案: 錯。看上去在main裡call doSomething沒有什麼問題,畢竟兩個methods都在同一個class裡。但仔細看,main是static的。static method不能直接call non-static methods。可改成"System.out.println("s.doSomething() returns " + s.doSomething());"。同理,static method不能通路non-static instant variable。

9.

此處,Something類的檔案名叫OtherThing.java

  1. class Something {
  2. private static void main(String[] something_to_do) {
  3. System.out.println("Do something ...");
  4. }
  5. }

這個好像很明顯。

答案: 正确。從來沒有人說過Java的Class名字必須和其檔案名相同。但public class的名字必須和檔案名相同。

10.

  1. interface A{
  2. int x = 0;
  3. }
  4. class B{
  5. int x =1;
  6. }
  7. class C extends B implements A {
  8. public void pX(){
  9. System.out.println(x);
  10. }
  11. public static void main(String[] args) {
  12. new C().pX();
  13. }
  14. }

答案:錯誤。在編譯時會發生錯誤(錯誤描述不同的JVM有不同的資訊,意思就是未明确的x調用,兩個x都比對(就象在同時import java.util和java.sql兩個包時直接聲明Date一樣)。對于父類的變量,可以用super.x來明确,而接口的屬性預設隐含為 public static final.是以可以通過A.x來明确。

11.

  1. interface Playable {
  2. void play();
  3. }
  4. interface Bounceable {
  5. void play();
  6. }
  7. interface Rollable extends Playable, Bounceable {
  8. Ball ball = new Ball("PingPang");
  9. }
  10. class Ball implements Rollable {
  11. private String name;
  12. public String getName() {
  13. return name;
  14. }
  15. public Ball(String name) {
  16. this.name = name;
  17. }
  18. public void play() {
  19. ball = new Ball("Football");
  20. System.out.println(ball.getName());
  21. }
  22. }

這個錯誤不容易發現。

答案: 錯。"interface Rollable extends Playable, Bounceable"沒有問題。interface可繼承多個interfaces,是以這裡沒錯。問題出在interface Rollable裡的"Ball ball = new Ball("PingPang");"。任何在interface裡聲明的interface variable (接口變量,也可稱成員變量),預設為public static final。也就是說"Ball ball = new Ball("PingPang");"實際上是"public static final Ball ball = new Ball("PingPang");"。在Ball類的Play()方法中,"ball = new Ball("Football");"改變了ball的reference,而這裡的ball來自Rollable interface,Rollable interface裡的ball是public static final的,final的object是不能被改變reference的。是以編譯器将在"ball = new Ball("Football");"這裡顯示有錯。

12、内部類可以引用他包含類的成員嗎?有沒有什麼限制?

一個内部類對象可以通路建立它的外部類對象的内容

13、WEB SERVICE名詞解釋。JSWDL開發包的介紹。JAXP、JAXM的解釋。SOAP、UDDI,WSDL解釋。

Web ServiceWeb Service是基于網絡的、分布式的子產品化元件,它執行特定的任務,遵守具體的技術規範,這些規範使得Web Service能與其他相容的元件進行互操作。

JAXP(Java API for XML Parsing) 定義了在Java中使用DOM, SAX, XSLT的通用的接口。這樣在你的程式中你隻要使用這些通用的接口,當你需要改變具體的實作時候也不需要修改代碼。

JAXM(Java API for XML Messaging) 是為SOAP通信提供通路方法和傳輸機制的API。

WSDL是一種 XML 格式,用于将網絡服務描述為一組端點,這些端點對包含面向文檔資訊或面向過程資訊的消息進行操作。這種格式首先對操作和消息進行抽象描述,然後将其綁定到具體的網絡協定和消息格式上以定義端點。相關的具體端點即組合成為抽象端點(服務)。

繼續閱讀