Home >Java >javaTutorial >Comprehensive practice in JAVA development: write a simple Java class yourself
Development principles of simple Java classes:
1. The class name must have practical meaning;
2. The attributes in the class need to be encapsulated privately;
3. To encapsulate properties, you need to write setter and getter methods;
4. Several construction methods can be defined, but one parameterless construction method must be retained;
5. In the class No output operation is allowed and must be output as a return value;
The code is as follows:
//定义一个员工类,包括:员工编号,姓名,工作,薪水以及补贴 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()); } }
The above is the detailed content of Comprehensive practice in JAVA development: write a simple Java class yourself. For more information, please follow other related articles on the PHP Chinese website!