简体   繁体   English

在抽象类java中调用非抽象方法

[英]calling non abstract method in abstract class java

I have 3 classes.我有3节课。 It seems basic question.这似乎是基本问题。 But I can'nt find answer by googling.但我无法通过谷歌搜索找到答案。

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?如何调用抽象类 t1 方法? Since I cant create object from abstract class how do I call t1 in abstract class?由于我无法从抽象类创建对象,如何在抽象类中调用 t1? Thank you.谢谢你。

Either you create a concrete class which doesn't override the method, or within a concrete class which does override the method, you can call super.t1() .您可以创建一个覆盖该方法的具体类,或者在一个确实覆盖该方法的具体类中,您可以调用super.t1() For example:例如:

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 cannot call the original method from "outside" the class, because that would break encapsulation... the purpose of overriding is to replace the behaviour of the original method.如果您只有一个覆盖方法的对象的实例,则不能从类“外部”调用原始方法,因为这会破坏封装......覆盖的目的是替换原始方法的行为.

you should be able to do it using你应该可以使用

Test test = new Test(){};
test.t1();

Abstract class means the class has the abstract modifier before the class keyword.抽象类意味着该类在class关键字之前具有abstract修饰符。 This means you can declare abstract methods, which are only implemented in the concrete classes.这意味着您可以声明仅在具体类中实现的抽象方法。

For example :例如 :

public abstract class Test {
     public abstract void foo();
}

public class Concrete extends Test {
    public void foo() {
        System.out.println("hey");
    }
}

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
    }
}

use keyword 'super' to do that使用关键字“超级”来做到这一点

void t1()
     {  super.t1();
        System.out.println("child");

    }

Make sure you use that in the overriden method though.不过,请确保在覆盖的方法中使用它。

Your code seems to call t1().您的代码似乎调用了 t1()。 However this is calling the concrete t1() because the abstract t1() has been overridden by the concrete class.然而,这是调用具体的 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();
    }

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();

An abstract class is used when we want that every class that inherited from our abstract class should implement that abstract method, so it is must to implement method otherwise it gives the compile-time error.当我们希望从我们的抽象类继承的每个类都应该实现该抽象方法时,使用抽象类,因此必须实现该方法,否则会产生编译时错误。

void t1()
    {
        super.t1; // means the parent methods
        System.out.println("child");
    } 

For example: Bird class has method sing() and there other classes that inherited from it like the sparrow, Pigeon, Duck, so these all have sing method so we make Bird class Abstract and make the sing() method abstract in it so every child of bird that implements Bird class should have a method of sing() with its on implementation.例如:Bird 类有 sing() 方法,还有其他继承自它的类,如麻雀、Pigeon、Duck,所以这些都有 sing 方法,所以我们将 Bird 类设为 Abstract 并将 sing() 方法抽象在其中,这样每个实现 Bird 类的 bird 的子对象应该有一个 sing() 方法及其 on 实现。

  1. First Create abstarct class like as shown in link: Creating Abstract Class首先创建抽象类,如链接所示:创建抽象类
  2. Create Sub-Classs like as shown in link: Sub-class extending创建子类,如链接所示:子类扩展
  3. Creating main method for executing this as show in link: Instanciate the subclass to access创建执行此操作的主要方法,如链接所示:实例化要访问的子类
  4. Result as shown here: Result结果如下所示:结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM