编写一个类,使用继承和组合2种方法复用这个类

来源:百度知道 编辑:UC知道 时间:2024/05/15 05:35:54
用Java语言

class A
{
int i;
A()
{
i = 30;
}
public int getI()
{
return i;
}
}

class B extends A
{
int j;
B()
{
super();
j = 40;
}
public int getI()
{
return j;
}
}

class C
{
A a;
int m;
C()
{
a = new A();
m = 50;
}
}

public class Test
{
public static void main(String[] args)
{
A a = new A();
System.out.println(a.getI());
B b = new B();
System.out.println(b.i);
System.out.println(b.getI());
C c = new C();
System.out.println(c.a.i);
System.out.println(c.a.getI());
}
}