简体   繁体   English

Java-超级通话问题

[英]Java - Super Call Issues

I'm new here, so some of my formatting may be odd. 我是新来的,所以我的某些格式可能很奇怪。

I'm having some trouble calling a super constructor. 我在调用超级构造函数时遇到了一些麻烦。 Every time I compile my runner class, I'm getting a few errors: 每次编译跑步者课程时,我都会遇到一些错误:

.class expected" or "; expected

What's going on here? 这里发生了什么?

(Code) (码)

在此处输入图片说明

(Compiler output) (编译器输出)

在此处输入图片说明

You need to call your super-constructor like this: 您需要这样调用您的超级构造函数:

super(y,m,d);

Of course, that begs the question where those three values are supposed to come from. 当然,这引出了这三个值应该来自哪里的问题。 Probably you want to add three parameters to your own constructor: 可能您想向自己的构造函数添加三个参数:

public DateDriver(int y, int m, int d){
   super(y,m,d);
}

But: Why would you want to extend Date in the first place? 但是:为什么您要首先扩展Date

You can't declare variables inside the parameter section of a call to super() ; 您不能在对super()的调用的参数部分内声明变量;

Perhaps what you need is: 也许您需要的是:

public DateDriver(int y, int m, int d) {
    super(y, m, d);
}

Then when you construct the DateDriver object in your main() method, you pass in values for y , m and d . 然后,当在main()方法中构造DateDriver对象时,传入ymd

super(int x, int x, int d); does not make sense. 没有道理。 It looks like you're trying to declare them while you're passing them in? 看起来您在传递它们时正在尝试声明它们? If there exists a constructor in date with three int parameters, then you have to overload that one... not the no-parameter constructor: 如果日期中存在一个带有三个int参数的构造函数,则您必须重载该参数……而不是无参数构造函数:

public DateDriver(int y, int m, int d) {
    super(y, m, d);
}

Right syntax is 正确的语法是

DateDriver()
{
   super(x, y, z);
   /* other code */
}

Always keep first line for a constructor call. 始终保持第一行用于构造函数调用。 If you want to make constructor chaining or you want to call parent class constructor. 如果要进行构造函数链接或要调用父类构造函数。

example : 例如:

//for constructor chaining
DateDriver()
{
   this(x, y, z);
}
// for super call
DateDriver()
{
   this(x, y, z);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM