01-方法--动手动脑

---恢复内容开始---

1.编写一个方法,使用以上算法生成指定数目(如1000个)的随机整数。

package 素数;

public class RandomNumber {
    public long a=12345L;//定义long类型的a,b,c变量
    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) {
        // TODO Auto-generated method stub
        RandomNumber r=new RandomNumber();
        long l;
        for(int i=1;i<1000;)//输出1000个随机数
        {
        l=r.rand();
        System.out.print(l+" ");
        i++;
        if (i%5==0)//每输入5个随机数后换行
        System.out.println("");
        }
        }
}

其运行结果为:

2.请看以下代码,你发现有什么特殊之处吗?

该代码运行结果如下:

特殊之处:

该实例代码展示了”方法重载“的特性,满足以下条件的两个或多个方法构成”重载关系“:1.方法名相同;2.参数类型、参数个数、或参数类型的顺序不同。(其中,方法的返回值不作为方法重载的判断条件)

3.查看JDK中System.out.println()的方法

System.out.println()的JDK源码为:
 Prints a String and then terminate the line. This method behaves as
 though it invokes <code>{@link #print(String)}</code> and then
 <code>{@link #println()}</code>.
 @param x The <code>String</code> to be printed.
浏览了一些博客发现,大量使用该语句会造成系统的一些性能受到影响。

猜你喜欢

转载自www.cnblogs.com/Qi77/p/9787304.html