java,使用主类的构造函数扩展类,并具有参数

java,使用主类的构造函数扩展类,并具有参数,java,Java,嗨。语言是java。 我想扩展这个构造函数有参数的类 这是主课 public class CAnimatedSprite { public CAnimatedSprite(String pFn, int pWidth, int pHeight) { } } 这是儿童班 public class CMainCharacter extends CAnimatedSprite { //public void CMainCharacter:CAnimatedSprite(

嗨。语言是java。 我想扩展这个构造函数有参数的类

这是主课

public class CAnimatedSprite {
     public CAnimatedSprite(String pFn, int pWidth, int pHeight) {
     }
}
这是儿童班

public class CMainCharacter extends CAnimatedSprite {

    //public void CMainCharacter:CAnimatedSprite(String pFn, int pWidth, int pHeight) {
    //}
}
如何编写正确的语法?
错误是“构造函数不能应用于给定的类型”

您可以为构造函数定义所需的任何参数,但必须调用超类的一个构造函数作为自己构造函数的第一行。这可以使用
super()
super(参数)
来完成


构造函数的第一条语句必须是对超类构造函数的调用。语法是:

super(pFn, pWidth, pHeight);
由您决定是否希望类的构造函数具有相同的参数,并将它们传递给超类构造函数:

public CMainCharacter(String pFn, int pWidth, int pHeight) {
    super(pFn, pWidth, pHeight);
}
或者传递其他信息,例如:

public CMainCharacter() {
    super("", 7, 11);
}

并且不要为构造函数指定返回类型。这是非法的。

如果我的根类中有多个构造函数怎么办?我是否必须在扩展类中为它们中的每一个执行super()?
public class CAnimatedSprite {
     public CAnimatedSprite(String pFn, int pWidth, int pHeight) {
     }
}


public class CMainCharacter extends CAnimatedSprite {

    // If you want your second constructor to have the same args
    public CMainCharacter(String pFn, int pWidth, int pHeight) {
        super(pFn, pWidth, pHeight);
    }
}
public class CAnimatedSprite {
     public CAnimatedSprite(String pFn, int pWidth, int pHeight) {
     }
}


public class CMainCharacter extends CAnimatedSprite {

    // If you want your second constructor to have the same args
    public CMainCharacter(String pFn, int pWidth, int pHeight) {
        super(pFn, pWidth, pHeight);
    }
}