【例4-1】属性的继承与隐藏的实例
/****************************test_hideAttributes.java ************************************/
public class test_hideAttributes{ //主程序
public static void main(String args[]) {
Employee employee = new Employee(); //创建员工的一个对象
System.out.println("编号为" + employee.employeeNo + "员工的最低工资为: " + employee.mini_salary);
//调用父类的方法输出员工的最低工资
System.out.println("编号为" + employee.employeeNo +"员工的最低工资为: " + employee.getMini_salary());
TimeWorker timeWorker = new TimeWorker(); //创建计时工的一个对象
System.out.println("编号为" + timeWorker.employeeNextNo + "钟点工的最低工资为: " + timeWorker.mini_salary);
//调用子类的方法输出计时工的最低工资
System.out.println("编号为" + timeWorker.employeeNextNo + "钟点工的最低工资为: "+timeWorker.getTimeSalary());
}
} // test_hideAttributes2
class Employee { //定义父类: 员工类
String employeeName; //姓名
int employeeNo; //个人编号
String employeeSex; //性别
double employeeSalary; //工资总额
static int employeeNextNo = 10001; // 存放员工编号
static double mini_salary = 600; //员工的最低 工资
Employee(){ //构造函数
employeeNo = employeeNextNo++;
}
public double getMini_salary(){ //获取最低工资
return mini_salary;
}
public
//其他的方法暂时忽略
}
}// Employee
class TimeWorker extends Employee {//定义子类:钟点工类
double ratePerHour; //每小时酬金,新的数据成员
double workHours; //当月工作小时数,新的数据成员
static double mini_salary = 300; //员工的最低 工资
public TimeWorker(){
}
public double getTimeSalary(){// 返回最低工资
return mini_salary;
}
//其他的方法暂时忽略
} // TimeWorker
/*---------------------------------------------------------------------------------------------------------*/
【例4-2】子类中使用构造函数的实例。
/********************************test_constructor.java***********************************/
public class test_constructor{ //主控程序
public static void main(String args[]){
Employee employee = new Employee("李 平"); //创建员工的一个对象
employee.employeeSex = "男";
employee.setEmployeeSalary(1200);
//输出员工的基本信息
System.out.println("员工的基本信息为 : " + employee.toString()+employee.getEmployeeSalary());
//创建子类一般员工的一个对象
CommonEmployee commonEmployee = new CommonEmployee("李晓云",400);
//子类继承父类的数据成员employeeSex
commonEmployee.employeeSex = "女";
//输出子类一般员工的基本信息
System.out.println("员工的基本信息为 : " + commonEmployee.toString()) ;
}
}
class Employee { //定义父类: 雇员类
private String employeeName; //姓名
int employeeNo; //个人编号
String employeeSex; //性别
private double employeeSalary; // 工资总额
static int employeeNextNo = 10000; // 存放员工编号
static double mini_salary = 600; //员工的最低 工资
public Employee(){ //无参构造函数
employeeNo = employeeNextNo++;
System.out.println("调用父类Employee的构造函数。");
}
public Employee(String name) { //有参构造函数
System.out.println("调用父类Employee的构造函数。");
employeeNo = employeeNextNo++;
employeeName = name;
}
public double getMini_salary(){ //获取最低工资
return mini_salary;
}
public int getEmployeeNo() { //获取雇员编号
return employeeNo;
}
public double getEmployeeSalary(){ //获取雇员工资
return employeeSalary;
}
public void setEmployeeSalary(double salary) { //计算员工的薪水
employeeSalary = salary + mini_salary ;
}
public String toString() { //输出员工的基本信息
String s;
s = "编号: " + employeeNo + "姓名: " + employeeName ;
if(employeeSex.compareTo("男")==0)
s = s+": 性别 : 男 ";
else
s = s+": 性别: 女";
return ( s+" : 工资: " );
}
}
class CommonEmployee extends Employee {//定义子类:一般员工类
private double bonus; //奖金, 新的数据成员
public CommonEmployee(){
System.out.println("调用子类CommonEmployee的构造函数。");
}
public CommonEmployee(String name,double bonus ){
super(name); //通过super()的调用,给父类的数据成员赋初值
this.bonus = bonus; //this指当前对象
System.out.println("调用子类CommonEmployee的构造函数。");
}
public double getBonus(){ //新增方法,获取员工奖金
return bonus;
}
public void setBonus(double newBonus) { //新增的方法,设置一般员工的薪水
bonus = newBonus;
}
//来自父类的继承,但在子类中重新覆盖父类方法,用于修改一般员工的薪水
public double getEmployeeSalary(){
double salary = getMini_salary();
return bonus + salary;
}
public String toString() {
String s;
s = super.toString();
//调用自身对象的方法getEmployeeSalary(),覆盖父类同名的该方法
return ( s+" : 工资: " + getEmployeeSalary() +" ");
}
}
/*---------------------------------------------------------------------------------------------------------*/
【例4-3】抽象类的使用实例。
/******************************* test_abstract .java***********************************/
/*程序功能介绍:多态性实现的工资系统中的一部分程序。Employee类是抽象的员工父类,Employee类的子类有经理Boss,每星期获取固定工资,而不计工作时间;子类普通雇员CommissionWorker类,除基本工资外还根据每周的销售额发放浮动工资等。子类Boss和CommissionWorker声明位final,表明它们不再派生新的子类。*/
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
public class test_abstract{
public static void main( String args[] ){
Employee employeeRef; // ref为Employee引用
String output = "";
Boss boss = new Boss( "李晓华", 800.00 );
CommissionWorker commission = new CommissionWorker( "张 雪",400.0, 3.0, 150);
//创建一个输出数据的格式化描述对象
DecimalFormat precision = new DecimalFormat( "0.00" );
// 把父类的引用employeeRef赋值为子类Boss对象boss的引用
employeeRef = boss;
output += employeeRef.toString() + " 工资 ¥" +
precision.format( employeeRef.getSalary() ) + "\n" +
boss.toString() + " 工资 ¥" +
precision.format( boss.getSalary() ) + "\n";
// 把父类的引用employeeRef赋值为子类普通员工对象commission的引用
employeeRef = commission;
output += employeeRef.toString() + " 工资 ¥" +
precision.format( employeeRef.getSalary() ) + "\n" +
commission.toString() + " 工资 ¥" +
precision.format( commission.getSalary() ) + "\n";
JOptionPane.showMessageDialog( null, output,"Demonstrating Polymorphism",JOptionPane.INFORMATION_MESSAGE );
System.exit( 0 );
}
}
abstract class Employee{//抽象的父类Employee
private String name;
private double mini_salary = 600;
public Employee( String name ) {// 构造函数
this.name = name;
}
public String getEmployeeName(){
return name;
}
public String toString(){ //输出员工信息
return name;
}
// Employee抽象方法getSalary(),将被他的每个子类具体实现
public abstract double getSalary();
}
final class Boss extends Employee
{
private double weeklySalary; //Boss新添成员,周薪
public Boss( String name, double salary) { // 经理Boss类的构造函数
super( name); // 调用父类的构造函数为父类员工赋初值
setWeeklySalary( salary ); //设置Boss的周薪
}
public void setWeeklySalary( double s ) {// 经理Boss类的工资
weeklySalary = ( s > 0 ? s : 0 );
}
public double getSalary(){//重写父类的getSalary()方法,确定Boss的薪水
return weeklySalary ;
}
public String toString() {//重写父类同名的方法toString(),输出Boss的基本信息
return "经理: " + super.toString(); //调用父类的同名方法
}
}
final class CommissionWorker extends Employee
{
private double salary; // 每周的底薪
private double commission; // 每周奖金系数
private int quantity; // 销售额
// 普通员工类的构造函数
CommissionWorker( String name,double salary, double commission, int quantity) {
super( name ); // 调用父类的构造函数
setSalary( salary );
setCommission( commission );
setQuantity( quantity );
}
public void setSalary( double s ) { // 确定普通员工的每周底薪
salary = ( s > 0 ? s : 0 );
}
public void setCommission( double c ) { // 确定普通员工的每周奖金
commission = ( c > 0 ? c : 0 );
}
public void setQuantity( int q ) { // 确定普通员工销售额
quantity = ( q > 0 ? q : 0 );
}
// 重写父类的getSalary()方法,确定CommissionWorker的薪水
public double getSalary() {
return salary + commission * quantity;
}
//重写父类同名的方法toString(),输出CommissionWorker的基本信息
public String toString(){
return "普通员工: " + super.toString(); //调用父类的同名方法
}
}
/*------------------------------------------------------------------------------------------------------------*/
【例4-4】家用电器遥控系统的实现。
/********************************test_interface.java***************************************/
import java.io.*;
class Light { //电灯类
public void turnLight(int degree) { //调整灯光亮度,0表示关灯,100表示亮度最大
System.out.println("电灯的亮度为: " + degree);
if(degree == 0)
System.out.println("关闭电灯。");
else
System.out.println("将电灯拧到最大亮度。");
}
}
class TV{ //电视机类
public void setChannel(int channel) {// 0表示关机,1表示开机并切换到1频道
System.out.println("电视选择的频道为: " + channel);
if(channel == 0)
System.out.println("关闭电视。");
else
System.out.println("将电视调到第 " + channel +" 频道。");
}
}
interface Command{ //接口类Command,为电灯和电视提供开、关的命令集
void on(); //电器打开
void off(); //电器关闭
}
class RemoteController { //遥控器类 ,选择使用的电器以及实现电器的开关选择
//遥控器有4个按钮,按照编号分别对应4个Command对象
protected Command []commands = new Command[4];
public void onPressButton(int button){
//按钮被按下时执行命令对象中的命令
if(button % 2 == 0)commands[button].on();
else commands[button].off();
}
public void setCommand(int button, Command command){
commands[button]= command; //设置每个按钮对应的命令对象
}
}
class LightCommand implements Command { //电灯命令类 ,实现接口Command
protected Light light; //指向要控制的电灯对象
public void on(){ //打开电灯,设置电灯的最亮值
light.turnLight(100);
}
public void off(){
light.turnLight(0);
}
public LightCommand(Light light) { //电灯命令器的构造函数,接受电灯的对象
this.light = light;
}
}
class TVCommand implements Command { //电视机命令类,实现接口Command
protected TV tv; //指向要控制的电视机对象
protected int channel;
public void on(){
tv.setChannel(channel); //设置电视的频道,假设为1频道
}
public void off(){ //关闭电视,频道数为0
tv.setChannel(0);
}
public TVCommand (TV tv ,int channel) { //构造函数,接受电视对象
this.tv = tv;
this.channel = channel;
}
}
public class test_interface{
public static void main(String []args){
Light light = new Light(); //创建电灯对象
TV tv = new TV(); //创建电视对象
int channel = 1;
//设置按钮和命令对象
LightCommand lightCommand = new LightCommand(light);
//设置遥控对象为电灯
RemoteController remoteController = new RemoteController();
remoteController.setCommand(0, lightCommand);
remoteController.setCommand(0,lightCommand); //开启电灯
remoteController.onPressButton(0);
System.out.println("请输入你要选择的电视频道:");
try {//从键盘读入一个整形数据,表示电视机选择的频道
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String string = new String();
string = br.readLine();
try{
channel = Integer.parseInt(string);
}
catch(NumberFormatException e){
System.out.println("输入错误,请重新输入数据。");
}
}
catch(IOException e){
System.out.println(e);
}
//创建电视对象,并选择所需的频道
TVCommand tvCommand = new TVCommand(tv,channel);
remoteController.setCommand(2,tvCommand); //开启电视
remoteController.onPressButton(2);
remoteController.setCommand(3,tvCommand); //关闭电视
remoteController.onPressButton(3);
remoteController.setCommand(1,lightCommand); //关闭电灯
remoteController.onPressButton(1);
}
}