欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

方法重载的参数为基本类型

程序员文章站 2024-03-23 14:37:22
...

1. 基本类型从一个较小的类型自动提升到一个较大的类型。

package example4;

import org.junit.Test;

/*
 * @className PrimitiveOverloading
 * @author HowardAllen
 * @date 2018年7月29日 上午10:17:40
 * @Description 
 *
 */

public class PrimitiveOverloading {
	
	void f1(char x){System.out.print("f1(char)");}
	void f1(byte x){System.out.print("f1(byte)");}
	void f1(short x){System.out.print("f1(short)");}
	void f1(int x){System.out.print("f1(int)");}
	void f1(long x){System.out.print("f1(long)");}
	void f1(float x){System.out.print("f1(float)");}
	void f1(double x){System.out.print("f1(double)");}
	
	void f2(byte x){System.out.print("f2(byte)");}
	void f2(short x){System.out.print("f2(short)");}
	void f2(int x){System.out.print("f2(int)");}
	void f2(long x){System.out.print("f2(long)");}
	void f2(float x){System.out.print("f2(float)");}
	void f2(double x){System.out.print("f2(double)");}
	
	void f3(short x){System.out.print("f3(short)");}
	void f3(int x){System.out.print("f3(int)");}
	void f3(long x){System.out.print("f3(long)");}
	void f3(float x){System.out.print("f3(float)");}
	void f3(double x){System.out.print("f3(double)");}
	
	void f4(int x){System.out.print("f4(int)");}
	void f4(long x){System.out.print("f4(long)");}
	void f4(float x){System.out.print("f4(float)");}
	void f4(double x){System.out.print("f4(double)");}
	
	void f5(long x){System.out.print("f5(long)");}
	void f5(float x){System.out.print("f5(float)");}
	void f5(double x){System.out.print("f5(double)");}
	
	void f6(float x){System.out.print("f6(float)");}
	void f6(double x){System.out.print("f6(double)");}
	
	void f7(double x){System.out.print("f7(double)");}
	
	@Test
	public void testCinstVal(){
		f1(5);
		f2(5);
		f3(5);
		f4(5);
		f5(5);
		f6(5);
		f7(5);
	}
	// output: f1(int)f2(int)f3(int)f4(int)f5(long)f6(float)f7(double)
	// 因为默认整型为int
	
	@Test
	public void testChar(){
		char a = 'H';
		f1(a);
		f2(a);
		f3(a);
		f4(a);
		f5(a);
		f6(a);
		f7(a);
	}
	// output: f1(char)f2(int)f3(int)f4(int)f5(long)f6(float)f7(double)
	// 系统默认将char转为int
	
	@Test
	public void testByte(){
		byte a = 20;
		f1(a);
		f2(a);
		f3(a);
		f4(a);
		f5(a);
		f6(a);
		f7(a);
	}
	// output: f1(byte)f2(byte)f3(short)f4(int)f5(long)f6(float)f7(double)
	
	@Test
	public void testShort(){
		short a = 20;
		f1(a);
		f2(a);
		f3(a);
		f4(a);
		f5(a);
		f6(a);
		f7(a);
	}
	// output: f1(short)f2(short)f3(short)f4(int)f5(long)f6(float)f7(double)
	
	@Test
	public void testInt(){
		int a = 20;
		f1(a);
		f2(a);
		f3(a);
		f4(a);
		f5(a);
		f6(a);
		f7(a);
	}
	// output: f1(int)f2(int)f3(int)f4(int)f5(long)f6(float)f7(double)
	
	@Test
	public void testLong(){
		long a = 20;
		f1(a);
		f2(a);
		f3(a);
		f4(a);
		f5(a);
		f6(a);
		f7(a);
	}
	// output: f1(long)f2(long)f3(long)f4(long)f5(long)f6(float)f7(double)
	
	@Test
	public void testFolat(){
		float a = 20F;
		f1(a);
		f2(a);
		f3(a);
		f4(a);
		f5(a);
		f6(a);
		f7(a);
	}
	// output: f1(float)f2(float)f3(float)f4(float)f5(float)f6(float)f7(double)
	
	@Test
	public void testDouble(){
		double a = 20F;
		f1(a);
		f2(a);
		f3(a);
		f4(a);
		f5(a);
		f6(a);
		f7(a);
	}
	// output: f1(double)f2(double)f3(double)f4(double)f5(double)f6(double)f7(double)
}

2. 基本类型是一个较大的类型时,必须强制转换,否则无法通过编译。