/*
继承的代码体现
由于继承中方法有一个现象:方法重写。
所以,父类的功能,就会被子类给覆盖掉。
有时候我们不想让子类去覆盖父类的功能,只能让子类去使用。
这时针对这种情况,java就提供了一个关键字:final
final:最终的意思。常见的是它可以修饰类,方法,变量。
*/
=================练习一========================
class Person{
public void show(){ System.out.println("重要方法"); } } class Student extends Person{ public void show(){ System.out.println("非重要方法"); } } class Test{ public static void main(String [] args){ Student s =new Student(); s.show(); } } 结果:
非重要方法
============================练习二=========================
class Person{
public final void show(){ System.out.println("重要方法"); } } class Student extends Person{ public void show(){ System.out.println("非重要方法"); } } class Test{ public static void main(String [] args){ Student s =new Student(); s.show(); } } 结果:
Student 中的 show() 无法覆盖 Person 中的 show();被覆盖的方法为 final
|
|