嘿。语言是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 class)的一个构造函数作为你自己构造函数的第一行。这可以使用 super()
或 super(arguments)
来完成。
public class CMainCharacter extends CAnimatedSprite {
public CMainCharacter() {
super("your pFn value here", 0, 0);
//do whatever you want to do in your constructor here
}
public CMainCharacter(String pFn, int pWidth, int pHeight) {
super(pFn, pWidth, pHeight);
//do whatever you want to do in your constructor here
}
}
关于java,用主类的构造函数扩展类有参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2056097/