集册 Java实例教程 创建和使用类

创建和使用类

欢马劈雪     最近更新时间:2020-01-02 10:19:05

585
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
创建和使用类

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 
展开阅读全文