时代Java,与您同行!
关注微信公众号,关注前沿技术,微信搜索:nowjava或时代Java,也可点击这里扫码关注
时代Java
首页
集册
文章
实例
项目
快讯
时代+
手册
下载
Jar查找
登录
注册
Java 创建和使用类
Java 创建和使用类
欢马劈雪
工程师 (已认证)
原创分享签约作者
发表于
实例源码
订阅
399
查看 / 运行 实例源码
创建和使用类
实例源码:
源代码:
执行
执行中...
public class Main { public static void main(String[] args) {/* 来 自 NowJava.com - 时 代 Java*/ // instantiate CommissionEmployee object Employee employee = new Employee("Ana", "Bates", "222-22-2222", 10000, .06); // get commission employee data System.out.println("Employee information obtained by get methods:"); System.out.printf("%n%s %s%n", "First name is", employee.getFirstName()); System.out.printf("%s %s%n", "Last name is", employee.getLastName()); System.out.printf("%s %s%n", "Social security number is", employee.getSocialSecurityNumber()); System.out.printf("%s %.2f%n", "Gross sales is", employee.getGrossSales()); System.out.printf("%s %.2f%n", "Commission rate is", employee.getCommissionRate()); employee.setGrossSales(5000); employee.setCommissionRate(.1); System.out.printf("%n%s:%n%n%s%n",// 来自 N o w J a v a . c o m "Updated employee information obtained by toString", employee); } } class Employee extends Object { private final String firstName; private final String lastName; private final String socialSecurityNumber; private double grossSales; // gross weekly sales private double commissionRate; // commission percentage // five-argument constructor public Employee(String firstName, String lastName, String socialSecurityNumber, double grossSales, double commissionRate) { // implicit call to Object's default constructor occurs here // if grossSales is invalid throw exception if (grossSales < 0.0) throw new IllegalArgumentException("Gross sales must be >= 0.0"); // if commissionRate is invalid throw exception if (commissionRate <= 0.0 || commissionRate >= 1.0) throw new IllegalArgumentException( "Commission rate must be > 0.0 and < 1.0"); this.firstName = firstName; this.lastName = lastName; this.socialSecurityNumber = socialSecurityNumber; this.grossSales = grossSales; this.commissionRate = commissionRate; } // return first name public String getFirstName() { return firstName; } // return last name public String getLastName() { return lastName; } // return social security number public String getSocialSecurityNumber() { return socialSecurityNumber; } // set gross sales amount public void setGrossSales(double grossSales) { if (grossSales < 0.0) throw new IllegalArgumentException("Gross sales must be >= 0.0"); this.grossSales = grossSales; } // return gross sales amount public double getGrossSales() { return grossSales; } // set commission rate public void setCommissionRate(double commissionRate) { if (commissionRate <= 0.0 || commissionRate >= 1.0) throw new IllegalArgumentException( "Commission rate must be > 0.0 and < 1.0"); this.commissionRate = commissionRate; } // return commission rate public double getCommissionRate() { /**代码未完, 请加载全部代码(NowJava.com).**/
编辑/阅读全部代码
执行结果:
Employee information obtained by get methods:
First name is Ana
Last name is Bates
Social security number is 222-22-2222
Gross sales is 10000.00
Commission rate is 0.06
Updated employee information obtained by toString:
commission employee: Ana Bates
social security number: 222-22-2222
gross sales: 5000.00
commission rate: 0.10
本文系作者在时代Java发表,未经许可,不得转载。如有侵权,请联系nowjava@qq.com删除。
编辑于
2020-03-26 09:23:27
2020-03-26 09:23:27
分享
分享文章到朋友圈
分享文章到 QQ
分享文章到微博
复制文章链接到剪贴板
扫描二维码
关注时代Java
实例源码
实例源码
订阅
订阅专栏
Java 判断文件是否为文本文件及获取文件编码格式的方法实例
bootstrap 实例演示下拉菜单(Dropdown)插件用法。
HashSet、LinkedHashSet、TreeSet类存储元素的自动排序规则实例测试
html css 对于 body和h1设置的实例源码
Java 获取在线网页的源代码
Java HashSet添加、迭代输出字符串的完整示例代码
Java 随机整数数组
html css 设置背景图片定位并且不平铺
扫描二维码
关注时代Java
返回顶部