Presentation is loading. Please wait.

Presentation is loading. Please wait.

辅导课程十一.

Similar presentations


Presentation on theme: "辅导课程十一."— Presentation transcript:

1 辅导课程十一

2 访问控制(Access Control) Java权限修饰符 public、protected、private 置于类的成员定义前,用来限定其它对象对该类对象成员的访问权限。 对于class的权限修饰只可以用 public 和 default。(inner class除外) public类可以在任意地方被访问 default类只可以被同一个包内部的类访问 修饰符 类内部 同一个包 子类 任何地方 private Yes default protected public TestAccess/TestAccess.java

3 类的继承中的访问权限控制 class Parent{ private int n_private =1;
int n_friendly = 2; protected int n_protected = 3; public int n_public = 4; } class Child extends Parent { public void f(){ //n_private =10; n_friendly = 20; n_protected = 30; n_public = 40;

4 方法的重写(覆盖) overwrite(override)
在子类中可以根据需要对从基类中继承来的方法进行重写。 重写方法必须和被重写方法具有相同方法名称、参数列表和返回类型。 重写方法不能使用比被重写方法更严格的访问权限。 TestOverWrite/TestOverWrite.java

5 super 关键字 在Java类中使用super来引用基类的成分。例如: TestSuper/TestSuper.java
class FatherClass { public int value; public void f(){ value = 100; System.out.println("FatherClass.value="+value); } class ChildClass extends FatherClass { public void f() { super.f(); value = 200; System.out.println("ChildClass.value="+value); System.out.println(value); System.out.println(super.value); TestSuper/TestSuper.java

6 stack heap value:100 value:200 cc:xx

7 stack heap value:100 value:200 super this cc:xx

8 stack heap value:100 this value:200 super this cc:xx


Download ppt "辅导课程十一."

Similar presentations


Ads by Google