简体   繁体   English

如何调用抽象类方法

[英]How to call abstract class method

I have an interface called Hospital.java 我有一个名为Hospital.java的界面

public interface Hospital {
    public void operate();
    public void doScan();
    public void doVaccination();
}

I have an abstract class called StreetHospital.java 我有一个名为StreetHospital.java的抽象类

public abstract class StreetHospital implements Hospital{
    public void operate(){
        System.out.println("Street Hospital operate");
  }
}

Now I am using another class CommunityHospital.java to extend StreetHospital.java 现在我正在使用另一个类CommunityHospital.java来扩展StreetHospital.java

public class CommunityHospital extends StreetHospital{

    public void doScan(){
        System.out.println("CommunityHospital doScan");
    }
    public void doVaccination(){
        System.out.println("CommunityHospital doVaccination");
}
    public void operate(){
        System.out.println("CommunityHospital operate");
    }
}

I am creating the CommunityHospital object in my MainHospital class 我在MainHospital类中创建了CommunityHospital对象

public class MainHospital {

    public static void main(String[] args){
        CommunityHospital ch = new CommunityHospital();
        ch.operate();
        ch.doScan();
        ch.doVaccination();

    }
}

I am getting this output: 我得到这个输出:

CommunityHospital operate
CommunityHospital doScan
CommunityHospital doVaccination

My question is how do I print "Street Hospital operate" sysout statement in output? 我的问题是如何在输出中打印“街道医院操作”sysout语句?

You can't do that. 你不能这样做。

There can't be two methods in one class with same name and param. 在一个具有相同名称和参数的类中不能有两个方法。 Once you override operate() in CommunityHospital , the same mthod in StreetHospital no longer exist. 一旦你在CommunityHospital中覆盖了operations(),StreetHospital中的同一个方法就不再存在了。

You need to use super keyword to invoke the superclass members in the subclass as shown below: 您需要使用super关键字来调用子类中的超类成员,如下所示:

public class CommunityHospital extends StreetHospital{

    public void doScan(){
        System.out.println("CommunityHospital doScan");
    }
    public void doVaccination(){
        System.out.println("CommunityHospital doVaccination");
    }
    public void operate(){
        super.operate();//call super class operate to get StreetHospital features
        //If you want, you can add CommunityHospital features here
    }
}

I suggest you look here for more details and understand the concept. 我建议你在这里查看更多细节并理解这个概念。

If your method overrides one of its superclass's methods, you can invoke the overridden method through the use of the keyword super . 如果您的方法覆盖了其超类的方法之一,则可以通过使用关键字super来调用重写方法。

One way is to remove the operate method from CommunityHospital class. 一种方法是从CommunityHospital类中删除operate方法。

In terms of design that would mean that community hospital's operate does not differ from a street hospital's operate. 在设计方面,这意味着社区医院的运营与街道医院的运营没有区别。

Another consideration would be that you actually want street hospital's operate and then there are some specifics that need to be accomplished in community hospital's operate. 另一个考虑因素是你实际上想要街道医院的运作,然后在社区医院的运作中需要完成一些具体细节。 For that CommunityHospital operate function should look like: 对于CommunityHospital operate功能应如下所示:

public void operate() {
        super.operate(); //Street hospital operate
        // Add community hospital operate details here
} 

You are extending a class, but then overwriting its methods. 您正在扩展一个类,但随后会覆盖它的方法。 So whenever you call operate it will use the one in CommunityHospital which is the overwritten one. 因此,无论何时召唤operate它都将使用CommunityHospital那个被覆盖的那个。 If you remove that, it will find the operate method from the StreetHospital class. 如果删除它,它将从StreetHospital类中找到operate方法。

If for some reason you do need an operate method in CommunityHospital , don't implement a new one and just call super.operate() . 如果出于某种原因,您确实需要在CommunityHospital使用operate方法,请不要实现新方法,只需调用super.operate() This will call the StreetHospital class' operate method. 这将调用StreetHospital类的operate方法。

CommunityHospital overrides the operate method, so if that class doesn't call super then is not possible... CommunityHospital覆盖了操作方法,所以如果那个类没有调用super那么就不可能......

do modify the class like: 修改类如:

 public void operate(){
        super.operate(); // call the method from super class

    }

Add another function to CommunityHospital with another name say superOperate like this: CommunityHospital添加另一个函数,另一个名称是superOperate如下所示:

public void superOperate(){
    super.operate();
}

and call it from anywhere 并从任何地方调用它

 public void operate(){
        super.operate(); // call the method from super class

    }

or just remove 或者只是删除

@Override
    public void operate() {
        System.out.println("CommunityHospital operate");
    }

form CommunityHospital 形成社区医院

If you want to write StreetHospital in a way so that subclasses can override operate() , but the StreetHospital 's operate() (the non-overridden one) can still be called, you could do this: 如果你想以某种方式编写StreetHospital ,以便子类可以覆盖operate() ,但仍可以调用StreetHospital operate() (未覆盖的),你可以这样做:

public abstract class StreetHospital implements Hospital {
    public final void streetOperate() {
        System.out.println("Street Hospital operate");
    }
    public void operate() {
        streetOperate();
    }
}

Now operate() can be overridden, but streetOperate() can still be called on any CommunityHospital object, or any other subclass of StreetHospital . 现在operate()可以被重写,但streetOperate()仍然可以在任何所谓CommunityHospital对象,或任何其他子类StreetHospital

However, it's really difficult for me to think of an actual case where this would be a good design. 但是,我真的很难想到一个实际情况,这将是一个很好的设计。

You could do it by using super 你可以用super来做到这一点

class CommunityHospital extends StreetHospital{
    // same code
    public void operate(){
        super.operate(); //-> this line
        //System.out.println("CommunityHospital operate");
    }
}

You could also make the operate method final , if you don't want to allow overriding of it. 如果您不想允许覆盖它,也可以使operate方法final

abstract class StreetHospital implements Hospital{
    final public void operate(){
        System.out.println("Street Hospital operate");
    }
}

As long as you don't override the operate method, it will be available in your class when you extend it. 只要您不覆盖操作方法,当您扩展它时,它将在您的类中可用。

So don't override the operate method in your class when you are extending it. 因此,在扩展它时,不要覆盖类中的操作方法。

public class CommunityHospital extends StreetHospital {
   public class CommunityHospital extends StreetHospital {

    public void doScan() {
        System.out.println("CommunityHospital doScan");
    }

    public void doVaccination() {
        System.out.println("CommunityHospital doVaccination");
    }
}

This shall work. 这应该工作。

Unfortunately there is no other way than calling super.operate in subclass. 不幸的是,除了在子类中调用super.operate之外super.operate他法。

Upcasting in java doesn't work. 在java中向上转换不起作用。 Demo that shows it: 显示它的演示:

package com.stackoverflow.main;

class A {
    void operate() {
        System.out.println("A");
    }
}

class B extends A {
    void operate() {
        System.out.println("B");
    }
}

public class Main {

    public static void main(String[] args) {
        A b = new B();
        ((A) b).operate();
    }
}

Prints: 打印:

B

Use the concept of down casting . 使用向下铸造的概念。 I have written the code in C# 我用C#编写了代码

abstract class MyClass1 {
    public void operate() {
        System.Console.WriteLine("base class");
    }
    public abstract void operate1();
    public virtual void operate2() {
        System.Console.WriteLine("Virtual");
    }
}

class myclass2 : MyClass1 {
    public override void operate1() {
        Console.WriteLine("Success");
    }
    public override void operate2() {
        Console.WriteLine("overidden");
    }
    public new void operate() {
        Console.WriteLine("!base class");
    }
}

class program {
    static void Main(string[] args) {
        MyClass1 m = new myclass2();
        m.operate();
    }
}

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

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