代码如下:
   /**
    * 计算经理薪资总额
    */
   void salSum() {
     houseRentAllowance = basic * 0.08;
     dearnessAllowance = basic * 0.3;
     medicalAllowance = 1500;
     leave = 3;     lessPay = (leave <= 5) ? (0.25 * basic) : (0.5 * basic);
     totalAmount = basic + houseRentAllowance + dearnessAllowance + medicalAllowance;
     NetPay = totalAmount - lessPay;
   }   /**
    * 方法调用父类方法
    * 显示详细信息
    */
   void show() {
     super.show();
     System.out.println("部门:\t\t\t" + department);     System.out.println("工资总额:\t\t" + totalAmount);
     System.out.println("净工资:\t\t" + NetPay);
   }

解决方案 »

  1.   

    完全不明白你这个代码的意思...如果你碰到的是参数传递问题的话,那Java就只有值传递。如果你传递的是基本数据类型的话,那Java就是拷贝该值并传递给相应的函数,之后他们俩就没有关系了。如果你传递的是一个对象的话,那也是传递的对象的引用值。这时候可能你改变对象的属性,在不同函数将也将看得到效果。
      

  2.   

    我想在 show()方法里,访问 totalAmount 和 NetPay,可输出的结果都是 0.0,
    请大家指教了,谢谢。
      

  3.   

    再发次代码: /**
      * 北大青鸟
      * 版权所有
      */
     /**
      * 这个程序演示继承
      * @version 1.0 2005 年 5 月 20 日
      * @author Michael
      */ abstract class Employee {
       /** 存储姓名 */
       String name;
       /** 存储薪资 */
       double basic;
       /** 存储地址 */
       String address;   /**
        * 构造方法
        */
       Employee() {
       }   /**
        * 构造方法
        * @param str 传递至构造方法的参数
        * @param sal 传递至构造方法的参数
        * @param addr 传递至构造方法的参数
        */
       Employee (String str, double sal, String addr) {
         name = str;
         basic = sal;
         address = addr;
       }   /**
        * 显示详细信息
        */
       void show() {
         System.out.println("姓名:\t\t\t" + name);
         System.out.println("地址:\t\t\t" + address);
         System.out.println("薪资:\t\t\t" + basic);
       }   /**
        * 计算薪资总额
        */
       abstract void salSum();
     } /**
      * 这个类是计算经理薪资的类,并且是 Employee 的子类。
      * @version 1.0 2005 年 5 月 20 日
      * @author Michael
      */
     class Manager extends Employee {
       /** 存储部门 */
       String department = "";
       double lessPay = 0; // 薪资扣除部分的金额数
       double leave = 0; // 休假天数
       double houseRentAllowance = 0; // 住房租金津贴
       double dearnessAllowance = 0; // 物价上调津贴
       double medicalAllowance = 0; // 医疗津贴
       double totalAmount = 0; // 薪资总额
       double NetPay = 0; // 净工资   /**
        * 构造方法
        */
       Manager () {
       }   /**
        * 构造方法使用 super 关键字
        * @param str 传递至构造方法的参数
        * @param sal 传递至构造方法的参数
        * @param addr 传递至构造方法的参数
        * @param dept 传递至构造方法的参数
        */
       Manager (String str, double sal, String addr, String dept) {
         super (str, sal, addr);
         department = dept;
       }   /**
        * 计算经理薪资总额
        */
       void salSum() {
         houseRentAllowance = basic * 0.08;
         dearnessAllowance = basic * 0.3;
         medicalAllowance = 1500;
         leave = 3;     lessPay = (leave <= 5) ? (0.25 * basic) : (0.5 * basic);
         totalAmount = basic + houseRentAllowance + dearnessAllowance + medicalAllowance;
         NetPay = totalAmount - lessPay;
       }   /**
        * 方法调用父类方法
        * 显示详细信息
        */
       void show() {
         super.show();
         System.out.println("部门:\t\t\t" + department);     System.out.println("工资总额:\t\t" + totalAmount);
         System.out.println("净工资:\t\t" + NetPay);
       }
     } /**
      * 这个类是计算董事薪资的类,并且是 Employee 的子类。
      * @version 1.0 2005 年 5 月 20 日
      * @author Michael
      */
     class Director extends Employee {
       /** 存储交通津贴 */
       double transportAllowance = 0;
       double lessPay = 0; // 薪资扣除部分的金额数
       double leave = 0; // 休假天数
       double houseRentAllowance = 0; // 住房租金津贴
       double dearnessAllowance = 0; // 物价上调津贴
       double medicalAllowance = 0; // 医疗津贴
       double entertainmentAllowance = 0; // 交际费津贴
       double totalAmount = 0; // 薪资总额
       double NetPay = 0; // 净工资
       
       /**
        * 构造方法
        */
       Director () {
       }   /**
        * 构造方法使用 super 关键字
        * @param str 传递至构造方法的参数
        * @param sal 传递至构造方法的参数
        * @param addr 传递至构造方法的参数
        * @param allowance 传递至构造方法的参数
        */
       Director (String str, double sal, String addr, double allowance) {
         super (str, sal, addr);
         transportAllowance = allowance;
       }   /**
        * 计算董事薪资总额
        */
       void salSum() {
         houseRentAllowance = basic * 0.2;
         dearnessAllowance = basic * 0.5;
         medicalAllowance = 4500;
         entertainmentAllowance = 5000;
         leave = 2;     lessPay = (leave <= 5) ? (0.25 * basic) : (0.5 * basic);
         totalAmount = basic + houseRentAllowance + dearnessAllowance + medicalAllowance + entertainmentAllowance + transportAllowance;
         NetPay = totalAmount - lessPay;
       }   /**
        * 方法使用 super 关键字
        * 显示详细信息
        */
       void show() {
         super.show();
         System.out.println("交通津贴:\t\t" + transportAllowance);     System.out.println("工资总额:\t\t" + totalAmount);   
         System.out.println("净工资:\t\t" + NetPay);
       }
     } /**
      * 这个类测试对象引用
      * @version 1.0 2005 年 5 月 20 日
      * @author Michael
      */
     public class EmployeeTest {
       /**
        * 构造方法
        */
       EmployeeTest() {
       }   /**
        * 这是一个 main 方法
        * @param args 传递至 main 方法
        */
       public static void main (String[] args) {
         Employee mgrObj = new Manager ("Henry", 5500.65, "Sydeny", "会计部");
         System.out.println("\n 经理详细信息");
         System.out.println("==================================");
         mgrObj.show();
         mgrObj.salSum();     Employee dirObj = new Director ("Stephen", 32400.00, "New York", 8000);
         System.out.println("\n\n 董事详细信息");
         System.out.println("=============================");
         dirObj.show();
         dirObj.salSum();
       }
     }红字,为访问不到的属性值。
    书上说,要编写一个方法,显示属性中的存储的值,可为什么存储不了值,我狠是困惑啊。。
      

  4.   

    mgrObj.show();
    mgrObj.salSum();哥们,你先调用显示,再计算赋值,当然是这个结果咯!
      

  5.   

    换个顺序,先计算再显示就行了!mgrObj.salSum();
    mgrObj.show();