java的函数有没有默认参数值这种用法:void function(int number=5){} ?????????

antpower 2003-05-25 12:01:17

我试了一下,这样用是不可以得。
void function(int number=5)
{
...............
}

是不是只能用函数重载来解决?
void function ()
{
in number=5;
.............
}

void function(int number)
{..........}
这样岂不是非常的麻烦〉?????

如何更好的解决????
...全文
3536 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
danceflash 2003-05-27
  • 打赏
  • 举报
回复
咣当~~~
原来已经结了啊^_^
antpower 2003-05-27
  • 打赏
  • 举报
回复
明白了,再次结:)
antpower 2003-05-27
  • 打赏
  • 举报
回复
5,1,0,5,0,3,3,0,0,1,0,0,1,0,0,1,0
贴子回复次数大于跟给分次数

怎么搞的,还结不了了呢!
danceflash 2003-05-27
  • 打赏
  • 举报
回复
其实可以对比一下Java和C++
在C++中是允许有默认参数的
但是在这种情况下有的时候会引起重载函数的模糊调用
而Java从这种方面考量,没有加入这种特性
但是其实你可以这么写:

public void function( int number )
{
......
}

public void function()
{
this.function(5);
}

这样写来,你依然觉得麻烦吗?^_^
xfcy2003 2003-05-27
  • 打赏
  • 举报
回复
java中不支持参数默认值的问题你可以通过构造函数实现呀!!!
antpower 2003-05-27
  • 打赏
  • 举报
回复
不怪不怪,见怪不怪,其怪自败。
结贴了
kmonkey 2003-05-27
  • 打赏
  • 举报
回复
这样的问题有点怪怪的
antpower 2003-05-27
  • 打赏
  • 举报
回复
to cooled(经典中--经过非典中):
"to antpower(方向不对,换个姿势再睡.) :如果用function(5);会有编译错误:
没有该方法"------可能你在理解上有偏差,放心不是误差:)

[1]给个正确的事例:

public class test
{
void function(int i)
{
System.out.println(i);
}
void function()
{
function(5);
}
public static void main(String[] args)
{
System.out.println("Hello World!");
test t=new test();
t.function();
}
}

---------- java ----------
Hello World!
5
Normal Termination
输出完成(耗费 0 秒)。


[2]给个错误的事例,让大家思考一下

public class test
{
void function(int i)
{
System.out.println(i);
}
void function()
{
function(5);
}
public static void main(String[] args)
{
System.out.println("Hello World!");
function();
}
}
---------- javac ----------
test.java:17: non-static method function() cannot be referenced from a static context function();
^
1 error
Normal Termination
输出完成(耗费 6 秒)。

为什么呢?因为function 不是静态的方法。

那么我们就修改一下这个test类中方法的定义
[3]
public class test
{
static void function(int i)
{
System.out.println(i);
}
static void function()
{
function(5);
}
public static void main(String[] args)
{
System.out.println("Hello World!");
function();
}
}


---------- java ----------
Hello World!
5
Normal Termination
输出完成(耗费 0 秒)。

呵呵,这下好了吧,我想大家都应该满意了,希望以后多多交流。

最后总结一下:
[1] java不支持默认参数
[2] 可以用动态中的重载函数来实现默认参数的功能
[3] 为了少写点代码可以用函数调用的方式:
void function(int i)
{
//...
}

void function()
{
function(5);
}

此致
antpower 2003-05-26
  • 打赏
  • 举报
回复
void function()
{
this(5);//应该这么用.

}
不可以,5的去向未知,也不知为何用,这里想要的默认值只是对函数有用,也不可能想让它
影响到整个类。
Gerry_Deng 2003-05-26
  • 打赏
  • 举报
回复
这样应该是不行的。
void function(int number){
function(int a)
{number=a;
}}
accp 2003-05-26
  • 打赏
  • 举报
回复
唉,就这一点不爽
疾风2002 2003-05-26
  • 打赏
  • 举报
回复
以下节选自java.uitl.Data类的源码:

/**
* Allocates a <code>Date</code> object and initializes it so that
* it represents the time at which it was allocated, measured to the
* nearest millisecond.
*
* @see java.lang.System#currentTimeMillis()
*/
public Date() {
this(System.currentTimeMillis());
}

/**
* Allocates a <code>Date</code> object and initializes it to
* represent the specified number of milliseconds since the
* standard base time known as "the epoch", namely January 1,
* 1970, 00:00:00 GMT.
*
* @param date the milliseconds since January 1, 1970, 00:00:00 GMT.
* @see java.lang.System#currentTimeMillis()
*/
public Date(long date) {
cal = null;
fastTime = date;
}

/**
* Allocates a <code>Date</code> object and initializes it so that
* it represents midnight, local time, at the beginning of the day
* specified by the <code>year</code>, <code>month</code>, and
* <code>date</code> arguments.
*
* @param year the year minus 1900.
* @param month the month between 0-11.
* @param date the day of the month between 1-31.
* @see java.util.Calendar
* @deprecated As of JDK version 1.1,
* replaced by <code>Calendar.set(year + 1900, month, date)</code>
* or <code>GregorianCalendar(year + 1900, month, date)</code>.
*/
public Date(int year, int month, int date) {
this(year, month, date, 0, 0, 0);
}

/**
* Allocates a <code>Date</code> object and initializes it so that
* it represents the instant at the start of the minute specified by
* the <code>year</code>, <code>month</code>, <code>date</code>,
* <code>hrs</code>, and <code>min</code> arguments, in the local
* time zone.
*
* @param year the year minus 1900.
* @param month the month between 0-11.
* @param date the day of the month between 1-31.
* @param hrs the hours between 0-23.
* @param min the minutes between 0-59.
* @see java.util.Calendar
* @deprecated As of JDK version 1.1,
* replaced by <code>Calendar.set(year + 1900, month, date,
* hrs, min)</code> or <code>GregorianCalendar(year + 1900,
* month, date, hrs, min)</code>.
*/
public Date(int year, int month, int date, int hrs, int min) {
this(year, month, date, hrs, min, 0);
}

/**
* Allocates a <code>Date</code> object and initializes it so that
* it represents the instant at the start of the second specified
* by the <code>year</code>, <code>month</code>, <code>date</code>,
* <code>hrs</code>, <code>min</code>, and <code>sec</code> arguments,
* in the local time zone.
*
* @param year the year minus 1900.
* @param month the month between 0-11.
* @param date the day of the month between 1-31.
* @param hrs the hours between 0-23.
* @param min the minutes between 0-59.
* @param sec the seconds between 0-59.
* @see java.util.Calendar
* @deprecated As of JDK version 1.1,
* replaced by <code>Calendar.set(year + 1900, month, date,
* hrs, min, sec)</code> or <code>GregorianCalendar(year + 1900,
* month, date, hrs, min, sec)</code>.
*/
public Date(int year, int month, int date, int hrs, int min, int sec) {
cal = null;
if (staticCal == null)
makeStaticCalendars();
synchronized (staticCal) {
staticCal.setTimeZone(TimeZone.getDefault());
staticCal.clear();
staticCal.set(year + 1900, month, date, hrs, min, sec);
fastTime = staticCal.getTimeInMillis();
}
}


to antpower(方向不对,换个姿势再睡.) :如果用function(5);会有编译错误:没有该方法
seven1996 2003-05-25
  • 打赏
  • 举报
回复
void function(int number=5)
{
...............
}
这样的函数我没见过
应该是这样:
void function ()
{
in number=5;
.............
}

void function(int number)
.....

这麻烦什么?
难道你的function只用在function(5)这一种情况??

PS: gaojunbo(飞马----结网ing)方法不一定要参数吧。构造函数。类库里面的许多函数都是不要参数的
antpower 2003-05-25
  • 打赏
  • 举报
回复
没办法了,只好用所谓的多态了。
gaojunbo 2003-05-25
  • 打赏
  • 举报
回复
调用的时候把方法的参数值赋上就行了,
void function(int i){}
function(1);

因为方法有参数,所以调用的时候必须有参数,不可能这样调用function();
crm2000 2003-05-25
  • 打赏
  • 举报
回复
没有默认的参数。
Layout 2003-05-25
  • 打赏
  • 举报
回复
to cooled(经典中--经过非典中):

void function()
{
this(5);//应该这么用.

}
???

这样可以吗?
antpower 2003-05-25
  • 打赏
  • 举报
回复
java里没有这种功能,但是可以用重载来实现:

void function(int i)
{
//...
}

void function()
{
function(5);
}

//还是这样吧,欢迎以后多多参与
疾风2002 2003-05-25
  • 打赏
  • 举报
回复
void function(int i)
{
//...
}

void function()
{
this(5);//应该这么用.

}
疾风2002 2003-05-25
  • 打赏
  • 举报
回复
void function(int i)
{
//...
}

void function()
{
function(5);
}
加载更多回复(2)

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧