重载练习1_四种不同参数类型的方法
生活随笔
收集整理的這篇文章主要介紹了
重载练习1_四种不同参数类型的方法
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
package cn.learn.day04.demo04;/*
題目要求:
比較兩個數(shù)據(jù)是否相等。
參數(shù)類型分別為兩個byte類型,兩個short類型,兩個int類型,兩個long類型,
并在main方法中進(jìn)行測試。*/
public class Demo02MethodOverloadSame {public static void main(String[] args) {byte a = 10;byte b = 20;System.out.println(isSame(a, b));System.out.println(isSame((short) 20, (short) 20));System.out.println(isSame(11, 12));System.out.println(isSame(10L, 10L));}public static boolean isSame(byte a, byte b) {System.out.println("兩個byte參數(shù)的方法執(zhí)行!");boolean same;if (a == b) {same = true;} else {same = false;}return same;}public static boolean isSame(short a, short b) {System.out.println("兩個short參數(shù)的方法執(zhí)行!");boolean same = a == b ? true : false;return same;}public static boolean isSame(int a, int b) {System.out.println("兩個int參數(shù)的方法執(zhí)行!");return a == b;}public static boolean isSame(long a, long b) {System.out.println("兩個long參數(shù)的方法執(zhí)行!");if (a == b) {return true;} else {return false;}}}
?
總結(jié)
以上是生活随笔為你收集整理的重载练习1_四种不同参数类型的方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 方法重载的基本使用
- 下一篇: 重载练习2_判断方法的正确重载