在抽象类java中调用非抽象方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27279341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
calling non abstract method in abstract class java
提问by Dilis
I have 3 classes. It seems basic question. But I can'nt find answer by googling.
我有 3 节课。这似乎是基本问题。但我无法通过谷歌搜索找到答案。
public abstract class Test {
void t1()
{
System.out.println("super");
}
}
public class concret extends Test{
void t1()
{
System.out.println("child");
}
void t2()
{
System.out.println("child2");
}
}
public class run {
public static void main(String[] args) {
Test t=new concret();
t.t1();
}
}
How do I call abstract class t1 method? Since I cant create object from abstract class how do I call t1 in abstract class? Thank you.
如何调用抽象类 t1 方法?由于我无法从抽象类创建对象,我如何在抽象类中调用 t1 ?谢谢你。
采纳答案by Jon Skeet
Either you create a concrete class which doesn'toverride the method, or within a concrete class which doesoverride the method, you can call super.t1()
. For example:
要么你创建一个具体的类,它不覆盖的方法,或混凝土类内不覆盖的方法,你可以调用super.t1()
。例如:
void t1()
{
super.t1(); // First call the superclass implementation
System.out.println("child");
}
If you've only got an instance of an object which overrides a method, you cannotcall the original method from "outside" the class, because that would break encapsulation... the purpose of overriding is to replacethe behaviour of the original method.
如果您只有一个覆盖方法的对象实例,则不能从类的“外部”调用原始方法,因为这会破坏封装……覆盖的目的是替换原始方法的行为.
回答by Stav Saad
Abstract class means the class has the abstract
modifier before the class
keyword. This means you can declare abstract methods, which are only implemented in the concrete classes.
抽象类意味着该类abstract
在class
关键字之前具有修饰符。这意味着您可以声明抽象方法,这些方法仅在具体类中实现。
For example :
例如 :
public abstract class Test {
public abstract void foo();
}
public class Concrete extends Test {
public void foo() {
System.out.println("hey");
}
}
回答by Rami Del Toro
Your code seems to call t1(). However this is calling the concrete t1() because the abstract t1() has been overridden by the concrete class.
您的代码似乎调用了 t1()。然而,这是调用具体的 t1() 因为抽象 t1() 已被具体类覆盖。
If you wish to call the abstract t1 method from main code, do not override the t1() in concrete.
如果您希望从主代码中调用抽象的 t1 方法,请不要覆盖具体的 t1()。
Or you can create a method in the concrete class for example:
或者您可以在具体类中创建一个方法,例如:
public void invokeSuperT1(){
super.t1();
}
回答by Alan Hay
See following tests:
请参阅以下测试:
public abstract class BaseClass {
public void doStuff() {
System.out.println("Called BaseClass Do Stuff");
}
public abstract void doAbstractStuff();
}
public class ConcreteClassOne extends BaseClass{
@Override
public void doAbstractStuff() {
System.out.println("Called ConcreteClassOne Do Stuff");
}
}
public class ConcreteClassTwo extends BaseClass{
@Override
public void doStuff() {
System.out.println("Overriding BaseClass Do Stuff");
}
@Override
public void doAbstractStuff() {
System.out.println("Called ConcreteClassTwo Do Stuff");
}
}
public class ConcreteClassThree extends BaseClass{
@Override
public void doStuff() {
super.doStuff();
System.out.println("-Overriding BaseClass Do Stuff");
}
@Override
public void doAbstractStuff() {
System.out.println("Called ConcreteClassThree Do Stuff");
}
}
public class Test {
public static void main(String[] args) {
BaseClass a = new ConcreteClassOne();
a.doStuff(); //Called BaseClass Do Stuff
a.doAbstractStuff(); //Called ConcreteClassOne Do Stuff
BaseClass b = new ConcreteClassTwo();
b.doStuff(); //Overriding BaseClass Do Stuff
b.doAbstractStuff(); //Called ConcreteClassTwo Do Stuff
BaseClass c = new ConcreteClassThree();
c.doStuff(); //Called BaseClass Do Stuff
//-Overriding BaseClass Do Stuff
c.doAbstractStuff(); //Called ConcreteClassThree Do Stuff
}
}
回答by Praveen
Create an anonymous Inner class,
创建一个匿名内部类,
Abstract class:
抽象类:
abstract class Test{
abstract void t();
public void t1(){
System.out.println("Test");
}
}
Here is how to create anonymous inner class:
下面是创建匿名内部类的方法:
Test test = new Test() {
@Override
void t() {
//you can throw exception here, if you want
}
};
Call the class via the object created for abstract class,
通过为抽象类创建的对象调用类,
test.t1();
回答by ShreyJo10
use keyword 'super' to do that
使用关键字“super”来做到这一点
void t1()
{ super.t1();
System.out.println("child");
}
Make sure you use that in the overriden method though.
不过,请确保在覆盖方法中使用它。
回答by NikhilP
you should be able to do it using
你应该可以使用
Test test = new Test(){};
test.t1();