两个问题一起提共100分,请热心的朋友解答一下,一定给分

wjpsky 2002-11-28 04:52:58
问题一:类B继承类A,并且覆盖类A中的方法a,为什么类B中的a方法的访问控制属性一定得大于等于类A中的a方法的访问控制属性。

问题二:
public class TestA
{
public void a()
{
System.out.println("TestA");
}
public TestA()
{
a();
}
}
public class TestB extends TestA
{
public void a()
{
System.out.println("TestB");
}
public static void main(String[] args)
{
new TestB();
}
}
第一种情况:TestA和TestB在同一包中
如果TestA中的的a方法的访问控制属性为private,程序结果为:TestA。
如果TestA中的的a方法的访问控制属性为pubilc、protected、无,程序结果为:TestB

第二种情况:TestA和TestB不在同一包中
如果TestA中的的a方法的访问控制属性为private、无,程序结果为:TestA。
如果TestA中的的a方法的访问控制属性为(pubilc、protected),程序结果为:TestB
...全文
31 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
wang_zheng_wz 2002-11-28
  • 打赏
  • 举报
回复
instantiate B时会先instantiate A,即生成一个A的object,于是执行TestA(),而在执行TestA()中的a()时,会首先在class A中找a(),如果没有,再从class B中找a(),无论a()是否被overridden
Javatomchen 2002-11-28
  • 打赏
  • 举报
回复
read the following carefully :

Java uses three explicit keywords to set the boundaries in a class: public, private, and protected. Their use and meaning are quite straightforward. These access specifiers determine who can use the definitions that follow. public means the following definitions are available to everyone. The private keyword, on the other hand, means that no one can access those definitions except you, the creator of the type, inside member functions of that type. private is a brick wall between you and the client programmer. If someone tries to access a private member, they’ll get a compile-time error. protected acts like private, with the exception that an inheriting class has access to protected members, but not private members. Inheritance will be introduced shortly.
Jacky1206 2002-11-28
  • 打赏
  • 举报
回复
chenyuan_tongji(codeguru高手的回答,非常漂亮
当TestA中的a()被声明为private后,其子类是不集成超类的private的元素的,所以就像chenyuan_tongji说的,再TestB中a()是新的方法,不是重载TestA中的a()
情况二一样,无对包外的方法或类来说就是private,同解了。
Javatomchen 2002-11-28
  • 打赏
  • 举报
回复
原因如下:
private :不能继承
public :继承
protected:包内继承,包外private
你的,明白?
weimenren 2002-11-28
  • 打赏
  • 举报
回复
问题一:类B继承类A,并且覆盖类A中的方法a,为什么类B中的a方法的访问控制属性一定得大于等于类A中的a方法的访问控制属性。


这是肯定需要大于的啊,在编译的时间B 继承A,也是A的属性先编译,而B的属性后编译的啊。我们可以定义 A instance = new B();也就是这个道理啊,后面的B需要有比A更大的访问控制属性,才能确保A中有的B是一定可以实现啊。

问题二:
1、TestA和TestB在同一包中
如果TestA中的的a方法的访问控制属性为private,程序结果为:TestA.

在这个时间new TestB();的时间,TestB()没有构件器,调用父类的构建器new TestA(),而new TestA()的时间调用private方法a,输出TestA。

如果TestA中的的a方法的访问控制属性为pubilc、protected、无,程序结果为:TestB

一样new TestB();的时间,调用父类的构建器new TestA(),而new TestA()的时间调用重写的方法a,输出TestB。

在同一个包中,default的方法是可以继承的,也就是说这个时间a()方法是重写。

而不在同一个包中,default的方法对于其他的包是private的



ilka 2002-11-28
  • 打赏
  • 举报
回复
agree chenyuan_tongji(codeguru)
chrischen79 2002-11-28
  • 打赏
  • 举报
回复
回答一:这个java的规定。

回答二:无论TestA和TestB是否在一个包里面,TestB是TestA的子类。TestA.a()如果modifier为private,则TestA.a()对其子类是不可见的,TestB中的a()是一个new method,即不是override。这种情况下,new TestB()->TestA()->a(),a()没有被override,直接使用基类的版本。

62,629

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧