对象初始化过程

时间:2014-05-27 15:23:11

标签: java

这是我的班级。

public class MyFirstClass
{ 

    private FirstInterface firstInterface;



    public FirstInterface getFirstInterface() {
        return firstInterface;
    }

    public void setFirstInterface(FirstInterface firstInterface) {
        this.firstInterface = firstInterface;


    aMethod()
    {
    firstInterface.getMyFunction();
    System.out.println(firstInterface); // prints some object value here.
    {

}

界面:

public abstract interface FirstInterface 
{
  public abstract getMyFunction();
}

实施类:

public class MyImplClass  implements MyFirstClass
{
}

我在这里找不到构造函数或任何其他初始化。有人请告诉我firstInterface如何初始化的不同方式?

3 个答案:

答案 0 :(得分:1)

请参阅初始化类的任何属性(引用或原语),我们始终使用constrcutor或setter方法。

在你的情况下,它可能就像下面

1)constrcutor

public class MyFirstClass
{ 

    private FirstInterface firstInterface;

//other stuff goes here
MyFirstClass(FirstInterface firstInterface)
{
this.firstInterface = firstInterface;

}

}

在某处创建MyFirstClass的对象时,你必须像下面那样调用

MyFirstClass m = new  MyFirstClass(new MyImplClass());

2)setter方法

public class MyFirstClass
{ 

    private FirstInterface firstInterface;



    public FirstInterface getFirstInterface() {
        return firstInterface;
    }

    public void setFirstInterface(FirstInterface firstInterface) {
        this.firstInterface = firstInterface;

}

并初始化,如下所示

MyFirstClass m = new MyFirstClass();
m.setFirstInterface(new MyImplClass());

答案 1 :(得分:1)

让我一步一步向你解释

  • 首先,您无法通过概念从界面创建新实例。
  • 其次,你没有制作任何构造函数
  • 第三,aMethod不是方法的正确声明
  • 第四,班级MyImplClass应该具有界面FirstInterface
  • 中所有功能的实现

所以,让我们看看正确的代码:

我在这里修复了aMethod的实现,并添加了构造函数。另外,我想您想在aMethod中执行的操作是打印getMyFunction

的结果
public class MyFirstClass
{ 

    private FirstInterface firstInterface;

    // Constructor
    public MyFirstClass(FirstInterface firstInterface)
    {
        this.firstInterface = firstInterface;
    }

    public FirstInterface getFirstInterface() 
    {
        return firstInterface;
    }

    public void setFirstInterface(FirstInterface firstInterface) 
    {
        this.firstInterface = firstInterface;
    }

    public void aMethod()
    {
        System.out.println(firstInterface.getMyFunction()); 
    {

}

接口很好,但默认情况下,接口中的任何方法都声明为abstractpublic,因此无需添加它们。你忘了定义方法的返回类型,我会假设它返回String
我还在单词abstract

之前删除了interface
public interface FirstInterface 
{
    String getMyFunction();
}

这里我添加了未实现的方法(我确信你看到了前面代码的错误) 此外,我还将implements MyFirstClass替换为implements FirstInterface

public class MyImplClass implements FirstInterface
{
    public String getMyFunction()
    {
        String res = "";
        // code
        return res;
    }
}

最后如何调用所有这些

public class Test
{
    public static void main(String[] args)
    {
        FirstInterface f = new MyImplClass();
        MyFirstClass ob = new MyFirstClass(f);
        ob.aMethod();
    }
}

所以这是一个完整的例子,说明你犯的错误及其更正,我希望这可以给你一个良好的开端。

答案 2 :(得分:0)

aMethod似乎根本不正确。编译错误。

我认为你的其他任何代码都不会编译。

您不需要在界面上使用抽象。

public interface FirstInterface 
{
  void getMyFunction();
}

我没看到MyImplClass的用途。为什么要扩展MyFirstClass?

我想知道为什么其中一个类没有实现接口。

public class MyFirstClass implements FirstInterface
{ 
    private FirstInterface firstInterface;

    public MyFirstClass(FirstInterface f) {
        if (f == null) throw new IllegalArgumentException("first interface cannot be null");
        this.firstInterface = f;
    }

    public void getMyFunction() { this.firstInterface.getMyFunction(); }
}
相关问题