简体   繁体   English

抽象类和多态问题

[英]Abstract class and polymorphism issue

I am trying to do the following:我正在尝试执行以下操作:

public abstract class MyBaseFragment extends Fragment {
    private FloatingActionButton fab;

    protected void initFab(View contentView, int resourceId) {
        fab = (FloatingActionButton) contentView.findViewById(resourceId);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                launchDetailsDialogFragment(new Animal());
            }
        });
    }

    private void launchDetailsDialogFragment(Animal animal) {
        //... 
    }
}

The key point here being the line about making a new Animal.这里的关键点是关于制作新动物的路线。

I have various types of Animals -- Cat, Dog, etc. The page about Cats, the page about Dogs, etc, they will all extend MyBaseFragment.我有各种类型的动物——猫、狗等。关于猫的页面、关于狗的页面等,它们都将扩展 MyBaseFragment。 So clicking the FloatingActionButton on the Cat page will execute a function that passes in a new Cat() to the DialogFragment where you can edit its details.因此,单击 Cat 页面上的 FloatingActionButton 将执行一个函数,该函数将一个 new Cat() 传递到 DialogFragment,您可以在其中编辑其详细信息。 Same for Dogs.狗也一样。 But each details DialogFragment is a little different, since Cats and Dogs have different properties from each other even though they are both Animals.但是每个细节 DialogFragment 都有点不同,因为 Cats 和 Dogs 彼此具有不同的属性,即使它们都是 Animals。

However I don't know how to apply polymorphism here.但是我不知道如何在这里应用多态性。 Right now all MyBaseFragment knows is that it will make a new Animal, but really I want it to make a new Cat, or a new Dog, etc, depending on where I am extending the base fragment.现在 MyBaseFragment 只知道它会创建一个新的 Animal,但我真的希望它创建一个新的 Cat,或一个新的 Dog 等,这取决于我扩展基础片段的位置。

How do I do this?我该怎么做呢?

Just make the called method abstract ad have each concrete class fill in its own implementation只需让被调用的方法抽象广告让每个具体类填写自己的实现

public abstract class MyBaseFragment extends Fragment {
    private FloatingActionButton fab;

    protected void initFab(View contentView, int resourceId) {
        fab = (FloatingActionButton) contentView.findViewById(resourceId);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                launchDetailsDialogFragment();
            }
        });
    }

    protected abstract void launchDetailsDialogFragment();
}

是的,您可以使用“访问者”模式处理对象系列: https : //en.wikipedia.org/wiki/Visitor_pattern

Make launchDetailsDialogFragment abstract:使launchDetailsDialogFragment抽象:

protected abstract void launchDetailsDialogFragment();

And implement it in the subclasses like you want.并在您想要的子类中实现它。

You must let the MyBaseFragment specific implementations decide which animal to instantiate while launching the detail fragment.As far as I can understand the MyBaseFragment abstract class only knows that there should be a detailfragment which should be launched on click event of fabbutton, it does not know of the the type of fragment which should be launched.This would be decided by the specific implementation of the fragment.你必须让 MyBaseFragment 特定的实现决定在启动细节片段时实例化哪个动物。 据我所知,MyBaseFragment 抽象类只知道应该有一个细节片段应该在 fabbutton 的点击事件上启动,它不知道应该启动的fragment的类型。这将由fragment的具体实现决定。 So, mark the method responsible for creating the detailfragment as protected abstract and override them in the specific implementations to create the specific animal.因此,将负责创建 detailfragment 的方法标记为受保护的抽象,并在特定实现中覆盖它们以创建特定动物。

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

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