方法访问修饰符在继承中的用法

本文通过一个Java示例程序展示了不同访问修饰符(final, private, default, protected, public)在类继承中的作用。演示了如何使用这些修饰符来控制基类方法在派生类中的可见性和覆盖行为。

/*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
*/

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值