protected修饰的成员属性和方法的可见范围:子类或同一个包中的类
2013-12-23 22:00
387 查看
If a method or instance variable is modified by protected (rather than public or
private), then it can be accessed by name inside its own class definition, it can be
accessed by name inside any class derived from it, and it can also be accessed by name
in the definition of any class in the same package (even if the class in the same package
is not derived from it). However, the protected method or instance variable cannot be
accessed by name in any other classes. Thus, if an instance variable is marked protected
in the class Parent and the class Child is derived from the class Parent, then the
instance variable can be accessed by name inside any method definition in the class
Child. However, in a class that is not in the same package as Parent and is not derived
from Parent, it is as if the protected instance variable were private.
private), then it can be accessed by name inside its own class definition, it can be
accessed by name inside any class derived from it, and it can also be accessed by name
in the definition of any class in the same package (even if the class in the same package
is not derived from it). However, the protected method or instance variable cannot be
accessed by name in any other classes. Thus, if an instance variable is marked protected
in the class Parent and the class Child is derived from the class Parent, then the
instance variable can be accessed by name inside any method definition in the class
Child. However, in a class that is not in the same package as Parent and is not derived
from Parent, it is as if the protected instance variable were private.
相关文章推荐
- 子类继承父类的同时又去实现一个成员变量、方法与父类相同的接口,最后调的是谁的成员变量和方法?
- C# 利用反射,遍历获得一个类的所有属性名,方法名,成员名
- 对protected修饰符的范围用代码说明(同时说明用protected修饰的属性,在继承时,一定程度上破坏了封装)
- 具有protected修饰的成员变量和方法,在不同包的子父类中调用的细节问题
- 为什么子类中不能访问另一个包中父类中的protected方法?
- Java中类的继承,属性和方法的四种修饰符的作用范围,final关键字,java的三大特点中的2个:封装和多态,以及多态的一个设计模式,模板方法模式(template method)
- 被protected修饰的属性/方法在何处可以访问?
- 为什么子类中不能访问另一个包中父类中的protected方法?
- 被protected修饰的属性/方法在何处可以访问?
- 尝试编写一个矩形类,将长与宽作为矩形类的属性,在构造方法中将长。宽初始化,定义一个成员方法求此矩形的面积。
- 尝试创建一个父类和子类,分别创建一个构造方法,然后向父类和子类添加成员变量和方法,并总结构造子类对象时的顺序。
- JavaSE8基础 final 修饰类中的成员方法 不能被子类重写
- 关于子类无法访问父类protected属性和方法的问题
- php类函数和成员方法的可见性和属性
- 习题4:编写一个控制台应用程序,定义一个类MyClass,类中包含有public、private以及protected数据成员及方法。
- 关于protected在子类创建父类对象无法访问父类protected方法或成员
- C# 利用反射,遍历获得一个类的所有属性名,方法名,成员名.
- 针对一个XML,如果不想使用任何第三方控件,只是使用wxString,设置属性可以用一下方法来操作
- php根据一个给定范围和步进生成数组的方法
- 类的特性讲解:封装-私有属性-成员属性-公有属性-公有的方法变成私有的方法-析构方法