编程语言
首页 > 编程语言> > 实战:简单Java类

实战:简单Java类

作者:互联网

对于简单Java类而言,其核心开发结构如下:

实例

class Dept { //类名称可以明确描述某类事物
    private long deptno;
    private String dname;
    private String loc;

    public Dept() {
    } //必须提供无参构造

    public Dept(long deptno, String dname, String loc) {
        this.deptno = deptno;
        this.dname = dname;
        this.loc = loc;
    }

    public String getInfo(){
        return "【部门信息】部门编号:"+this.deptno+"、部门名称:"+this.dname+"、部门位置:"+this.loc;
    }
    public void setDeptno(long deptno) {
        this.deptno = deptno;
    }

    public void setDname(String dname) {
        this.dname = dname;
    }

    public void setLoc(String loc) {
        this.loc = loc;
    }

    public long getDeptno() {
        return this.deptno;
    }

    public String getDname() {
        return this.dname;
    }

    public String getLoc() {
        return this.loc;
    }
}

public class Demo{
    public static void main(String[] args) {
        Dept dept=new Dept(10,"技术部","北京");
        System.out.println(dept.getInfo());
    }
}

标签:实战,loc,Java,String,dname,Dept,简单,deptno,public
来源: https://blog.csdn.net/z1360408752/article/details/115056025