构造函数中的Java错误

时间:2014-07-09 15:44:27

标签: java constructor

我在java程序中收到编译错误,但我无法理解它是什么。

我有一个主要执行四个类:towerblock s的组织),blockcoloredblockjollyblock

这些是错误:

.\coloredblock.java:8: error: constructor block in class block cannot be applied t
o given types;
    public coloredblock(int n,String c){
                                         ^

required: int
found: no arguments
reason: actual and formal argument lists differ in length
.\coloredblock.java:11: error: cannot find symbol
    if (c.lenght() > 10)
         ^
symbol:   method lenght()
location: variable c of type String
coloredblock.java:13: error: call to super must be first statement in constr
uctor
    super(n);
         ^
jollyblock.java:3: error: constructor block in class block cannot be applied to given    
    public jollyblock(int n){

这是block类:

public abstract class block{
    //CAMPI
    private int number;
    private String color;

    public block(int n){
        if (n < 0)
        throw new IllegalArgumentException("n < 0!");
        number=n;
    }   
}

这是coloredblock

public class coloredblock extends block{
    //CAMPI
    private String color;

    public coloredblock (int n,String c){
        if (n < 0)
        throw new IllegalArgumentException("n < 0!");
        if (c.lenght() > 10)
        throw new IllegalArgumentException("more of 10!");
        super(n);
        color=c;
    }  
}

这是课程tower

import java.util.Collections;
import java.util.ArrayList;

public class tower{
    //CAMPI
    private String nametower;
    private ArrayList<Blocco> Towerblock;
    private int nblocchicolorati;
    private int nblocchijolly;

    public Torre(String n){     //COSTRUTTORI

        Towerblock= new ArrayList<block>();
        nametower=n;
    }

    public void addblock(block b){    //METODI
        Towerblock.add(0,b);  //da vedere
    }

    public int numColorati(){
        nblocchicolorati=0;

        for(int i=0;i < Towerblock.size();i++)
        {
            if(Towerblock.get(i) instanceof coloredblock)
            nblocchicolorati++;
        }

        return nblocchicolorati;
    }

    public int numJolly(){

        nblocchijolly=0;

        for(int i=0;i < Towerblock.size();i++)
        {
            if(Towerblock.get(i) instanceof jollyblock)
            nblocchicolorati++;
        }

        return nblocchijolly;

    }
}

//这是主要的:

public class exercise1{

    public static void main(String[] args){

        Tower tower= new Tower ("Alfa");

        tower.addBlock( new coloredblock( 1, "green") );
        tower.addBlock( new coloredblock( 5, "green") );
        tower.addBlock( new coloredblock( 4, "red") );
        tower.addBlock( new jollyblock(10) );


        // *** Stampare tutta la torre
        //System.out.println("NUM. BLOCCHI COLORATI: " + tower.numColorati() );
        //System.out.println("NUM. BLOCCHI JOLLY: " + tower.numJolly() );
    } 
}

非常感谢你的帮助。这是我参加大学课程的练习,明天我将参加考试。

(我没有包含jollyblock的代码,因为它类似于coloredblock的代码

3 个答案:

答案 0 :(得分:1)

下次,请告诉我们您的尝试。对于这些简单的错误,请尝试搜索Google,您将很快找到答案。

您对“lenght”的呼吁拼写不正确。它应该是“length()”。

super(n)行应该是构造函数的开头行之后的第一行,如下所示:

public coloredblock (int n,String c){
    super(n); //first line
    if (n<0)
        throw new IllegalArgumentException("n < 0!");
    if (c.lenght() > 10)
        throw new IllegalArgumentException("more of 10!");
    color=c;    
}

答案 1 :(得分:0)

有两个错误。首先是你拼错了length()方法。另一个是如果你在构造函数中调用super(),它必须是构造函数中的第一个语句。将您的coloredblock构造函数更改为:

public coloredblock (int n,String c) {
  super(n);

  if (n<0)
    throw new IllegalArgumentException("n < 0!");
  if (c.length() > 10)
    throw new IllegalArgumentException("more of 10!");

  color=c;    
}
祝你好运。

答案 2 :(得分:0)

super(n)命令应该是您为扩展构造函数调用的第一个命令。此外,您不需要重复相同的检查'if(n&lt; 0)',因为这是由'block'构造函数处理的。 'super()'方法执行初始构造函数的所有命令。试试这个,看看这是否可以解决您的问题:

public class coloredblock extends block
{

    //CAMPI

    private String color;

    public coloredblock (int n,String c)
    {

        super(n);

        if (c.lenght() > 10)
        throw new IllegalArgumentException("more of 10!");

        color=c;    
    }
}