返回类型:
- void
- int
- boolean
- String
- class
- …
一般在写一个方法的时候(构造函数除外),格式为:
权限 静态(非) 返回类型 方法名(参数)
{
//方法体
}
当该方法为静态static时,可以直接调用,无需通过对象调用
当返回类型为void时:无需返回值
//权限 静态 返回类型 方法名
public static void f1()
{
}
//该方法为静态 可以直接调用
//权限 非静态(可省略) 返回类型 方法名
public void f2()
{
}
当返回类型为int时, 方法调用结束后需返回一个int型的量
//权限:共有 非静态(可省略) 返回类型:整型 函数名 (参数)
public int f3(int x)
{
return x;
}
//权限:共有 非静态(可省略) 返回类型:整型 函数名 (参数)
public int f4()
{
int x = 0;
return x;
}
当返回类型为bool类型,方法调用结束时需返回true 或 false
//权限:公有 非静态(可省略) 返回类型:bool 函数名 (参数)
public boolean f5(boolean x)
{
return x;
}
//权限:公有 非静态(可省略) 返回类型:bool 函数名 (参数)
public boolean f6()
{
boolean x = false;
return x;
}
当返回类型为类类型,最后方法调用结束时需返回类
public class Test {
//权限 类名 方法名
public Test f7()
{
return this;
}
}