小弟刚学java,看了书上的这段代码有点不懂
import java.util.*;public class ManagerTest
{
public static void main(String[] args)
{
// construct a Manager object
Manager boss = new Manager("Carl Cracker", 80000, 1987, 12, 15);
boss.setBonus(5000); Employee[] staff = new Employee[3]; // fill the staff array with Manager and Employee objects staff[0] = boss;
staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
staff[2] = new Employee("Tommy Tester", 40000, 1990, 3, 15); // print out information about all Employee objects
for (Employee e : staff)
System.out.println("name=" + e.getName()
+ ",salary=" + e.getSalary());
}
}class Employee
{
public Employee(String n, double s, int year, int month, int day)
{
name = n;
salary = s;
GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
hireDay = calendar.getTime();
} public String getName()
{
return name;
} public double getSalary()
{
return salary;
} public Date getHireDay()
{
return hireDay;
} public void raiseSalary(double byPercent)
{
double raise = salary * byPercent / 100;
salary += raise;
} private String name;
private double salary;
private Date hireDay;
}class Manager extends Employee
{
/**
@param n the employee's name
@param s the salary
@param year the hire year
@param month the hire month
@param day the hire day
*/
public Manager(String n, double s, int year, int month, int day)
{
super( n, s, year, month, day);
bonus = 0;
} public double getSalary()
{
double baseSalary = super.getSalary();
return baseSalary + bonus;
} public void setBonus(double b)
{
bonus = b;
} private double bonus;
}在这段程序中,是不是子类Manager不能直接使用超类Employee的构造方法呢?我把Employee中的private double salary;改成public double salary;
把子类Manager中
public double getSalary()
{
double baseSalary = super.getSalary();
return baseSalary + bonus;
}
改成
public double getSalary()
{
double baseSalary = salary;
return baseSalary + bonus;
}
不行呢?不是public 定义的在所有类中都可以使用吗?
解决方案 »
- 远程访问mysql数据库问题请教
- 关于java垃圾回收器 变量回收的问题
- 重发:getColumnName()怎么和getColumnLabel()一样?怎么才能得到字段名? java ResultSetMetaData
- JAVA问题,如何引用一个函数的数组返回值(急!在线等,有分送)
- 求解一个ip地址的问题....
- 该看啥?
- list.toArray()之后为什么第一个为null
- 如何方便地进行61.1.1.1到061.001.001.001这样的格式转换?
- 我明明得分了,但为什么在"我的专家分"里都是0??
- 怎样使html中的flash的大小跟screenWidth 和 screenHeight一样?
- 关于重载时的可变参数列表问题
- 关于serialPort监听对象,替换监听对象的问题,小弟这里急!
你说的:我把Employee中的private double salary;改成public double salary; 这个不管是私有的还是公有的在本类中你都可以用,出了这个类你就不能直接的拿来用。
但将子类中的super.getSalay()改成salay是不对的。salary是Employee类的私有变量,你只能通过继承超类的public double getSalary() 方法才能调用salary的值。
为什么我把Employee中的private double salary;改成public double salary;
仍然只能在Employee中使用,而在Manager这个类中不能使用salary呢?