【问题标题】:The object initialization process [closed]对象初始化过程[关闭]
【发布时间】:2014-05-27 15:23:10
【问题描述】:

这是我的班级的样子。

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 是如何初始化的?

【问题讨论】:

  • 您的问题不清楚。你的界面是否应该被称为FirstInterface?您是在询问MyFirstClass 中的firstInterface 字段吗?
  • 我也不懂你的问题,你能用简单的英语告诉我们你真正想要做什么吗?
  • MyFirstClass 中似乎缺少一些代码(在this.firstInterface = firstInterface;aMethod() 之间)
  • 是的。我试图打印firstInterface,它打印了一些对象值。这个对象是从哪里分配的?
  • @KonstantinNaryshkin:这是一个错字。编辑了问题..

标签: java


【解决方案1】:

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

在你的情况下,它可能如下所示

1)构造器

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());

【讨论】:

    【解决方案2】:

    让我一步一步给你解释

    • 首先,您不能通过概念从界面创建新实例。
    • 第二,你没有做任何构造函数
    • 第三,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
    我还删除了interface这个词之前的abstract

    public interface FirstInterface 
    {
        String getMyFunction();
    }
    

    在这里我添加了未实现的方法(我相信您在之前的代码中看到了该错误)。
    另外在这里我用implements FirstInterface替换了implements MyFirstClass

    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();
        }
    }
    

    所以这是一个完整的示例,其中包含您所犯的错误及其更正,我希望这可以给您一个良好的开端。

    【讨论】:

      【解决方案3】:

      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(); }
      }
      

      【讨论】:

      • 是的,他问如何初始化界面。我给他看了。例外是按合同编程:明确它不能为空。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-10
      • 2014-11-23
      • 2013-06-27
      • 2013-07-06
      • 1970-01-01
      相关资源
      最近更新 更多