下例不能编译通过,证明final方法不能被继承重写 class AA{ final void func(){System.out.println("in parent");} } class BB extends AA{ void func(){System.out.println("in parent");} public static void main(String[] args){ BB b=new BB(); b.func(); } }
不能被继承的方法? 不行的。要么你的类就不要让人继承。要么你可以考虑如下代码public class TestParent{final void cannotInheiMethod( TestParent tp ){ if( tp instanceof TestParent ){ do what you want } else { throw not support this method exception } }}
不让继承可能不行,但是能阻止子类直接调用,相当于间接不让继承。(子类有这个方法,但是不能使用)public class A { public final void foo() { if (!(this.getClass().getName().equals(B.class.getName()))) { throw new NoSuchMethodError(); } ...... } }A a = new A(); a.foo(); //完全正常 B b = new B(); // B extends A b.foo(); // 会出错 A aa = new B(); aa.foo(); // 类型转换还是会出错 这样,就相当于间接不让继承了。 :-)
final方法可以继承,但是不能重载,6楼的哥们概念不清
上面应该判断的是 if (!(this.getClass().getName().equals(A.class.getName()))) {bluesmile979(笑着) 的 instanceof 可能达不到目的。
class A { public final void test() { } } class B extends A { public void test() { } }这样根本不能编译通过,声明了final的方法是不允许子类继承之的
public final <E> method(){}
俺刚明白楼主的意思: A类有public final 方法a一个, B类继承A类,却不希望B类有a方法。
class A { void AA() { System.out.println("just Test A"); } final void AA2() { System.out.println("just Test A final"); } }class B extends A { void BB() { System.out.println("just Test B"); } }public class FinalTest { public static void main(String arg[]) { B b = new B(); b.AA(); b.AA2(); } }这个为什么能编译通过?结果是 just Test A just Test A final
class A { public final void test() { } } class B extends A { public void test() { } }这样根本不能编译通过,声明了final的方法是不允许子类继承之的 =============================================================to ChDw(米) : 你这个明显是重载好不好,怎么变成继承了阿. final方法是不允许被重载,不是被继承.
final不能继承 static是前期帮定,复写了也没用
修正一下我的代码,没仔细考虑就把代码敲出来了,不好意思。public class TestParent { public TestParent() { } public final void testMethod( TestParent tp ){ if( tp.getClass().getName().equals( TestParent.class.getName() ) ){ System.out.println("testMethod"); } else { System.out.println("not support this method"); } } public void print(){ System.out.println("print"); } }
这个问题实际是有可能碰到的,jdk里面好像也有类似的代码,不过我记不清楚是在哪里看到的了。
人{ public void 走路(); public void 说话(); } 聋哑人 exntends 人{ public void 走路() {} public void 说话() { //应该在这里标明子类的特殊性。 //比如说:说不出话 //而不是让聋哑人没有说话这个方法 } }所以说,应该考虑你的设计,而不是出现奇怪的代码
不懂!
class AA{
final void func(){System.out.println("in parent");}
}
class BB extends AA{
void func(){System.out.println("in parent");}
public static void main(String[] args){
BB b=new BB();
b.func();
}
}
if( tp instanceof TestParent ){
do what you want
} else {
throw not support this method exception
}
}}
public final void foo() {
if (!(this.getClass().getName().equals(B.class.getName()))) {
throw new NoSuchMethodError();
}
......
}
}A a = new A();
a.foo(); //完全正常
B b = new B(); // B extends A
b.foo(); // 会出错
A aa = new B();
aa.foo(); // 类型转换还是会出错
这样,就相当于间接不让继承了。
:-)
if (!(this.getClass().getName().equals(A.class.getName()))) {bluesmile979(笑着) 的 instanceof 可能达不到目的。
public final void test() {
}
}
class B extends A {
public void test() {
}
}这样根本不能编译通过,声明了final的方法是不允许子类继承之的
A类有public final 方法a一个,
B类继承A类,却不希望B类有a方法。
void AA() {
System.out.println("just Test A");
}
final void AA2() {
System.out.println("just Test A final");
}
}class B extends A {
void BB() {
System.out.println("just Test B");
}
}public class FinalTest {
public static void main(String arg[]) {
B b = new B();
b.AA();
b.AA2();
}
}这个为什么能编译通过?结果是
just Test A
just Test A final
public final void test() {
}
}
class B extends A {
public void test() {
}
}这样根本不能编译通过,声明了final的方法是不允许子类继承之的
=============================================================to ChDw(米) : 你这个明显是重载好不好,怎么变成继承了阿.
final方法是不允许被重载,不是被继承.
static是前期帮定,复写了也没用
} public final void testMethod( TestParent tp ){
if( tp.getClass().getName().equals( TestParent.class.getName() ) ){
System.out.println("testMethod");
} else {
System.out.println("not support this method");
}
} public void print(){
System.out.println("print");
}
}
public void 走路();
public void 说话();
}
聋哑人 exntends 人{
public void 走路() {}
public void 说话() {
//应该在这里标明子类的特殊性。
//比如说:说不出话
//而不是让聋哑人没有说话这个方法
}
}所以说,应该考虑你的设计,而不是出现奇怪的代码