二 函数以及函数的重载

二 函数以及函数的重载

/**
 * 函数(方法)
 * 函数的基本格式:
 * [访问权限] 返回类型  函数名 ([参数列表...])
 * {
 *   函数代码块;
 *   [return (返回值);]
 * }
 *
 * 函数的重载:
 * 可以通过不改变函数名只改变函数的
 *   返回类型(必须伴随参数的改变)、
 *   参数类型、
 *   参数数目、
 *   颠倒不同类型的参数顺序
 * 的方式来对函数进行重载。
 */
public class Function
{
 /*第一段(函数的创建和基本调用)*/
 public void func1()//无参数的void类型函数
 {
  System.out.println("void");
  //即使没有return语句,void类型函数也会在该函数结束前(右花括号前)自动加上一个return;
  return;
 }
 public void func2(int i)//带参数的void类型函数
 {
  if(i <= 0)
   return;//return;可以提前跳出该函数体.
  System.out.println(i);
 }
 public byte /*char short int long float double*/func3()//无参数的基本类型函数
 {
  return 1+1;
 }
 public byte /*char short int long float double*/func4(int i,int j)//带参数的基本类型函数
 {
  if((i+j) >127 || (i+j) < 0 )
   return -1;
  return (byte)(i + j);//注意类型的转换.
 }
 public String/* Object Array[]*/ func5(String s)//返回类型是类类型的函数
 {
  return s;
 }
 public Function func6(Object obj)//返回类型可以是该类本身
 {
  if(obj instanceof Function)
  {
   return (Function)obj;
  }
  else
   return null;
 }
 public String toString()//重写了java.lang.Object的toString方法
 {
  return "function";//这样在打印这个类的对象是将打印"function"这句话.
 }
 public int[] func7(int...integers)//返回类型是数组的可变参数函数
 {
  int[] inner = new int[integers.length];
  for(int i = 0;i < inner.length; i++)
  {
   inner[i] = integers[i];
  }
  return inner;
 }
 public boolean func8(Object obj)
 {
  if(obj instanceof Function)
   return true;
  else
   return false;
 }
 /*第二段(函数的重载)*/
 /**
  * 改变参数
  */
 public void reloadArgs()//无参的函数
 {
  System.out.println("void and no args");
 }
 public void reloadArgs(int i)//添加一个int类型参数对函数进行重载
 {
  System.out.println("void and one int args");
 }
 public void reloadArgs(int i,int j)//添加两个int类型参数对函数进行重载
 {
  System.out.println("void and two int args");
 }
 public void reloadArgs(int i,byte j)//改变参数的类型对函数进行重载
 {
  System.out.println("void and two args a int other byte");
 }
 public void reloadArgs(int i,String j)//改变参数的类型对函数进行重载
 {
  System.out.println("void and two args a int other String");
 }
 public void reloadArgs(String i,int j)//交换两个不同参数的位置对函数进行重载
 {
  //这种重载方式没有任何意义,不推荐使用.
  System.out.println("void and two args a String other int");
 }
 //public void reloadArgs(int j,int i){} 错误!
 //交换的参数必须是不同类型才能达到重载效果.
 
 /**
  * 改变返回类型
  * 必须伴随参数的改变,不允许只改变函数的返回类型来对函数进行重载.
  */
 public void reloadType()
 {
  System.out.println("type void");
 }
 //public int reloadType(){} 错误!
 //不能通过改变函数的返回类型来对函数进行重载!
 public int reloadType(int i)
 {
  return i;
 }
 public String reloadType(String str)
 {
  return str;
 }
 public Function reloadType(Function f)
 {
  return f;
 }
 public static void main(String[] args)
 {
  Function fun = new Function(); 
  System.out.println("调用func1");
  fun.func1();
  System.out.println("调用func2");
  fun.func2(3);
  System.out.println("调用func3");
  System.out.println(
    fun.func3()
   );
  System.out.println("调用func4");
  System.out.println(
    fun.func4(12, 33) + " " +
    fun.func4(127, 1) + " " +
    fun.func4(-1, -2)
   );
  System.out.println("调用func5");
  Function fun1 = new Function();
  System.out.println(
    fun.func5("func5")
   );
  System.out.println("调用func6");
  System.out.println(
    fun.func6(fun1)//自动调用toString()方法,打印"function"这句话.
   );
  System.out.println(
    fun.func6("abc")
   );
  System.out.println("调用func7");
  for(int i = 0; i<fun.func7(11,12,13,14).length; i++)
  {
   System.out.println(fun.func7(11,12,13,14)[i]);
  }
  System.out.println("调用func8");
  System.out.println(
    fun.func8(fun1) + " " +
    fun.func8("abc")
   );
  System.out.println("函数的 重载");
  fun.reloadArgs();
  fun.reloadArgs(10);
  fun.reloadArgs(10, (byte)20);//对于整数类型的参数要注意通过强制类型转换对类型指明.
  fun.reloadArgs(10, 20);
  fun.reloadArgs(10, "abc");
  fun.reloadArgs("abc", 10);//交换两个不同类型的参数位置
  
  fun.reloadArgs();
  System.out.println(
    fun.reloadType(30) + "/n" +
    fun.reloadType("bcd") + "/n" +
    fun.reloadType(fun1)
   );
 }
}
/* ~~Console out~~
调用func1
void
调用func2
3
调用func3
2
调用func4
45 -1 -1
调用func5
func5
调用func6
function
null
调用func7
11
12
13
14
调用func8
true false
函数的 重载
void and no args
void and one int args
void and two args a int other byte
void and two int args
void and two args a int other String
void and two args a String other int
void and no args
30
bcd
 */

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值