java,用main类的构造函数扩展类有参数 [英] java, extending class with the constructor of main class has parameter

查看:161
本文介绍了java,用main类的构造函数扩展类有参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

hei. the language is java. i want to extend this class which the constructor has parameters.

这是主类

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) {
    //}
}

如何编写正确的语法?
,错误是构造函数不能应用于给定类型

how do i write the correct syntax? and the error is "constructor cannot be applied to given types"

推荐答案

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

You can define any arguments you need for your constructor, but it is necessary to call one constructor of the super class as the first line of your own constructor. This can be done using super() or 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,用main类的构造函数扩展类有参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆