JAVA代碼查錯:
1.
- abstract class Name {
- private String name;
- public abstract boolean isStupidName(String name) {}
- }
大俠們,這有何錯誤?
答案: 錯。abstract method必須以分号結尾,且不帶花括号。
2.
- public class Something {
- void doSomething () {
- private String s = "";
- int l = s.length();
- }
- }
有錯嗎?
答案: 錯。局部變量前不能放置任何通路修飾符 (private,public,和protected)。final可以用來修飾局部變量
(final如同abstract和strictfp,都是非通路修飾符,strictfp隻能修飾class和method而非variable)。
3.
- abstract class Something {
- private abstract String doSomething ();
- }
這好像沒什麼錯吧?
答案: 錯。abstract的methods不能以private修飾。abstract的methods就是讓子類implement(實作)具體細節的,怎麼可以用private把abstract
method封鎖起來呢? (同理,abstract method前不能加final)。
4.
- public class Something {
- public int addOne(final int x) {
- return ++x;
- }
- }
這個比較明顯。
答案: 錯。int x被修飾成final,意味着x不能在addOne method中被修改。
5.
- public class Something {
- public static void main(String[] args) {
- Other o = new Other();
- new Something().addOne(o);
- }
- public void addOne(final Other o) {
- o.i++;
- }
- }
- class Other {
- public int i;
- }
和上面的很相似,都是關于final的問題,這有錯嗎?
答案: 正确。在addOne method中,參數o被修飾成final。如果在addOne method裡我們修改了o的reference (比如: o = new Other();),那麼如同上例這題也是錯的。但這裡修改的是o的member vairable (成員變量),而o的reference并沒有改變。
6.
- class Something {
- int i;
- public void doSomething() {
- System.out.println("i = " + i);
- }
- }
有什麼錯呢? 看不出來啊。
答案: 正确。輸出的是"i = 0"。int i屬於instant variable (執行個體變量,或叫成員變量)。instant variable有default value。int的default value是0。
7.
- class Something {
- final int i;
- public void doSomething() {
- System.out.println("i = " + i);
- }
- }
和上面一題隻有一個地方不同,就是多了一個final。這難道就錯了嗎?
答案: 錯。final int i是個final的instant variable (執行個體變量,或叫成員變量)。final的instant variable沒有default value,必須在constructor (構造器)結束之前被賦予一個明确的值。可以修改為"final int i = 0;"。
8.
- public class Something {
- public static void main(String[] args) {
- Something s = new Something();
- System.out.println("s.doSomething() returns " + doSomething());
- }
- public String doSomething() {
- return "Do something ...";
- }
- }
看上去很完美。
答案: 錯。看上去在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
- class Something {
- private static void main(String[] something_to_do) {
- System.out.println("Do something ...");
- }
- }
這個好像很明顯。
答案: 正确。從來沒有人說過Java的Class名字必須和其檔案名相同。但public class的名字必須和檔案名相同。
10.
- interface A{
- int x = 0;
- }
- class B{
- int x =1;
- }
- class C extends B implements A {
- public void pX(){
- System.out.println(x);
- }
- public static void main(String[] args) {
- new C().pX();
- }
- }
答案:錯誤。在編譯時會發生錯誤(錯誤描述不同的JVM有不同的資訊,意思就是未明确的x調用,兩個x都比對(就象在同時import java.util和java.sql兩個包時直接聲明Date一樣)。對于父類的變量,可以用super.x來明确,而接口的屬性預設隐含為 public static final.是以可以通過A.x來明确。
11.
- interface Playable {
- void play();
- }
- interface Bounceable {
- void play();
- }
- interface Rollable extends Playable, Bounceable {
- Ball ball = new Ball("PingPang");
- }
- class Ball implements Rollable {
- private String name;
- public String getName() {
- return name;
- }
- public Ball(String name) {
- this.name = name;
- }
- public void play() {
- ball = new Ball("Football");
- System.out.println(ball.getName());
- }
- }
這個錯誤不容易發現。
答案: 錯。"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 格式,用于将網絡服務描述為一組端點,這些端點對包含面向文檔資訊或面向過程資訊的消息進行操作。這種格式首先對操作和消息進行抽象描述,然後将其綁定到具體的網絡協定和消息格式上以定義端點。相關的具體端點即組合成為抽象端點(服務)。