Posted on 2007-08-25 22:13
Java蜘蛛人 --郑成桥 阅读(153)
评论(0) 编辑 收藏
1
2
import java.util.*;
3
public class ManagerTest
4
{
5
public static void main (String[] args)
6
{
7
Manager boss=new Manager("xiaoqiao",8000,1989,04,11);
8
boss.SetBonus(5000);
9
Employee [] staff=new Employee[3];
10
staff[0]=boss; //父类 可以被子类转换 .... 但是子类 不能被父类转换.
11
staff[1]=new Employee("dddd",5000,1989,10,12);
12
staff[2]=new Employee("aaaaa",4000,1988,11,12);
if(staff[1] instanceof Manager) //staff[1] 能不能 (instanceof)转换为: Manager
/* 我试图把staff[1]转换为Manager 但是失败了 */
{
Manager aa=(Manager)staff[1];
}
else
{
System.out.println("不能装换");
}
13
14
for(Employee e:staff)
15
System.out.println("mingzi \t"+e.getName()+"\t gongzi \t"+e.getSalary()+"\t shengri \t"+e.getHrireDay());
16
}
17
18
}
19
class Employee
20
{
21
public Employee(String n,double s,int year,int month,int day) //首先我们传5个参数
22
{
23
name=n;
24
salary=s;
25
GregorianCalendar calendar =new GregorianCalendar(year,month -1,day);
26
hireDay=calendar.getTime();
27
}
28
public String getName() //得到名字
29
{
30
return name;
31
}
32
public double getSalary() //得到工资
33
{
34
return salary;
35
}
36
public Date getHrireDay() //得到年龄
37
{
38
return hireDay;
39
}
40
public void raiseSalary(double byPercent)
41
{
42
double raise=salary*byPercent/100;
43
salary+=raise;
44
}
45
private String name;
46
private double salary;
47
private Date hireDay;
48
}
49
class Manager extends Employee //定义一个 Manager 类继承 Employee
50
{
51
/**//**
52
@parm n the employee's name
53
@parm s the salary
54
@parm year the hire year
55
@parm month the hire month
56
@parm day the hire day
57
*/
58
public Manager(String n,double s,int year,int month,int day)
59
{
60
super(n,s,year,month,day); //关键字 继承Employee 里的方法
61
bonus=0;
62
}
63
public double getSalary() //定义一个方法重载..
64
{
65
double baseSalary=super.getSalary();
66
return baseSalary+bonus; //就是在工资的基本上加上奖金
67
}
68
public void SetBonus(double b) //定义一个方法 存储奖金
69
{
70
bonus=b;
71
}
72
private double bonus;
73
}
动态的绑定的意思 . 假如 调用 x.f("Hello") 编译器会挑 x.f(String) 不会挑x.f(int)
假如你子类想覆盖 父类的方法 . 那么方法返回的类型必须是一样的
比如:
Employee 类中有
public Employee gethaha()
{
...........
}
那么在后面的 Manager中可以 按照一样的方法
public Manager gethaha()
{
...........
}
如果你想要覆盖一个父类的方法的时候. 你的子类的方法不能低于父类的可见性
比如.父类是 public . 那么你子类覆盖他 也必须是 public 否则会出现错误的...
今天继承就写这么多.
如果你想阻止继承的话. 就用 final 修饰符.. 可以看看下面的代码就会懂了
public class Test //假如在这加一个final就会报错. 因为一个被final修饰的类 是不能继承的.
{
private String name;
private double salary;
Test(String name,double salary)
{
this.name=name;
this.salary=salary;
}
public final String getName() //假如在这加一个 final 编译不会影响什么. 因为子类没有覆盖这个方法
{
return name;
}
public double getsalary() //然而我们在这加一个 final 编译就会报错了.因为子类覆盖了这个类.
{
return salary;
}
public static void main (String[] args)
{
Test1 t1=new Test1("xiaoqiao",80000);
t1.aaa(600);
System.out.println("小桥="+t1.getName()+"\t"+t1.getsalary());
}
}
class Test1 extends Test
{
private double bb;
public Test1(String name,double salary)
{
super(name,salary);
}
public double getsalary()
{
double aa=super.getsalary();
return aa+bb;
}
public void aaa(double bb)
{
this.bb=bb;
}
}
___作者: 郑成桥