我怎么知道使用构造函数解决

时间:2019-10-18 23:58:30

标签: java arrays inheritance

修改以下类以使用两种不同方式打印“我的应用程序”:
-使用构造函数。
-使用方法替代。

CHARFORMAT cf = { 0 };
int txtLen =  GetTextLength();

cf.cbSize = sizeof(cf);
cf.dwMask = CFM_COLOR;
cf.dwEffects = ~CFE_AUTOCOLOR;//No auto color
cf.crTextColor = RGB(0, 0, 255);//color of the text

SetSel(txtLen, -1);//Deselect all
SetSelectionCharFormat(cf);//Assign the format to the cursor pos
ReplaceSel(newText);

2 个答案:

答案 0 :(得分:1)

构造函数无法返回值。因此,只需将构造函数添加到MyApp类中即可。

abstract class Application {
    protected Application() {
        System.out.println("default Constructor");
    }
    //constructer only can be called by child class using super keyword
    protected  Application(String appName) {
        System.out.println(appName);
    }
    public abstract String getName();
}

class DefaultApplication extends Application {
    public static final String NAME = "defapp";

    public DefaultApplication() {
        super("abstract contructor : " + NAME);
        System.out.println(NAME);
    }

    @Override
    public String getName() {
        return NAME;
    }
}

class MyApp extends DefaultApplication {
    public static final String NAME = "myapp";

    public MyApp() {
        System.out.println(NAME);
    }
}

答案 1 :(得分:0)

abstract class Application{
    public abstract String getName();
}

class DefaultApplication extends Application
{
    public static final String NAME = "defapp";

    @Override
    public String getName()
    {
        return NAME;
    }
}

class MyApp extends DefaultApplication
{
    public static final String NAME = "myapp";

    public MyApp()
    {
        System.out.println(NAME);
    }

    @Override
    public String getName()
    {
        return NAME;
    }
}

public class Applicationtest
{
    public static void main(String[] args)
    {
        Application myApp = new MyApp();
        System.out.println(myApp.getName());
    }
}