java中部分知识点的验证实现

java中运算符重载

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

(1)方法名相同;

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

注意:方法的返回值不作为方法重载的判断条件。

下面试一个运算符重载的一个简单程序以及输出结果

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));
        System.out.println("\nThe square of double 7.5*9-8.4 is " + square(7.5,9,8.4));
        System.out.println("\nThe square of double 7*8*9 is " + square(7,9,4));
    }

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

    public static double square(double y) {
        return y * y;

    }
    public static int square(int x,int y,int z)
    {
        return x*z*y;
    }
    public static double square(double a,int x,double y)
    {
        return a*x-y;
    }

随机数的生成

JDK提供了一个Random类,及Math.radom()可以更方便地生成随机数

相同“种子(seed)”的Random对象会生成相同的随机数。

通常使用以下方法生成较好的“随机数”,它以当前时间为“种子”。 Random ran = new Random( System.currentTimeMillis() );

 

posted on 2016-10-16 11:45  发酸的土豆  阅读(181)  评论(0编辑  收藏  举报