方法的基本定义

方法是一段可以重复调用的代码,

package way;
//编写一个java简单类:雇员 员工编号,员工姓名,职位,员工工资,员工佣金
class Emp
{
private String empno;
private String name;
private String job;
private int sal;
private int comm;
public Emp() {}
public Emp(String eno,String na,String j,int s,int c) 
{
empno=eno;
name=na;
job=j;
sal=s;
comm=c;
}
public void setEmpno(String eno) 
{
empno=eno;
}
public void setName(String na)
{
name=na;
}
public void setJob(String j)
{
job=j;
}
public void setSal(int s)
{
sal=s;
}
public void setComm(int c)
{
comm=c;
}
public String getEmpno() 
{
return empno;
}
public String getName()
{
return name;
}
public String getJob()
{
return job;
}
public int getSal()
{
return sal;
}
public int getComm() 
{
return comm;
}
public String getInfo()
{
return "编号:"+getEmpno()+"\n"+
"姓名:"+getName()+"\n"+
"职位:"+getJob()+"\n"+
"工资:"+getSal()+"\n"+
"佣金:"+getComm()+"\n";
}
}
public class jdjava {
public static void main(String[] args) {
// TODO Auto-generated method stub
//System.out.println(new Emp("2ew13","namde","记者",20,40).getInfo());
    Emp emp=new Emp();
    emp.setEmpno("2ew13");
    emp.setName("namde");
    emp.setJob("记者");
    emp.setSal(20);
    emp.setComm(30);
    System.out.println(emp.getInfo());
}

}

2.由主方法调用

package way;
//编写一个java简单类,部门信息类(部门编号,名称,位置)
class Dept
{
private int deptno;
private String name;
private String loc;
public Dept() {}
public Dept(int tno,String na,String oc) 
{
this.setDeptno(tno);
this.setName(na);
this.setLoc(oc);
}
public void setDeptno(int tno) 
{
deptno=tno;
}
public void setName(String na) {
name=na;
}
public void setLoc(String oc) 
{
loc=oc;
}
public int getDeptno() 
{
return deptno;
}
public String getName()
{
return name;
}
public String getLoc() 
{
return loc;
}
public String getInfo() {
return "部门编号:"+getDeptno()+"\n"+
"名称:"+getName()+"\n"+
"位置:"+getLoc();

}
}
public class jdjavaa {
public static void main(String[] args)
{
System.out.println(new Dept(7356,"SFSFD","DSDSF").getInfo());
}

}

3.方法重载(OVERLOAD)

//方法重载 方法名相同,参数类型和个数不同
public class overload {
  public static void main(String[] args) 
  {
 System.out.println(add(10,20));
 System.out.println(add(10,20,30));
 System.out.println(add(10.1,20.1));
  }
  public static int add(int x,int y) 
  {
 return x+y;
  }
  public static int add(int x,int y,int z)
  {
 return x+y+z;
  }
  public static double add(double x,double y)
  {
return x+y; 
  }

}

//方法重载的概念中,并没有对返回值由统一要求。但是返回值最好相同,这属于人为开发的规定,而不是语法规定

4方法递归调用

public class recursion {
public static void main(String agrs[]) 
{
/*int i=1;
int max=0;
while(i<=100)
{
//max+=i++;
max+=i;
i++;
}
System.out.println(max);*/
//下面用递归调用来实现
System.out.print(sum(100));
}
public static int sum(int x) 
{
if(x==1)
{
return 1;
}
return x+sum(x-1);
}
/*
第一次调用sum(由主方法调用):return 100+sun(99);
第二次调用sum(自身调用):return  99+sum(98)
倒数二次调用sum(自身调用):return 2+sum(1);
倒数一次调用sum(自身调用):return 1;
最终 return 99+98+97+...+2+1;*/
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值