无法从抽象类

时间:2018-06-13 18:26:14

标签: java android

我正在尝试从SingleChoiceViewHolder.java中的SettingsItem.java调用getSetting()。有没有办法在保持getSetting()非静态抽象类的同时调用SettingsItem?这是我尝试添加到SingleChoiceViewHolder.java的内容,但是Android Studio说'SettingsItem'是抽象的;无法实例化。:

SettingsItem instance = new SettingsItem();
instance.getSetting();

IntSetting setting = (IntSetting) getSetting();
mTextSettingDescription.setText(setting.getValue());

我也尝试过尝试将SettingsItem转换为界面并将其与SingleChoiceViewHolder extends SettingViewHolder一起实现,但原始问题仍然存在。

文件附在下面。

SingleChoiceViewHolder.java:

public final class SingleChoiceViewHolder extends SettingViewHolder
{
	private SingleChoiceSetting mItem;

	private TextView mTextSettingName;
	private TextView mTextSettingDescription;

	public SingleChoiceViewHolder(View itemView, SettingsAdapter adapter)
	{
		super(itemView, adapter);
	}

	@Override
	protected void findViews(View root)
	{
		mTextSettingName = (TextView) root.findViewById(R.id.text_setting_name);
		mTextSettingDescription = (TextView) root.findViewById(R.id.text_setting_description);
	}

	@Override
	public void bind(SettingsItem item)
	{
		mItem = (SingleChoiceSetting) item;

		mTextSettingName.setText(item.getNameId());

		if (item.getDescriptionId() == R.string.dynamic_descriptionId)
		{
			SettingsItem instance = new SettingsItem();
			instance.getSetting();

			IntSetting setting = (IntSetting) getSetting();
			mTextSettingDescription.setText(setting.getValue());
		}

		if (item.getDescriptionId() > 0 && item.getDescriptionId() != R.string.dynamic_descriptionId)
		{
			mTextSettingDescription.setText(item.getDescriptionId());
		}
	}

	@Override
	public void onClick(View clicked)
	{
		getAdapter().onSingleChoiceClick(mItem);
	}

SettingsItem.java:

public abstract class SettingsItem
{
	public static final int TYPE_HEADER = 0;
	public static final int TYPE_CHECKBOX = 1;
	public static final int TYPE_SINGLE_CHOICE = 2;
	public static final int TYPE_SLIDER = 3;
	public static final int TYPE_SUBMENU = 4;
	public static final int TYPE_INPUT_BINDING = 5;
	public static final int TYPE_RADIO_BUTTON = 6;

	private String mKey;
	private String mSection;
	private int mFile;

	private Setting mSetting;

	private int mNameId;
	private int mDescriptionId;

	public SettingsItem(String key, String section, int file, Setting setting, int nameId, int descriptionId)
	{
		mKey = key;
		mSection = section;
		mFile = file;
		mSetting = setting;
		mNameId = nameId;
		mDescriptionId = descriptionId;
	}

	public String getKey()
	{
		return mKey;
	}

	public String getSection()
	{
		return mSection;
	}

	public int getFile()
	{
		return mFile;
	}

	public Setting getSetting()
	{
		return mSetting;
	}

	public void setSetting(Setting setting)
	{
		mSetting = setting;
	}

	public int getNameId()
	{
		return mNameId;
	}

	public int getDescriptionId()
	{
		return mDescriptionId;
	}

	public abstract int getType();
}

2 个答案:

答案 0 :(得分:0)

根据定义,抽象类意味着它没有实例化,但您可以从中继承。如果要创建具有相同值但名称不同的许多不同对象,则只需扩展SettingsItem。

此外,如果您想要更多抽象以供将来使用,您可以使用与抽象方法相同的方法创建一个接口,以防您需要为不同的设置项制作自定义方法。

示例:

interface SettingsInterface {
    void doSomething();
}

class abstract SettingsItem implements SettingsInterface {
    public void doSomething() {
        System.out.println("Hello");
    }
}

class RegularSettings extends SettingsItem {}

class CustomSettings implements SettingsInterface {
    public void doSomething() {
        System.out.println("Goodbye");
    }
}

class TestClass {

    public static void testAbstract(SettingsItem extendedAbstract) {
        extendedAbstract.doSomething();
    }

    public static void testInterface(SettingsInterface interface) {
        interface.doSomething();
    }

    public static void main(String[] args) {
        SettingsItem abstractExtended = new RegularSettings();
        // also could be CustomSettings instead of SettingsInterface
        SettingsInterface customClass = new CustomSettings();

        testInterface(abstractExtended);
        testInterface(customClass);

        testAbstract(abstractExtended);
        // will throw errors since it doesn't extend SettingsItem
        testAbstract(customClass);
    }
}

答案 1 :(得分:0)

由于getSetting()不是静态方法,因此需要在扩展抽象类SettingsItem的某个具体类的实例上调用它。

想一想。如果你有这样一个类的两个实例,并且两个实例的mSetting变量不同,那么应该从类似静态的调用返回到getSetting()?