java语言

java继承抽象类

时间:2024-07-28 17:07:57 java语言 我要投稿
  • 相关推荐

java继承抽象类

  Java具有简单性、面向对象、分布式、健壮性、安全性、独立与可移植性、多线程、动态性等特点。Java可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序等。以下是小编为大家搜索整理的java继承抽象类,欢迎大家阅读!更多精彩内容请及时关注我们应届毕业生考试网!

  /* 文件名 : Salary.java */

  public class Salary extends Employee

  {

  private double salary; //Annual salary

  public Salary(String name, String address, int number, double

  salary)

  {

  super(name, address, number);

  setSalary(salary);

  }

  public void mailCheck()

  {

  System.out.println("Within mailCheck of Salary class ");

  System.out.println("Mailing check to " + getName()

  + " with salary " + salary);

  }

  public double getSalary()

  {

  return salary;

  }

  public void setSalary(double newSalary)

  {

  if(newSalary >= 0.0)

  {

  salary = newSalary;

  }

  }

  public double computePay()

  {

  System.out.println("Computing salary pay for " + getName());

  return salary/52;

  }

  }

  尽管我们不能实例化一个Employee类的对象,但是如果我们实例化一个Salary类对象,该对象将从Employee类继承3个成员变量和7个成员方法。

  /* 文件名 : AbstractDemo.java */

  public class AbstractDemo

  {

  public static void main(String [] args)

  {

  Salary s = new Salary("Mohd Mohtashim", "Ambehta, UP", 3, 3600.00);

  Employee e = new Salary("John Adams", "Boston, MA", 2, 2400.00);

  System.out.println("Call mailCheck using Salary reference --");

  s.mailCheck();

  System.out.println(" Call mailCheck using Employee reference--");

  e.mailCheck();

  }

  }

  以上程序编译运行结果如下:

  Constructing an Employee

  Constructing an Employee

  Call mailCheck using Salary reference --

  Within mailCheck of Salary class

  Mailing check to Mohd Mohtashim with salary 3600.0

  Call mailCheck using Employee reference--

  Within mailCheck of Salary class

  Mailing check to John Adams with salary 2400.

【java继承抽象类】相关文章:

java的继承与组合的区别07-19

接口和抽象类的区别11-19

java教程之Java编程基础12-03

Java是什么12-04

JAVA的就业方向11-19

Java 的分支结构11-29

Java语言的优点12-05

java的多线程12-04

java多线程03-27