java:不能在抽象类中使用构造函数
我在 red5 中为作业调度程序创建了以下抽象类:
package com.demogames.jobs;
import com.demogames.demofacebook.MysqlDb;
import org.red5.server.api.IClient;
import org.red5.server.api.IConnection;
import org.red5.server.api.IScope;
import org.red5.server.api.scheduling.IScheduledJob;
import org.red5.server.api.so.ISharedObject;
import org.apache.log4j.Logger;
import org.red5.server.api.Red5;
/**
*
* @author ufk
*/
abstract public class DemoJob implements IScheduledJob {
protected IConnection conn;
protected IClient client;
protected ISharedObject so;
protected IScope scope;
protected MysqlDb mysqldb;
protected static org.apache.log4j.Logger log = Logger
.getLogger(DemoJob.class);
protected DemoJob (ISharedObject so, MysqlDb mysqldb){
this.conn=Red5.getConnectionLocal();
this.client = conn.getClient();
this.so=so;
this.mysqldb=mysqldb;
this.scope=conn.getScope();
}
protected DemoJob(ISharedObject so) {
this.conn=Red5.getConnectionLocal();
this.client=this.conn.getClient();
this.so=so;
this.scope=conn.getScope();
}
protected DemoJob() {
this.conn=Red5.getConnectionLocal();
this.client=this.conn.getClient();
this.scope=conn.getScope();
}
}
然后我创建了一个扩展前一个类的简单类:
public class StartChallengeJob extends DemoJob {
public void execute(ISchedulingService service) {
log.error("test");
}
}
问题是我的主应用程序只能看到没有任何参数的构造函数。 意味着我可以做new StartChallengeJob()
为什么主应用程序看不到所有构造函数?
谢谢!
I created the following abstract class for job scheduler in red5:
package com.demogames.jobs;
import com.demogames.demofacebook.MysqlDb;
import org.red5.server.api.IClient;
import org.red5.server.api.IConnection;
import org.red5.server.api.IScope;
import org.red5.server.api.scheduling.IScheduledJob;
import org.red5.server.api.so.ISharedObject;
import org.apache.log4j.Logger;
import org.red5.server.api.Red5;
/**
*
* @author ufk
*/
abstract public class DemoJob implements IScheduledJob {
protected IConnection conn;
protected IClient client;
protected ISharedObject so;
protected IScope scope;
protected MysqlDb mysqldb;
protected static org.apache.log4j.Logger log = Logger
.getLogger(DemoJob.class);
protected DemoJob (ISharedObject so, MysqlDb mysqldb){
this.conn=Red5.getConnectionLocal();
this.client = conn.getClient();
this.so=so;
this.mysqldb=mysqldb;
this.scope=conn.getScope();
}
protected DemoJob(ISharedObject so) {
this.conn=Red5.getConnectionLocal();
this.client=this.conn.getClient();
this.so=so;
this.scope=conn.getScope();
}
protected DemoJob() {
this.conn=Red5.getConnectionLocal();
this.client=this.conn.getClient();
this.scope=conn.getScope();
}
}
Then i created a simple class that extends the previous one:
public class StartChallengeJob extends DemoJob {
public void execute(ISchedulingService service) {
log.error("test");
}
}
The problem is that my main application can only see the constructor without any parameters.
with means i can do new StartChallengeJob()
why doesn't the main application sees all the constructors ?
thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
构造函数不会被继承。 StartChallengeJob 有效如下所示:
如果您希望所有超类构造函数签名可用,则还需要在
StartChallengeJob
中包含这些构造函数:Constructors aren't inherited. StartChallengeJob effectively looks like this:
If you want all of the superclass constructor signatures to be available, you'll need to have those constructors in
StartChallengeJob
too:对于问题中显示的 StartChallengeJob 类,编译器会生成一个默认构造函数,该构造函数隐式调用基类的默认构造函数。
如果这一默认行为不是您想要的,您需要在 StartChallengeJob 中显式定义一个或多个构造函数,然后调用所需的基类构造函数。例如,如果您想要默认构造函数和参数化构造函数,则需要同时定义两者:
For the
StartChallengeJob
class shown in your question, the compiler generates a default constructor, which implicitly calls the default constructor of the base class.If this default behaviour is not what you want, you need to explicitly define one or more constructor(s) in
StartChallengeJob
, which then call the desired base class constructor. E.g. if you want both a default and a parameterized constructor, you need to define both: