📄 abtest.java
字号:
//ABTest.Java
//定义父类A
class A
{
private int a;
int i,j;
void setA(int a)
{
this.a=a;
}
int getA()
{
return a;
}
void setIJ(int i,int j)
{
this.i=i;
this.j=j;
}
}
class B extends A
{
int k;
void setK(int k)
{
this.k=k;
}
int sum()
{
//return a+i+j+k; 错误,子类不能继承父类的私有变量a
return i+j+k;
}
}
public class ABTest
{
public static void main(String args[]){
A father=new A();
B child=new B();
father.setA(5);
father.setIJ(10,20);
//System.out.println(“father.a=“+father.a); a为私有变量,不能直接访问
System.out.println("father.a="+father.getA());
System.out.println("father.i="+father.i);
System.out.println("father.j="+father.j);
child.setIJ(3,6);
child.setK(12);
System.out.println("child.i="+child.i);
System.out.println("child.j="+child.j);
System.out.println("child.k="+child.k);
System.out.println("child.i+j+k="+child.sum());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -