class father 

int a=1,6=2; 
void show() 

System.out.println(a+" "+b); 


class som extends father 

int b=3,c=4; 
void show() 

system.out.println(a+" "+b+" "+c); } 

class jex6_24 

public static void main(string[] args) 

son S =new son(); 
S.show(); 
farther Fb=S; 
Fb.show(); 
System.out.println(Fb.a+""+Fb.b); 
if(Fb instanceof son) 
S=(son)Fb; 
System.out.println(S.a+""+S.b+""+S.c); 


解决方案 »

  1.   

    class som extends father 
    son S =new son(); //代码错误
    int a=1,6=2; 
    子类对象引用问题 
    这个怎么了??
      

  2.   

    你的程序错误如下:
    1. 在class father 中6=2,这里可能是b=2;
    2. class som extends father 这里类的名字应该是son,你把n写成m。
    3. 还是在son类中,system.out.println(a+" "+b+" "+c); //这里的system的S应该是大写的--〉System
    4.jex6_24中public static void main(string[] args) {//这里的string[]中的S应该是大写的--〉String
    5.jex6_24中farther Fb = S;//这里应该是把farther--〉father .多了一个r。
      

  3.   

    楼主的错别字还真多啊!继承来说,只有方法是重写的,而属性既farther中a,b的值是一直不变的.
      

  4.   

    正确的代码:class farther 

    int a=1,b=2; 
    void show() 

    System.out.println(a+" "+b); 


    class son extends farther 

    int b=3,c=4; 
    void show() 

    System.out.println(a+" "+b+" "+c); } 

      public class jex6_24 
      { 
           public static void main(String[] args) 
           { 
    son S =new son(); 
    S.show(); 
    farther Fb=S; 
    Fb.show(); 
    System.out.println(Fb.a+" "+Fb.b); 
    //if(Fb instanceof son) 
    S=(son)Fb; 
    System.out.println(S.a+" "+S.b+" "+S.c); 
             } 
       } 
    输出:
    1 3 4
    1 3 4
    1 2
    1 3 4farther Fb=S; 形成多态,show方法只调用子类重写的方法。但是属性不重写。
      

  5.   

    class Father 

    int a=1,b=2; 
    void show() 

    System.out.println(a+" "+b); 


    class Son extends Father 

    int b=3,c=4; 
    void show() 

    System.out.println(a+" "+b+" "+c); } 

    class Test1

    public static void main(String[] args) 

    Son S =new Son(); 
    S.show(); 
    Father Fb=S; 
    Fb.show(); 
    System.out.println(Fb.a+""+Fb.b); 
    if(Fb instanceof Father) 
    S=(Son)Fb; 
    System.out.println(S.a+""+S.b+""+S.c); 


    楼上说的很对,属性是不会被重写的,子类会把父类中的方法覆盖住
      

  6.   


    我汗~~~估计楼主是想当高手,用记事本写代码~~~~找个IDE用用吧~~~
      

  7.   


    class Father {
    int a = 1;
    int b = 2; void show() {
    System.out.println(a + " " + b);
    }
    }class Son extends Father {
    int b = 3, c = 4; void show() {
    System.out.println(a + " " + b + " " + c); }
    }public class jex6_24 {
    public static void main(String[] args) {
    Son S = new Son();
    S.show();
    Father Fb = S;
    Fb.show();
    System.out.println(Fb.a + "" + Fb.b);
    if (Fb instanceof Son)
    S = (Son) Fb;
    System.out.println(S.a + "" + S.b + "" + S.c);
    }
    }