澳门新葡亰平台官网Java之面向对象(二)

by admin on 2020年1月31日

**************************************************************

构造函数没有返回值且必须与类名同名。
当类的属性为privated时,其他类可以通过get和set方法访问它。
现在来看看写的简单的类:
Employee.java
package qingke1;

public class Employee {

public abstract class Employee{
 private String firstName;
 private String lastName;
 private String socialSecurityNumber;
 public Employee(String first,String last,String ssn){
  firstName=first;
  lastName=last;
  socialSecurityNumber=ssn;
 }
 public void setFirstName(String first){firstName=first;}
澳门新葡亰平台官网, public String getFirstName(){return firstName;}
 public void setLastName(String last){lastName=last;}
 public String getLastName(){return lastName;}
 public void setSocialSecurityNumber(String
number){socialSecurityNumber=number;}
 public String getSocialSecurityNumber(){return socialSecurityNumber;}
 public String toString(){
  return getFirstName()+”
“+getLastName()+”n社会保险号:”+getSocialSecurityNumber();
 }
 public abstract double earnings();
}

public class Employee {
private int id;
private String firstName;
private String lastName;
private int salary;
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public Employee(int id,String firstName,String lastName,int salary){
this.id=id;
this.firstName=firstName;
this.lastName=lastName;
this.salary=salary;
}
public int getID(){
return id;
}
public String getFirstName(){
return firstName;
}
public String getLastName(){
return lastName;
}
public String getName(){
return firstName+lastName;
}
public int getAnnualSalary(){
return salary12;
}
public int raiseSalary(int percent){
if(percent>0){
double a =(double)percent/100;
salary = (int) (salary
(1+a));
}
return salary;
}
public String toString(){
return
“Employee[id=”+id+”,name=”+firstName+lastName+”,salary=”+salary+”]”;
}

private int id;

*************************************************************

}

private String firstName;

public class BasePlusCommissionEmployee extends CommissionEmployee{
 private double baseSalary;
 public BasePlusCommissionEmployee(String first,String last,String
socialSecurityNumber,double grossSalesAmount,double rate,double
baseSalaryAmount){
  super(first,last,socialSecurityNumber,grossSalesAmount,rate);
  setBaseSalary(baseSalaryAmount);
 }
 public void setBaseSalary(double salary){
  baseSalary=salary<0.0?0.0:salary;
 }
 public double getBaseSalary(){
  return baseSalary;
 }
 public double earnings(){
  return getBaseSalary()+super.earnings();
 }
 public String toString(){
  return “n拿薪金的代理人:”+super.getFirstName()+”
“+super.getLastName()+”n社会保险号:”+super.getSocialSecurityNumber();
 }
}
*************************************************************  

private String lastName;

public class CommissionEmployee extends Employee{
 private double grossSales;
 private double commissionRate;
 public CommissionEmployee(String first,String last,String
socialSecurityNumber,double grossWeeklySales,double percent){
  super(first,last,socialSecurityNumber);
  setGrossSales(grossWeeklySales);
  setCommissionRate(percent);
 }
 public void setCommissionRate(double rate){
  commissionRate=(rate>0.0&&rate<1.0)?rate:0.0;
 }
 public double getCommissionRate(){
  return commissionRate;
 }
 public void setGrossSales(double sales){
  grossSales=sales<0.0?0.0:sales;
 }
 public double getGrossSales(){
  return grossSales;
 }
 public double earnings(){
  return getCommissionRate()*getGrossSales();
 }
 public String toString(){
  return “n代理人:”+super.toString();
 }
}

private int salary;

*************************************************************

public static void main(String[] args) {

public class HourlyEmployee extends Employee{
 private double wage;
 private double hours;
 public HourlyEmployee(String first,String last,String
socialSecurityNumber,double hourlyWage,double hourlyWorked){
  super(first,last,socialSecurityNumber);
  setWage(hourlyWage);
  setHours(hourlyWorked);
 }
 public void setWage(double
wageAmount){wage=wageAmount<0.0?0.0:wageAmount;}
 public double getWage(){return wage;}
 public void setHours(double
hoursWorked){hours=(hoursWorked>=0.0&&hoursWorked<=168.0)?hoursWorked:0.0;
 }
 public double getHours(){return hours;}
 public double earnings(){
  if(hours<=40)
   return wage*hours;
  else
   return 40*wage+(hours-40)*wage*1.5;
 }
 public String toString

Employee e2=new Employee(8, “李”, “聪聪”, 9988);

System.out.println(“月薪为:  “+e2.getSalary());

System.out.println(“年薪为:  “+e2.getAnnualSalary());

System.out.println(“全名为:  “+e2.getName());

System.out.println(“工号为:  “+e2.getID());

}

public Employee(int id,String firstName,String lastName,int salary){

this.id=id;

this.firstName=firstName;

this.lastName=lastName;

this.salary=salary;

}

public int getID(){

return id;

}

public String getFirstName(){

return firstName;

}

public String getLastName(){

return lastName;

}

public String getName(){

return firstName+lastName;

}

public int getSalary(){

return salary;

}

public void setSalary(int salary){

}

public int getAnnualSalary(){

return salary*12;

}

public int raiseSalary(int percent){

return (int)(salary*(1+(double)percent/100));

}

public String toString(){

return null;

}

}

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图