以下节选自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);会有编译错误:没有该方法
这样应该是不行的。 void function(int number){ function(int a) {number=a; }}
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); }此致
java中不支持参数默认值的问题你可以通过构造函数实现呀!!!
其实可以对比一下Java和C++ 在C++中是允许有默认参数的 但是在这种情况下有的时候会引起重载函数的模糊调用 而Java从这种方面考量,没有加入这种特性 但是其实你可以这么写:public void function( int number ) { ...... }public void function() { this.function(5); }这样写来,你依然觉得麻烦吗?^_^
{
...............
}
这样的函数我没见过
应该是这样:
void function ()
{
in number=5;
.............
}void function(int number)
.....这麻烦什么?
难道你的function只用在function(5)这一种情况??PS: gaojunbo(飞马----结网ing)方法不一定要参数吧。构造函数。类库里面的许多函数都是不要参数的
{
...............
}
这样的函数在C++中是一定有的,java里看来是没有了。
我觉得有了这种带默认参数的函数在一定程度上可以省掉一些重载函数。这样说吧,如果支持默认参数的话,我们就可以这样使用上面那个函数:
function();
这是完全可以运行的(可惜java不支持)。
所以才想到用多态去实现,所以不能不承认c++在一定程度上比java要灵活一些。呵呵,如此
这个问题我想该差不多已经解决了,对不?
java里没有这种功能,但是可以用重载来实现:void function(int i)
{
//...
}void function()
{
function(5);
}
{
//...
}void function()
{
function(5);
}
{
//...
}void function()
{
this(5);//应该这么用.}
{
//...
}void function()
{
function(5);
}//还是这样吧,欢迎以后多多参与
{
this(5);//应该这么用.}
???这样可以吗?
{
this(5);//应该这么用.}
不可以,5的去向未知,也不知为何用,这里想要的默认值只是对函数有用,也不可能想让它
影响到整个类。
* 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);会有编译错误:没有该方法
void function(int number){
function(int a)
{number=a;
}}
"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);
}此致
在C++中是允许有默认参数的
但是在这种情况下有的时候会引起重载函数的模糊调用
而Java从这种方面考量,没有加入这种特性
但是其实你可以这么写:public void function( int number )
{
......
}public void function()
{
this.function(5);
}这样写来,你依然觉得麻烦吗?^_^