Home > Java > javaTutorial > Comprehensive practice in JAVA development: write a simple Java class yourself

Comprehensive practice in JAVA development: write a simple Java class yourself

无忌哥哥
Release: 2018-07-19 11:45:41
Original
2281 people have browsed it

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());
    }
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template