class B {
int b1 = 1;
public int b2 = 2;
protected int b3 = 3;
private int b4 = 4;
int getb4() {return b4;}
}
class A extends B {
public static void main(String args[]){
int q=5;
int Sum(int t){return b1+b2+b3+getb4()+t;}//提示Sun()有错,望解决,谢谢
System.out.println(Sum(q));
}
}

解决方案 »

  1.   

    class B {
    int b1 = 1;
    public int b2 = 2;
    protected int b3 = 3;
    private int b4 = 4;
    protect int getb4() {return b4;}
    }
    class A extends B {
    private int Sum(int t)
             {return b1+b2+b3+getb4()+t;}
    System.out.println(Sum(5));
    }public static void main(String args[]){
    new B().Sum(5);
    }
    }
      

  2.   

    class B{
      int b1 = 1;
      public int b2 = 2;
      protected int b3 = 3;
      private int b4 = 4;
      protected int getb4(){
        return b4;
      }
    }public class A extends B{
      public int Sum(int t){
        return b1+b2+b3+getb4()+t;
      }  public static void main(String args[]){
        System.out.println(new A().Sum(5));
      }
    }
      

  3.   

    sum是一个函数
    而main也是一个函数
    不能函数嵌套的!
      

  4.   

    private int b4 = 4;//为B的私有属性,A不能继承此属性,当然出错了,呵呵!
    int getb4() {return b4;}
      

  5.   

    b1缺省修饰符,不能被子类访问
    加上public应该可以了
    b4虽然为私有可可以通过getb4()获得
      

  6.   

    (1)不应该函数嵌套,编译的时候也有提示---";"expected;
    (2)b4虽为private,但可以通过访问器方法获得的.
    (3)b1为缺省修饰符,是可以被同一个包内的类访问,不可以被其他包的类访问的.
       所以A可以访问到b1.修改如下:(已测试运行通过)class B 
    {
    int b1 = 1;
    public int b2 = 2;
    protected int b3 = 3;
    private int b4 = 4;
    int getb4() 
    {return b4;}
    }class A extends B 
    {
      public static void main(String args[])
      {
       int q=5;
       System.out.println(new A().Sum(q));
      }
      public int Sum(int t)
      {return b1+b2+b3+getb4()+t;}
    }