动手动脑10.14

一、

public class Suiji{
public long a=12345L;
public long c=12345L;
public long m=456123L;
public long r=1;
public long rand() {
r=(r*a+c)%m;
return r;
}
public static void main(String[] args) {
Suiji s=new Suiji();
long r;
for(int i=1;i<1000;i++) {
r=s.rand();
System.out.println(r);
}
}
}

 

public class MethodOverload {

public static void main(String[] args) {
System.out.println("The square of integer 7 is " + square(7));
System.out.println("\nThe square of double 7.5 is " + square(7.5));
}

public static int square(int x)//int类型
{
return x * x;
}

public static double square(double y)//double类型
{
return y * y;
}
}

输出结果

The square of integer 7 is  49

The square of double 7.5 is 56.25

该程序中所使用两个函数虽然名字相同,但由于实参类型不同且返回值类型不同,并不会发生冲突。

上述示例代码展示了Java的“方法重载(overload)”特性。

满足以下条件的两个或多个方法构成“重载”关系:

(1)方法名相同;

(2)参数类型不同,参数个数不同,或者是参数类型的顺序不同。

System.out.println()方法中实参表内可输入很多类型。

猜你喜欢

转载自www.cnblogs.com/quyangzhangsiyuan/p/9786208.html