Java 构造函数与编译
两件事:Java 构造函数与编译,java,constructor,compiler-errors,Java,Constructor,Compiler Errors,两件事: 这是我编写的程序的一部分,但它没有运行 一般来说,我得到的任务包括定义2个构造函数,我不明白一个类如何能够在逻辑上使用2个构造函数。 我想我成功地创建了基本构造函数,但我不知道如何创建它(为什么?) 第二个构造函数(在/*和*/之间的代码中显示的构造函数)需要接收另一个时间并复制其值 谢谢你的帮助 ================================================================================== public class
public class Time1 {
private int _hour,
_minute,
_second;
public Time1 (int h, int m, int s) {
if (hourIsValid (h))
_hour = h;
else _hour = 0;
if (minuteIsValid (m))
_minute = m;
else minute = 0;
if (secondIsValid (s))
_second = s;
else _second = 0;
}
boolean hourIsValid (h) {
return (h <= 23 && h >= 0);
}
boolean minuteIsValid (m) {
return (m <= 59 && m >= 0);
}
boolean secondIsValid (s) {
return (s <= 59 && s >= 0);
}
/* public Time1 (Time1 other) {...
}
*/
int getHour() {
return _hour;
}
int getMinute() {
return _minute;
}
int getSecond() {
return _second;
}
void setHour (int num) {
if (num >= 0 && num <= 23)
_hour = num;
}
void setMinute (int num) {
if (num >= 0 && num <=59)
_minute = num;
}
void setSecond (int num) {
if (num >= 0 && num <=59)
_second = num;
}
String toString () {
String strH,
strM,
strS;
if (_hour < 10)
strH = "0" + _hour + ":";
else strH = _hour + ":";
if (_minute < 10)
strM = "0" + _minute + ":";
else strM = _minute + ":";
if (_second < 10)
strS = "0" + _second;
else strS = _second;
return (strH + strM + strS);
}
/*if (_hour < 10)
System.out.print("0" + _hour + ":");
else System.out.print(_hour + ":");
if (_minute < 10)
System.out.print("0" + _minute + ":");
else System.out.print(_minute + ":");
if (_second < 10)
System.out.print("0" + _second);
else System.out.print(_second);
}
*/
boolean equals (Time1 other) {
return ((_hour == other._hour) && (_minute == other._minute) && (_second == other._second));
}
boolean before (Time1 other) {
return ((_hour < other._hour) ||
((_hour == other._hour) && (_minute < other._minute)) ||
((_hour == other._hour) && (_minute == other._minute) && (_second < other._second)));
}
boolean after (Time1 other) {
return other.before(this);
}
int difference (Time1 other) {
int obSec, otherSec;
obSec = ((_hour * 60 * 60) + (_minute * 60) + _second);
otherSec = ( (other._hour * 60 * 60) + (other._minute * 60) + other._second);
return (obSec - otherSec);
}
}
公共类时间1{
私人国际时间,
_分钟,
_第二;
公共时间1(整数h、整数m、整数s){
如果(小时有效(h))
_小时=小时;
否则_小时=0;
如果(分钟有效(m))
_分钟=m;
否则分钟=0;
如果(第二个有效)
_秒=秒;
否则_秒=0;
}
布尔hourIsValid(h){
回报率(h=0);
}
布尔分钟有效值(m){
返回值(m=0);
}
布尔值secondIsValid(s){
返回(s=0);
}
/*公共时间1(时间1其他){。。。
}
*/
int getHour(){
每小时返回;
}
int getMinute(){
返回(分钟);;
}
int getSecond(){
返回秒;
}
无效设置小时(整数){
如果(num>=0&&num=0&&num=0&&num,则称为重载。您可以使用相同的名称定义方法,只要它们具有不同的参数,但共享返回类型(或返回void,或者对于构造函数,缺少返回类型)。编译器将根据用于调用它的参数知道使用什么方法
在这种情况下,我猜你想要的是:
public Time1 (Time1 other) {
this._hour = other.getHour();
this._minute = other.getMinute();
this._second = other.getSecond();
}
另一个选项是使用this
关键字链接构造函数,如下所示:
public Time1 (Time1 other) {
this(other.getHour(), other.getMinute(), other.getSecond());
}
当你想从Time1对象复制变量时,你就把它传递给构造函数。当然,你应该添加错误检查等等,但是我希望你能得到一般的想法
Oracle有一些不错的方法,您可能需要研究。这称为重载。您可以使用相同的名称定义方法,只要它们具有不同的参数,但共享返回类型(或返回void,或者对于构造函数,缺少返回类型).编译器将根据用于调用该方法的参数知道要使用的方法
在这种情况下,我猜你想要的是:
public Time1 (Time1 other) {
this._hour = other.getHour();
this._minute = other.getMinute();
this._second = other.getSecond();
}
另一个选项是使用this
关键字链接构造函数,如下所示:
public Time1 (Time1 other) {
this(other.getHour(), other.getMinute(), other.getSecond());
}
当你想从Time1对象复制变量时,你就把它传递给构造函数。当然,你应该添加错误检查等等,但是我希望你能得到一般的想法
Oracle有一些不错的方法,您可能需要研究。这称为重载。您可以使用相同的名称定义方法,只要它们具有不同的参数,但共享返回类型(或返回void,或者对于构造函数,缺少返回类型).编译器将根据用于调用该方法的参数知道要使用的方法
在这种情况下,我猜你想要的是:
public Time1 (Time1 other) {
this._hour = other.getHour();
this._minute = other.getMinute();
this._second = other.getSecond();
}
另一个选项是使用this
关键字链接构造函数,如下所示:
public Time1 (Time1 other) {
this(other.getHour(), other.getMinute(), other.getSecond());
}
当你想从Time1对象复制变量时,你就把它传递给构造函数。当然,你应该添加错误检查等等,但是我希望你能得到一般的想法
Oracle有一些不错的方法,您可能需要研究。这称为重载。您可以使用相同的名称定义方法,只要它们具有不同的参数,但共享返回类型(或返回void,或者对于构造函数,缺少返回类型).编译器将根据用于调用该方法的参数知道要使用的方法
在这种情况下,我猜你想要的是:
public Time1 (Time1 other) {
this._hour = other.getHour();
this._minute = other.getMinute();
this._second = other.getSecond();
}
另一个选项是使用this
关键字链接构造函数,如下所示:
public Time1 (Time1 other) {
this(other.getHour(), other.getMinute(), other.getSecond());
}
当你想从Time1对象复制变量时,你就把它传递给构造函数。当然,你应该添加错误检查等等,但是我希望你能得到一般的想法
Oracle有一些不错的工具,你可能想研究一下。看起来你对java和一般编程都是新手?你读过关于类/构造函数/方法等的书吗?不管怎样,互联网上很多地方都有关于多个构造函数的解释,所以简单的谷歌搜索可能就可以了……看起来你是新手java和一般编程?你读过关于类/构造函数/方法等的书吗?不管怎样,互联网上很多地方都有关于多个构造函数的解释,所以一个简单的谷歌搜索可能就可以了…看起来你对java和一般编程都是新手?你读过关于类/构造函数/方法的书吗等等?不管怎样,互联网上很多地方都有关于多个构造函数的解释,所以一个简单的谷歌搜索可能就可以了…看起来你对java和编程都是新手?你读过关于类/构造函数/方法等等的书吗?不管怎样,互联网上很多地方都有关于多个构造函数的解释,所以一个简单的谷歌搜索可能就可以了…+1我很好地解释道。你的教程很好,但我建议OP在这里使用quick info[.@BoskoMijin Good link。我只是在Java方面默认使用Oracle,忘了还有其他可能更好的东西:)如果我理解正确,您只有在使用基本构造函数之后才使用第二个构造函数?@user3403740不,构造函数是独立的,将执行哪一个构造函数取决于您创建对象时传递的参数。不过,您可以链接构造函数调用,因此如果您有一个接受三个参数的构造函数,则可以ll是一个包含两个参数的构造函数,以此类推;这可以最大限度地减少代码重复