简单Java类的开发原则:
1、类名称必须具有实际意义;
2、类中属性需要使用private封装;
3、封装属性需要编写setter、getter方法;
4、可定义若干构造方法,但必须保留一个无参构造方法;
5、类中不允许有任何输出操作,需作为返回值输出;
代码如下://定义一个员工类,包括:员工编号,姓名,工作,薪水以及补贴
class Emp{
private int empno;
private String ename;
private String job;
private double sal;
private double comm;
public Emp(){}
public Emp(int no,String name,String j,double s,fouble c){
setNo(no);
setName(name);
setJob(j);
setSal(s);
setComm(c);
}
public int setNo(int no){
empno = no;
}
public String setName(String name){
ename = name;
}
public String setJob(String j){
job = j;
}
public double setSal(double s){
sal = s;
}
public double setComm(double s){
comm = s;
}
public String getValue(){
return "编号:"+empno+"姓名:"+ename+"工作:"+job+"薪水:"+sal+"补贴:"+comm;
}
}
//调用员工类并输出员工基本信息
public class mainDemo{
public static void main(String args[]){
Emp emp = new Emp("1","张三","清洁",3000,0);
System.out.println(emp.getValue());
}
}