今天看书看到了方法重载,于是自己写了一个程序,可编译的时候出错,希望网友给出正确答案,并说明我错在什么地方,及日后如何避免同类错误!
程序如下:
public class Maxint{
public Integer Maxint(int a,int b){
if (a>b)
return a;
else
return b;
}
public Integer Maxint(int a,int b,int c){
if (a>b)
return a+c;
else
return b+c;
}
public static void Main(String[] args){
int a=3;
int b=6;
int c=4;
int d;
d=Maxint(a,b);
System.out.println(d);
d=Maxint(a,b,c);
System.out.println(d);
}
}
程序用途是返回两个数中的大数;或三个数中前两个数中的大数与第三个数的和!
程序如下:
public class Maxint{
public Integer Maxint(int a,int b){
if (a>b)
return a;
else
return b;
}
public Integer Maxint(int a,int b,int c){
if (a>b)
return a+c;
else
return b+c;
}
public static void Main(String[] args){
int a=3;
int b=6;
int c=4;
int d;
d=Maxint(a,b);
System.out.println(d);
d=Maxint(a,b,c);
System.out.println(d);
}
}
程序用途是返回两个数中的大数;或三个数中前两个数中的大数与第三个数的和!
解决方案 »
- switch(整形表达式)中的整形表达式为什么不能是long型?
- java面试题
- 关于java浮点型操作报空置针异常
- 关于JAVA BEAN的一点问题
- 哪位仁兄有有关检索方面的资料?
- eclipse 与数据库连接问题
- 各位高手,有个小问题能帮我解答一下么?
- http://community.csdn.net/Expert/topic/3232/3232024.xml?temp=.5569879
- 怎样得到操作系统的安装路径?
- 调查一下,因某方面的考虑,你用jdk是1.3还是1.4?
- 浮点数加法精度问题,两种处理方式结果不同,大家帮忙看看!!
- Swing 两个JList如何共用一个 litener
public class Maxint{
public static Integer Maxint(int a,int b){
if (a>b)
return a;
else
return b;
}
public static Integer Maxint(int a,int b,int c){
if (a>b)
return a+c;
else
return b+c;
}
public static void Main(String[] args){
int a=3;
int b=6;
int c=4;
int d;
d=Maxint(a,b);
System.out.println(d);
d=Maxint(a,b,c);
System.out.println(d);
}
}
要么创建一个该类的对象,调用之;
第二:尽量不要将你的方法的名字和类的名字相同。
Maxint.java:19: 无法从静态上下文中引用非静态 方法 maxint(int,int)
d=maxint(a,b);
^
Maxint.java:21: 无法从静态上下文中引用非静态 方法 maxint(int,int,int)
d=maxint(a,b,c);
^
2 错误
错误还是一样!
public static Integer Maxint(int a,int b){
if (a>b)
return a;
else
return b;
}
public static Integer Maxint(int a,int b,int c){
if (a>b)
return a+c;
else
return b+c;
}
public static void main(String[] args){
int a=3;
int b=6;
int c=4;
int d;
d=Maxint(a,b);
System.out.println(d);
d=Maxint(a,b,c);
System.out.println(d);
}
}
Integer是个类,int只是基本数据类型,行吗这样??
不行!
1、凡是以static修饰的方法所调用的方法都应该以static修饰。
2、方法名可以与类重名(我试过,可以!),但最好不要重名,以便程序阅读。
3、程序入口中的“main”一定是要用小写,刚才一不小心给写成大写了。
4、如果想在static修饰的方法下调用非static修饰的方法,一定在生成实例。
以上的总结不知道是否正确,请各位网友给出意见,对于不对,或者我理解错的地方,我也好改正!
谢谢大家!
以下是正确程序的代码:
public class Maxint{
public static Integer Maxint(int a,int b){
if (a>b)
return a;
else
return b;
}
public static Integer Maxint(int a,int b,int c){
if (a>b)
return a+c;
else
return b+c;
}
public static void main(String[] args){
int a=3;
int b=6;
int c=4;
int d;
d=Maxint(a,b);
System.out.println(d);
d=Maxint(a,b,c);
System.out.println(d);
}
}
public static void main(String[] args){
int a=3;
int b=6;
int c=4;
int d;
d=Maxint(a,b);
System.out.println(d);
d=Maxint(a,b,c);
System.out.println(d);
}
有问题,
Maxint()方法是对象实例化以后才能调用的,不是类的静态方法,不能这么调用。
如果想调用,只能另外写个测试类,
在这个测试类中实例化一个对象,
对象的main方法
public static void main(String[] args){
int a=3;
int b=6;
int c=4;
int d;
Maxint m=new Maxint();
d=m.Maxint(a,b);
System.out.println(d);
d=m.Maxint(a,b,c);
System.out.println(d);
}
或者把Maxint定义为静态方法。