java中词法和句法错误的区别

时间:2014-02-03 19:31:25

标签: java compiler-errors

我有以下java类,我需要找到可能的错误并区分词汇,语义和句法。我把错误放在每一个方面,但我不确定它们之间有什么区别,如果我确定它们是正确的。

class Part {
    private int number;
    private String name;
    private int quantity;

    public Part(int number, String name, int quantity){
        this.number = number;
        this.name = name;
        this.quantity = quantity;
    }

    public Part(int number, string name){ // Semantic Error
       this(number, name, 0);
    }

    public Part(){
        this(0, 'No Name'); // Lexical Error
    }

    public void decreaseQuantity(int amount){
        quantity =- amount;
    }

    public int getName(){ //Semantic error
        return name;
    }

    public int getnumber(){
        return number;
    }

    public int getQuantity(){
        return quanity; // Semantic error
    } // Syntatic Error(this was a missing bracket)
} 

1 个答案:

答案 0 :(得分:0)

您可以添加enum来区分它们:

public enum ErrorType {LEXICAL, SEMANTIC, SYNTACTIC}

在课堂上添加属性:

class Part {
    private ErrorType errorType; 
    // other properties and methods}
}