Re: [问题] private继承问题

楼主: qrtt1 (有些事,有时候。。。)   2014-07-14 22:19:28
※ 引述《kiwatami (悠游自在)》之铭言:
: ※ 引述《meya (落寞之心)》之铭言:
: : class A{
: : private int a1=1;
: : }
: : public class test extends A{
: : public static void main(String[] args){
: : test t=new test();
: : System.out.println(t.a1);
: : }
: : }
: : 子类别无法直接存取父类别的private a1,但
: : 究竟子类别有无继承父类别的private a1?
: : 应该用下列哪种理解才正确?
: : (A)子类别内的a1继承自父类别的a1,只是父类别用private修饰a1,让其他类别无法直接
: : 存取父类别内的a1
: : (B)使用private修饰过的a1,无法被子类别继承
: : 恳请回答谢谢
: 答案是A
: 当然有被继承
: 不然你用get会拿到什么?
: 用private只是封装变量不给外部使用
: 用意是在外部程式若随便更改变量可能会导致程式出现错误(当然这不是唯一的目的)
: 所以要封装起来由setter做更动
: 不一定正确啦 因为我满讨厌读定义的XD
: 你参考一下
http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.2
Members of a class that are declared private are
not inherited by subclasses of that class.
Only members of a class that are declared protected
or public are inherited by subclasses declared
in a package other than the one in which the class is declared.

Links booklink

Contact Us: admin [ a t ] ucptt.com