首页 > 技术文章 > 动手动脑

sin30 2017-10-14 01:08 原文

package Test;

//MethodOverload.java
//Using overloaded methods

public class dsadasd {

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) {
return x * x;
}

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

以上代码中对函数square进行了重载,形参的类型改变了分别用于整数,小数的平方。所以对于不同的实参调用不同的函数

 

package Test;

import java.util.Scanner;

 

public class Random {
public static void main(String[] args)
{
int i,num=1;
Scanner input=new Scanner(System.in);
System.out.println("请输入生成的随机整数的个数");
if(input.hasNextInt())
num=input.nextInt();
for(i=0;i<num;i++)
System.out.println((int)(Math.random()*100));
}
}

 

 

推荐阅读