/*Filename: FinalAndPrivate.java
* Task: 不同访问修饰符在继承中的用法
* 在创建派生类时:
* (1)基类final方法不能被覆盖,保证基类构造函数始终调用同一方法
* (2)基类private可以被派生类覆盖,也可以保证基类构造函数调用对应自己的方法
* (3)基类的default,protected,public方法将被派生类中的同名方法替换
*/
public class FinalAndPrivate {
public static void main(String[] args){
System.out.println("new Base");
new Base();
System.out.println("new Child");
new Child();
}
}
class Base {
Base(){
String s="was Called in Base Construction";
System.out.println("In Base Construction");
finalMethod(s);
privateMethod(s);
defaultMethod(s);
protectedMethod(s);
publicMethod(s);
}
final void finalMethod(String s){
String des="finalMethod Def in Base";
System.out.println(des+s);
}
private void privateMethod(String s){
String des="privateMethod Def in Base";
System.out.println(des+s);
}
void defaultMethod(String s){
String des="defaultMethod Def in Base";
System.out.println(des+s);
}
protected void protectedMethod(String s){
String des="protectedMethod Def in Base";
System.out.println(des+s);
}
public void publicMethod(String s){
String des="publicMethod Def in Base";
System.out.println(des+s);
}
}
class Child extends Base{
Child(){
String s="was Called in Child Construction";
System.out.println("In Child Construction");
System.out.println(s);
finalMethod(s);
privateMethod(s);
defaultMethod(s);
protectedMethod(s);
publicMethod(s);
}
/*
final void finalMethod(String s){
String des="finalMethod Def in Base";
System.out.println(des+s);
}
*/
private void privateMethod(String s){
String des="privateMethod Def in Child";
System.out.println(des+s);
}
void defaultMethod(String s){
String des="defaultMethod Def in Child";
System.out.println(des+s);
}
protected void protectedMethod(String s){
String des="protectedMethod Def in Child";
System.out.println(des+s);
}
public void publicMethod(String s){
String des="publicMethod Def in Child";
System.out.println(des+s);
}
}
/*
Programme Print:
new Base
In Base Construction
finalMethod Def in Basewas Called in Base Construction
privateMethod Def in Basewas Called in Base Construction
defaultMethod Def in Basewas Called in Base Construction
protectedMethod Def in Basewas Called in Base Construction
publicMethod Def in Basewas Called in Base Construction
new Child
In Base Construction
finalMethod Def in Basewas Called in Base Construction
privateMethod Def in Basewas Called in Base Construction
defaultMethod Def in Childwas Called in Base Construction
protectedMethod Def in Childwas Called in Base Construction
publicMethod Def in Childwas Called in Base Construction
In Child Construction
was Called in Child Construction
finalMethod Def in Basewas Called in Child Construction
privateMethod Def in Childwas Called in Child Construction
defaultMethod Def in Childwas Called in Child Construction
protectedMethod Def in Childwas Called in Child Construction
publicMethod Def in Childwas Called in Child Construction
*/
方法访问修饰符在继承中的用法
本文通过一个Java示例程序展示了不同访问修饰符(final, private, default, protected, public)在类继承中的作用。演示了如何使用这些修饰符来控制基类方法在派生类中的可见性和覆盖行为。

被折叠的 条评论
为什么被折叠?



