本帖最后由 liqi85 于 2010-05-17 16:56:46 编辑

解决方案 »

  1.   

    多态简单来说就是“方法的重用”。我这里貌似解释不清楚,你就多找几个例子敲一下,然后思考一下,然后你就有点通了。要是死扣概念的话,估计你永远都理解不了
    把你的Person 类前面的public去掉,然后就能够编译成功啦~~~~
    看看是不是你要的结果 ++++++++++分哟                       HOME_ning(玩物丧志)
      

  2.   

    我运行下面代码没报错啊class Person {
    protected int age;
    protected String name;
    protected char sex;Person(int age,String name,char sex){
    this.age= age;
    this.name = name;
    this.sex = sex;
    }
    void show(){
    System.out.println(age+name+sex);
    }
    }class Student5 extends Person{
    int id;
    Student5(int age, String name, char sex,int id) {
    super(age, name, sex);
    this.id=id;
    }
    void show(){
    System.out.println(age+name+sex+id);
    }
    }class Teacher extends Person{
    int workId;
    Teacher(int age, String name, char sex,int workId) {
    super(age, name, sex);
    this.workId=workId;
    }
    void show(){
    System.out.println(age+name+sex+workId);
    }
    }class Test{
      public static void main(String[] args){
      Person p=new Student5(18,"lucy",'女',1001);
      //p.show();
      ((Student5)p).show();
      Person t=new Teacher(28,"jack",'男',10034);
      ((Teacher)t).show();
      }
    }
      

  3.   

    protected 你可以不用加这个的 系统默认就是这个 protected int age;和 int age ;效果是一样的
      

  4.   


    我运行的也没错,楼主是不是把((Student5)p).show()写成((Student)p).show()了
      

  5.   

    语法上,是没有问题的。他们之间有继承关系,可以这样强制转换的。
    但转换的成败与否,取决于p在内存中实际的类型。
    如果
    p instanceof Student
    p extends Student
    p implements Student
    三者之一成立,则转换成功。