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));
}
}
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));
}
}
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);
}
}
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));
}
}
而main也是一个函数
不能函数嵌套的!
int getb4() {return b4;}
加上public应该可以了
b4虽然为私有可可以通过getb4()获得
(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;}
}