有一道 java面向对象编程题 求大家帮我写一下 面向对象基础 看到迷茫
答案:1 悬赏:30 手机版
解决时间 2021-01-30 00:51
- 提问者网友:我是女神我骄傲
- 2021-01-29 01:39
有一道 java面向对象编程题 求大家帮我写一下 面向对象基础 看到迷茫
最佳答案
- 五星知识达人网友:低音帝王
- 2021-01-29 02:51
package employee;
public class Employee {
//员工私有属性
private String name; //员工姓名
private int age; //员工年龄
private String position; //员工职位
private int salary; //工资
public Employee(String name,int age,String position,int salary){
this.name = name;
this.age =age;
this.position = position;
this.salary = salary;
}
public void setName(String name){
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age){
if(age<18){
this.age=18;
System.out.println("当年龄无效时,默认为18");
}else{
this.age =age;
}
}
public int getAge() {
return age;
}
public void setPosition(String position){
if(position.equals("售后服务") || position.equals("销售员") ){
this.position = position;
}else{
this.position = "售后服务";
System.out.println("输入不符合要求,默认为售后服务");
}
}
public String getPosition() {
return position;
}
public void setSalary(){
if(age>=18 && age<=20){
this.salary = 1000;
}else if(age>=21 && age<=25){
this.salary = 1500;
}else if(age>=26 && age<=30){
this.salary = 2000;
}else if(age>=31 && age<=40){
this.salary = 3000;
}else if(age>=41 && age<=50){
this.salary = 3500;
}else if(age>=51){
this.salary = 4000;
}else{
System.out.println("没有设置年龄或者年龄无效");
}
}
public int getSalary() {
return salary;
}
}
测试类
package employee;
import java.util.Scanner;
public class TestEmployee {
public static void main(String[] args){
Employee emp1 = new Employee(null, 0, null, 0) ;
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个员工姓名");
//获取输入的名字
String name1 = sc.next();
emp1.setName(name1);
System.out.println("请输入第一个员工年龄");
//获取输入的年龄
int age1 = sc.nextInt();
emp1.setAge(age1);
System.out.println("请输入第一个员工职位");
//获取输入的职位
String position1 = sc.next();
emp1.setPosition(position1);
emp1.setSalary();
System.out.println("---------------------------------");
System.out.println("员工1姓名为:"+emp1.getName());
System.out.println("年龄:"+emp1.getAge());
System.out.println("工作为:"+emp1.getPosition());
System.out.println("工资为:"+emp1.getSalary());
}
}
第一次回答问题,玩玩而已但是还是希望采纳
public class Employee {
//员工私有属性
private String name; //员工姓名
private int age; //员工年龄
private String position; //员工职位
private int salary; //工资
public Employee(String name,int age,String position,int salary){
this.name = name;
this.age =age;
this.position = position;
this.salary = salary;
}
public void setName(String name){
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age){
if(age<18){
this.age=18;
System.out.println("当年龄无效时,默认为18");
}else{
this.age =age;
}
}
public int getAge() {
return age;
}
public void setPosition(String position){
if(position.equals("售后服务") || position.equals("销售员") ){
this.position = position;
}else{
this.position = "售后服务";
System.out.println("输入不符合要求,默认为售后服务");
}
}
public String getPosition() {
return position;
}
public void setSalary(){
if(age>=18 && age<=20){
this.salary = 1000;
}else if(age>=21 && age<=25){
this.salary = 1500;
}else if(age>=26 && age<=30){
this.salary = 2000;
}else if(age>=31 && age<=40){
this.salary = 3000;
}else if(age>=41 && age<=50){
this.salary = 3500;
}else if(age>=51){
this.salary = 4000;
}else{
System.out.println("没有设置年龄或者年龄无效");
}
}
public int getSalary() {
return salary;
}
}
测试类
package employee;
import java.util.Scanner;
public class TestEmployee {
public static void main(String[] args){
Employee emp1 = new Employee(null, 0, null, 0) ;
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个员工姓名");
//获取输入的名字
String name1 = sc.next();
emp1.setName(name1);
System.out.println("请输入第一个员工年龄");
//获取输入的年龄
int age1 = sc.nextInt();
emp1.setAge(age1);
System.out.println("请输入第一个员工职位");
//获取输入的职位
String position1 = sc.next();
emp1.setPosition(position1);
emp1.setSalary();
System.out.println("---------------------------------");
System.out.println("员工1姓名为:"+emp1.getName());
System.out.println("年龄:"+emp1.getAge());
System.out.println("工作为:"+emp1.getPosition());
System.out.println("工资为:"+emp1.getSalary());
}
}
第一次回答问题,玩玩而已但是还是希望采纳
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯