初识java继承



实验练习   Employee层次结构

 

 

程序模板是一个完整的、可实际运行的JAVA程序,其中关键的1行或者多行代码已经替换为注释。请先阅读问题描述,分析示例输出;然后研究模板的代码。参考问题解答提示,用JAVA代码替换注释。编译并执行程序,并将输出结果与提供的示例输出进行比较。然后回答后续问题。

 

1、 实验目的

建立一个类层次结构

创建正确调用超类构造函数的子类构造函数

使用类层次结构中的类

将某个类添加到现有的类层次结构中

2、 问题描述

创建类图中的各个类。Employee类应有姓、名和社会保险号。除以上这些外,SalariedEmployee类还应有周薪;HourlyEmployee类还应有每小时工资和工作时数;

CommissionEmployee还应有佣金率和销售总额。每个类都应有合适的构造函数、设置方法和读取方法。编写一个程序,实例化这些类的对象,并输出与每个对象相关的信息(包括所继承的信息。)

 


3、 示例输出

 

4、 程序模板

// Lab 1: Employee.java

// Employee superclass.

 

public class Employee {

   /* Declare instance variables for the first name, 

      last name and social security number. */

 

   // constructor

   /* Declare a constructor with three parameters that are used to 

      initialize the first name, last name and social security number. */

 

   // set methods

   /* Create set methods for every instance variable. */

 

   // get methods

   /* Create get methods for every instance variable. */

 

   // return String representation of Employee object

   /* Create a toString method that returns a String containing the first name 

      and last name separated by a space. Then, append a newline and the social  security number. */

   

} // end class Employee

 

 

 

// Lab 1: SalariedEmployee.java

// SalariedEmployee class derived from Employee.

 

/* Write a class header in which class SalariedEmployee 

   inherits from class Employee */ 

 

   /* Declare an instance variable for the weekly salary. */

 

   // constructor

   /* Declare a constructor that initializes all the data for a SalariedEmployee,

      including the data originally defined in class Employee. */

 

   // set methods

   /* Create a set method for the instance variable. */

 

   // get methods

   /* Create a get method for the instance variable. */

 

   // return String representation of SalariedEmployee object

   /* Create a toString method that outputs the complete information 

      for a SalariedEmployeex. */

   

} // end class SalariedEmployee

 

 

// Lab 1: CommissionEmployee.java

// CommissionEmployee class derived from Employee.

 

/* Write a class header in which class CommissionEmployee 

   inherits from class Employee */ 

 

   /* Declare instance variables for gross sales and commission rate. */

 

   // constructor

   /* Declare a constructor that initializes all the data for a CommissionEmployee,

      including the data originally defined in class Employee. */

 

   // set methods

   /* Create set methods for every instance variable. */

 

   // get methods

   /* Create get methods for every instance variable. */

 

   // return String representation of CommissionEmployee object

   /* Create a toString method that outputs the complete information 

      for a CommissionEmployee. */

   

} // end class CommissionEmployee

 

 

// Lab 1: HourlyEmployee.java

// HourlyEmployee class derived from Employee.

 

/* Write a class header in which class HourlyEmployee 

   inherits from class Employee */ 

 

   /* Declare instance variables for wages and hours worked. */

 

   // constructor

   /* Declare a constructor that initializes all the data for a HourlyEmployee,

      including the data originally defined in class Employee. */

 

   // set methods

   /* Create set methods for every instance variable. */

 

   // get methods

   /* Create get methods for every instance variable. */

 

   // return String representation of HourlyEmployee object

   /* Create a toString method that outputs the complete information 

      for a HourlyEmployee. */

      

} // end class HourlyEmployee

 

 

// Lab 1: EmployeeTest.java

// Employee hierarchy test program.

import java.text.DecimalFormat;

import javax.swing.JOptionPane;

 

public class EmployeeTest {

 

   public static void main( String[] args ) 

   {

      DecimalFormat twoDigits = new DecimalFormat( "0.00" );

 

      // Create employees

      /* Create SalariedEmployee John Smith with social security number 

         111-11-1111 and weekly salary $800.00. */

      /* Create SalariedEmployee Sue Jones with social security number 

         222-22-2222 with gross sales of $10000 and a commission rate of .06. */

      /* Create SalariedEmployee Karen Price with social security number 

         444-44-4444 an hourly salary of $16.75 and 40 hours worked. */

 

      // output each employee

      /* Create a String called output and assign it the String representation 

         of the three employee objects separated by newlines. */

 

      JOptionPane.showMessageDialog( null, output );  // display output

      System.exit( 0 );

 

   } // end main

 

} // end class EmployeeTest

5、问题解答提示

(1) 每个扩展Employee的类的构造函数都应调用Employee构造函数,并将姓、名和社会保险号传递给它。

(2) 每个扩展Employee的类的toString方法都应调用Employee类的toString方法,以获取姓、名和社会保险号的String表示,然后在返回的String中添加额外的信息。

(3) 练习中有任何问题请及时询问老师。

6、强化练习

1)改进实验练习1的类层次结构,使其包含BasePlusCommissionEmployee类。BasePlusCommissionEmployee类与CommissionEmployee类具有相同的特征,但是还包括每星期的基本工资。该类的构造函数应调用CommissionEmployee类的构造函数;而该类的toString方法应调用CommissionEmployee类的toString方法。在创建BasePlusCommissionEmployee类后,改进实验1的程序,以实例化这个新类的一个对象,并输出该对象的信息(包括它所继承的信息)。输出结果应如下所示。

下面是我自己写的代码,基本能够实现实验一的内容, Employee1.0

Employee类是下面所有******Employee类的父类

package com.whj.exam;
public class Employee {
    private String thefirstname;
    private String thelastname;
    private String  socialnumber;
    public Employee(String thefirstname, String thelastname, String socialnumber) 
    {
    	//super();继承Employee的父类的构造方法,Employee的父类是object;
    	this.thefirstname = thefirstname;
    	this.thelastname = thelastname;
    	this.socialnumber = socialnumber;
    }
	public String getThefirstname() {
		return thefirstname;
	}
	public void setThefirstname(String thefirstname) {
		this.thefirstname = thefirstname;
	}
	public String getThelastname() {
		return thelastname;
	}
	public void setThelastname(String thelastname) {
		this.thelastname = thelastname;
	}
	
	public String getSocialnumber() {
		return socialnumber;
	}
	public void setSocialnumber(String socialnumber) {
		this.socialnumber = socialnumber;
	}
	@Override
	public String toString() {
		return getClass().getName()+":"+getThefirstname()+getThelastname()+"\n"+"social  security number:"+getSocialnumber();
	}
}

测试类,包含主方法

package com.whj.exam;
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
public class Test {
	public static void main(String[] args) {
		DecimalFormat twoDigits = new DecimalFormat( "0.00" );
	      Employee employee[]=new Employee[3];
	      employee[0]=new SalariedEmployee("John", "Smith", "111-11-1111","$800.00");
              employee[1]=new CommissionEmployee("Sue", "Jones", "222-22-2222","$10000",".06");
              employee[2]=new HourlyEmployee("Karen", "Price","444-44-4444","$16.75","40");
	      String output;
          output=employee[0].toString()+"\n"+employee[1].toString()+"\n"+employee[2].toString();
	      JOptionPane.showMessageDialog( null, output ); 
	      System.exit( 0 );
	}

}

SalariedEmployee类,是Employee的继承子类,其中多了每周的薪金。

package com.whj.exam;

public class SalariedEmployee extends Employee {
	private  String salary;
	public SalariedEmployee(String thefirstname, String thelastname,
			String socialnumber, String salary) {
		super(thefirstname, thelastname, socialnumber);
		this.salary = salary;
	}
public String getSalary() {
		return salary;
	}

public void setSalary(String salary) {
		this.salary = salary;
	}
@Override
public String toString()
{
return super.toString()+"\n"+"weekly salary:"+getSalary()+"\n";
}

}

CommissionEmployee类,是Employee的子类,其中多了佣金率和销售总额。

package com.whj.exam;

public class CommissionEmployee extends Employee{
     private String grosssales;
     private String commissionrate;
	public CommissionEmployee(String thefirstname, String thelastname,
			String socialnumber, String grosssales, String commissionrate) {
		super(thefirstname, thelastname, socialnumber);
		this.grosssales = grosssales;
		this.commissionrate = commissionrate;
	}
	public String getGrosssales() {
		return grosssales;
	}
	public void setGrosssales(String grosssales) {
		this.grosssales = grosssales;
	}
	public String getCommissionrate() {
		return commissionrate;
	}
	public void setCommissionrate(String commissionrate) {
		this.commissionrate = commissionrate;
	}
	@Override
	public String toString() {
		return super.toString()+"\n"+"gross sales:"+getGrosssales()+"\n"
				+"a commission rate:"+getCommissionrate()+"\n";
	}
	
	
}

HourlyEmployee类,是Employee的子类,其中添加了每小时工资和工作时数。

package com.whj.exam;

public class HourlyEmployee extends Employee{
   private String hourlysalary;
   private String workhour;
public String getHourlysalary() {
	return hourlysalary;
}
public void setHourlysalary(String hourlysalary) {
	this.hourlysalary = hourlysalary;
}
public String getWorkhour() {
	return workhour;
}
public void setWorkhour(String workhour) {
	this.workhour = workhour;
}
public HourlyEmployee(String thefirstname, String thelastname,
		String socialnumber, String hourlysalary, String workhour) {
	super(thefirstname, thelastname, socialnumber);
	this.hourlysalary = hourlysalary;
	this.workhour = workhour;
}
@Override
public String toString() {
	return  super.toString()+"\n"+"hourly salary:"+getHourlysalary()+"\n"
			+"hours worked:"+getWorkhour()+"\n";
}
   
}

通过老师给的实验对java中继承的初步运用,其中的一些细节还是有点把握不清,还有实验一扩展练习没做。(employee 2.0)
 




 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
组章节作业 1、将一个组中的元素倒排过来,不能新开一个组的临时存储空 间,只能在原组上改。 2、写一个用来模拟栈这种据结构,要求底层 使用组存储据, 并给出相的进栈和出栈的方法。MyStack int arr[]; int count;//栈中元素个 public MyStack(int n){ arr = new int[n]; } boolean push(int num){ if(count==arr.length){} arr[count++]=num; } int pop(){ if(count==0){ return -1;} return arr[--count]; } 3、实现在一个组指定位置添加元素和删除元素的功能。 1、组容量问题? 2、添加元素前后组中元素的变化 3、删除元素前后组中元素的变化 面向对象章节作业 1,写一个,名为Animal,该有两个私有属性, name(代表动物的名字),和legs(代表动物的腿的条 )。并提供个两构造方法,一个无参,默认给name 赋值为AAA,给legs赋值为4;另一个需要两个参, 分别用这两个参给私有属性赋值。该还有两个重载的move()方法,其中一个无参,在屏幕上输出一行文字: XXX Moving!!(XXX为该动物的名字);另一个需要一个int参n,在屏幕上输出n次 XXX Moving!! 2,写一个Person,包含以下属性:String name; int age; boolean gender; Person partner。 为Person写一个marry(Person p)方法,代表当前 对象和p结婚,如若可以结婚,则输出恭贺信息, 否则输出不能结婚原因。要求在另外一个中写一 个主方法,来测试以上程序。(下列情况不能结婚: 结婚年龄,男<24,女<22;3,某一方已婚) 3,写一个,名为Animal,该有两个私有属性,name(代表动物的名字),和legs(代表动物的腿的条);要求为两个私有属性提供public 的访问方法。并提个两构造方法,一个无参,默认给name赋值为AAA,给legs赋值为4;另一个需要两个参,分别用这两个参给私有属性赋值。要求在第一个构造方法中调用第二个构造方法。该还有两个重载的move()方法,其中一个无参,在屏幕上输出一行文字: XXX Moving!!(XXX为该动物的名字);另一个需要一个int参n,在屏幕上输出n次 XXX Moving!! 4,写一个Fish,继承自Animal,并提供一个构造方法,该构造方法需要一个参name,并给legs赋默认值0;该还要求覆盖Animal中的无参move()方法,要求输出:XXX Swimming!! 5,写一个Bird,继承自Animal,并提供一个构造方法,该构造方法需要一个参name,并给legs赋默认值2;该还要求覆盖Animal中的无参move()方法,要求输出:XXX Flying!! 6,写一个Zoo,要求分别生成若干个Animal,Fish和Bird。并调用他们的属性和方法。 7,写Shape,要求如下: 1.int型属性x和y,分别表示图形的中心点坐标 2.无参构造器 3.构造器,对x和y进行初始化 4.draw()方法,输出"Shape draw" 写Circle继承Shape,要求如下 1.double型属性r,表示圆的半径 2.无参构造器,将r初始化为1.0 3.构造器,对r进行初始化 4.构造器,对x、y、r进行初始化 5.draw()方法,输出"draw in circle"和x,y,r的值 写Rectangle继承Shape,要求如下 1.double型属性height和width,表示矩形的高和宽 2.无参构造器,将height和width都初始化为1.0 3.构造器,对height和width进行初始化 4.构造器,对x、y、height、width进行初始化 5.draw()方法,输出"draw in rectangle"和x,y,height,width的值 使用ShapeTest测试以上代码。 8,某公司的雇员分为以下若干Employee:这是所有员工总的父,属性:员工的姓名,员工的生日月份。方法:getSalary(int month) 根据参月份来确定工资,如果该月员工过生日,则公司会额外奖励100元。 SalariedEmployeeEmployee的子,拿固定工资的员工。属性:月薪 HourlyEmployeeEmployee的子,按小时工资的员工,每月工作超出160小时的部分按照1.5倍工资发放。属性:每小时工资、每月工作小时 SalesEmployeeEmployee的子,销售人员,工资由月销售额和提成率决定。属性:月销售额、提成率 BasedPlusSalesEmployee:SalesEmployee的子,有固定底薪的销售人员,工资由底薪加上销售提成部分。属性:底薪。 写一个程序,把若干各种型的员工放在一个Employee组里,写一个函,打印出某月每个员工的工资额。注意:要求把每个都做成完全封装,不允许非私有化属性。 容器章节作业 1、写MyStack,实现栈功能。在中使用ArrayList保存据。 2、使用TreeSet和Comparator,写TreeSetTest1 要求:对TreeSet中的元素"HashSet"、"ArrayList"、"TreeMap"、"HashMap"、"TreeSet"、"LinkedList"进行升序和倒序排列 3、使用TreeSet和Comparator,写TreeSetTest2 要求:对TreeSet中的元素1,2,3,4,5,6,7,8,9,10进行排列,排序逻辑为奇在前偶在后,奇按照升序排列,偶按照降序排列 4、使用TreeSet和Comparator,写TreeSetTestInner 要求: 对TreeSet中的元素"HashSet"、"ArrayList"、"TreeMap"、"HashMap"、"TreeSet"、"LinkedList"进行升序和倒序排列 1. 使用匿名内部实现 2. 使用静态内部实现
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值