赞
踩
用Java编写一个员工类程序:1.属性:员工编号,员工姓名,基本工资,奖金,2.构造方法:至少两个。如下:
package com.test;public class Employee { /** * 员工编号 */ private String number; /** * 员工姓名 */ private String name; /** * 员工薪水 */ private double salary; /** * 无参数构造函数 */ public Employee() { } /** * 给属性赋值构造函数 * @param number * @param name * @param salary */ public Employee(String number, String name, double salary) { super(); this.number = number; this.name = name; this.salary = salary; } public static void main(String[] args) { //员工一,并且构造函数里设置值 Employee e1 = new Employee("e0001", "xiaoming", 5000.0); System.out.println("员工一:" + e1); //员工二,用set设置值,get的话可以获取到员工某个属性 Employee e2 = new Employee(); e2.setName("小二"); e2.setNumber("e0002"); e2.setSalary(5500.1); System.out.println("员工二:" + e2); } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } @Override public String toString() { return "Employee [number=" + number + ", name=" + name + ", salary=" + salary + "]"; }}运行结果:员工一:Employee [number=e0001, name=xiaoming, salary=5000.0]员工二:Employee [number=e0002, name=小二, salary=5500.1]
取消
评论
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。