覆盖父级的内部接口?

时间:2014-03-22 02:54:09

标签: java android

我有两个非常相似的类,除了一些方法。出于这个原因,我希望将所有方法放在一个类(Parent)中,然后从Child扩展Parent,覆盖其中的一些方法和接口。对于我所看到的,在子代中声明一个方法直接覆盖了父代的相同方法,但是我还没有能够覆盖父接口。这就是我正在尝试的:

家长班:

public class ContactList extends ListFragment implements SwipeListener, AdapterView.OnItemClickListener, LoaderManager.LoaderCallbacks<Cursor> {

    //...

    public interface ContactsQuery {
        final static int QUERY_ID = 1;
        final static Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI;

        @SuppressLint("InlinedApi")
        final static String SELECTION = Contacts.HAS_PHONE_NUMBER + "=1" + " AND " + Contacts.IN_VISIBLE_GROUP + "=1" + " AND " + (Utils.hasHoneycomb() ? Contacts.DISPLAY_NAME_PRIMARY : Contacts.DISPLAY_NAME) + "<>''";

        @SuppressLint("InlinedApi")
        final static String SORT_ORDER = Utils.hasHoneycomb() ? Contacts.SORT_KEY_PRIMARY : Contacts.DISPLAY_NAME;

        @SuppressLint("InlinedApi")
        final static String[] PROJECTION = {
                Contacts._ID,
                Contacts.LOOKUP_KEY, Utils.hasHoneycomb() ? Contacts.DISPLAY_NAME_PRIMARY : Contacts.DISPLAY_NAME,
                SORT_ORDER,
        };

        // The query column numbers which map to each value in the projection
        final static int ID = 0;
        final static int LOOKUP_KEY = 1;
        final static int DISPLAY_NAME = 2;
        final static int SORT_KEY = 3;
    }

儿童班:

public class ContactListFavs extends ContactList {
    public interface ContactsQuery {
        final static int QUERY_ID = 1;
        final static Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI;

        @SuppressLint("InlinedApi")
        final static String SELECTION = Contacts.STARRED + " AND " + Contacts.HAS_PHONE_NUMBER + "=1" + " AND " + Contacts.IN_VISIBLE_GROUP + "=1" + " AND " + (Utils.hasHoneycomb() ? Contacts.DISPLAY_NAME_PRIMARY : Contacts.DISPLAY_NAME) + "<>''";

        @SuppressLint("InlinedApi")
        final static String SORT_ORDER = Utils.hasHoneycomb() ? Contacts.SORT_KEY_PRIMARY : Contacts.DISPLAY_NAME;

        @SuppressLint("InlinedApi")
        final static String[] PROJECTION = {
                Contacts._ID,
                Contacts.LOOKUP_KEY, Utils.hasHoneycomb() ? Contacts.DISPLAY_NAME_PRIMARY : Contacts.DISPLAY_NAME,
                SORT_ORDER,
        };

        // The query column numbers which map to each value in the projection
        final static int ID = 0;
        final static int LOOKUP_KEY = 1;
        final static int DISPLAY_NAME = 2;
        final static int SORT_KEY = 3;
    }
}

然而,我无法弄清楚如何做到这一点。在实例化Child类时,它使用来自它父级的接口。如何用孩子的方法覆盖它?

2 个答案:

答案 0 :(得分:0)

你可以这样做......

class Parent
    {
        interface myP{
            public int good();
            public int good2();
            public int good3();
        }

    }

class Child extends Parent
    {
      interface myC extends Parent.myP{
            public int good();
            public int NOTgood();
        }
    }

如果您extend Childimplement Child.myC,您必须实施本案例中的所有方法,这将会发生什么。

子接口(myC)方法:

public int good();
public int NOTgood();

父接口(myP)方法:

public int good();
public int good2();
public int good3();

但您需要将访问修饰符接口保持公开,默认或受保护,但不私有

这两个界面都有一个常见的方法good()

答案 1 :(得分:0)

而不是类Parent扩展尝试使用和实现两个类(超类和子类)的接口。该版本有点冗长,但它对代码重用和修改更易于维护和清晰:

public interface iCalculate 
{
    public int calculate();
}

public class Parent implements iCalculate
{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        int res = calculate();
    }

    public int calculate()
    {
        return 1 + 1;
    }
}

public class Child implements iCalculate
{
    public int calculate()
    {
        return 2 + 2;
    }
}

如果您使用的是Java 8,则可以使用default methods in interface实现来实现传统的继承功能。这次(在界面默认方法的情况下),当您的默认行为适合时,您不需要实现方法。

相关问题