Java代碼查錯,Java代碼

來源:互聯網
上載者:User

Java代碼查錯,Java代碼



1.
abstract class Name {
   private String name;
   public abstract boolean isStupidName(String name) {}
}
有何錯誤?
-------------- 抽象類別裡的抽象方法沒有方法體,以分號結尾

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

--------------方法內不能使用存取修飾詞
3.
abstract class Something {
   private abstract String doSomething ();
}

---------------private和abstract不能在一起用
4.
public class Something {
   public int addOne(final int x) {
       return ++x;
   }
}
----------------當final修飾的標量作為參數時,可以被應用一次,但是其值不能被改變

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;
}

-----------------沒錯(O的引用並沒有變,只是調用他的屬性)
6.
class Something {
    int i;
    public void doSomething() {
        System.out.println("i = " + i);
    }
}
有什麼錯呢?
------------------沒錯,成員變數有defaultValue;
7.
class Something {
    final int i;
    public void doSomething() {
        System.out.println("i = " + i);
    }
}

-------------------final修飾的成員變數沒有defaultValues
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方法是static的,不能直接調用non-static方法
9.
此處,Something類的檔案名稱叫OtherThing.java
class Something {
    private static void main(String[] something_to_do) {       
        System.out.println("Do something ...");
    }
}

----------------------沒錯,非public的類名可以與檔案名稱不一樣
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();
   }
}
-------------------------調用不明確
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());
    }
}
----------------------------介面的成員變數預設為public static final,故  ball = new Ball("Football");改變了其引用,故錯誤

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.