day11-final关键字用于修饰方法
This commit is contained in:
13
day11/src/com/inmind/final_method/Demo02.java
Normal file
13
day11/src/com/inmind/final_method/Demo02.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package com.inmind.final_method;
|
||||
|
||||
public class Demo02 {
|
||||
public static void main(String[] args) {
|
||||
//创建一个父类对象
|
||||
Fu fu = new Fu();
|
||||
fu.method();
|
||||
//多态的写法:
|
||||
Fu f = new Zi();//格式:父类引用指向子类对象
|
||||
//编译看左边,运行看右边
|
||||
f.method();
|
||||
}
|
||||
}
|
||||
9
day11/src/com/inmind/final_method/Fu.java
Normal file
9
day11/src/com/inmind/final_method/Fu.java
Normal file
@@ -0,0 +1,9 @@
|
||||
package com.inmind.final_method;
|
||||
/*
|
||||
final关键字用于修饰方法:该方法就不能被子类重写
|
||||
*/
|
||||
public class Fu {
|
||||
public final void method(){
|
||||
System.out.println("父类的method方法");
|
||||
}
|
||||
}
|
||||
14
day11/src/com/inmind/final_method/Zi.java
Normal file
14
day11/src/com/inmind/final_method/Zi.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package com.inmind.final_method;
|
||||
|
||||
public class Zi extends Fu{
|
||||
//ctrl+o
|
||||
|
||||
/*@Override
|
||||
public void method() {
|
||||
//super.method();//调用父类的成员方法,沿用
|
||||
System.out.println("子类的method方法,重写了父类的功能");
|
||||
}*/
|
||||
|
||||
public void methodZi(){
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user