【问题标题】:please help me with self-reference pointer请帮助我使用自引用指针
【发布时间】:2012-11-06 06:28:05
【问题描述】:

我写了两个类,第一个是会员,第二个是商店。我编写了一个可以从成员类创建对象的方法,我试图在成员类中编写一个 Store 类型的字段存储,我希望它存储对成员输入的存储的引用。 有人告诉我这样做: memberRegister() 需要作为参数传递一个指向您当前所在的 Store 对象的指针。

其实Store对象需要能够告诉Member对象“指向我”。也就是说,Store 对象需要一个指向自身的指针。 但我没有得到它

这是成员类

private int pinNumber;
private String name, id; 
private Store store;


/**
 * Constructor for objects of class Member
 */
public Member(String name, String id, int pinNumber, Store store)

{
    // initialise instance variables
    this.name = name;
    this.id = id;
    this.pinNumber = pinNumber;
    checkId();
    checkPinNumber();
}

/**
 * An example of a method - replace this comment with your own
 * 
 * @param  y   a sample parameter for a method
 * @return     the sum of x and y 
 */
private void checkId()
{
    // put your code here
    int length; 
     length = id.length();
    if (length > 10 ){
        System.out.println("lentgh must be at 10 ");
    }
}
private void checkPinNumber()
{
    int length; 
     length = id.length();
    if ((length > 4) && (length < 4 )){
        System.out.println("lentgh must be at 4"); 
}

类商店

   private String storeName;
private int total; 
private  Member member;


/**
 * Constructor for objects of class Store
 */
public Store(String storeName, int total)
{
    // initialise instance variables
    this.storeName = storeName;
    this.total = total;
}

/**
 * 
 */
public String getStoreName() 
{
return storeName;
}

/**
 * An example of a method - replace this comment with your own
 * 
 * @param  y   a sample parameter for a method
 * @return     the sum of x and y 
 */
public Member memberRegister(String name, String id, int pinNumber)
{
    // put your code here
   Member member;
   member = new Member(name, id, pinNumber)
   return member;
}

【问题讨论】:

  • this 不够吗? IE。 return new Member(name, id, pinNumber, this);?
  • @Kerrek 如果构造函数将存储存储在它的私有成员变量中,它会这样做,但它不会这样做。

标签: java pointers parameters parameter-passing bluej


【解决方案1】:

使用关键字this 是获得自引用指针的方法。您应该能够按照 @Kerrek SB 的建议和 return new Member(name, id, pinNumber, this)memberRegister 方法内部进行操作。

【讨论】:

    【解决方案2】:

    在您的情况下,将 this 关键字传递给方法 memberRegisteruseless

    returningthis关键字是useful

    了解更多关于这个关键字check this

    【讨论】:

      【解决方案3】:

      您的 memberRegister 方法没有正确调用您的 Member 构造函数:

      public Member memberRegister(String name, String id, int pinNumber)
      {
          // put your code here
         Member member;
         member = new Member(name, id, pinNumber, this) //this passes in a reference to your store
         return member;
      }
      

      然后在 Member 构造函数中分配引用:

      public Member(String name, String id, int pinNumber, Store store)
      
      {
          // initialise instance variables
          this.name = name;
          this.id = id;
          this.store = store //where this.store is a Store
          this.pinNumber = pinNumber;
          checkId();
          checkPinNumber();
      }
      

      希望对您有所帮助。顺便说一句,更新 cmets,使其与您的代码匹配。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-12-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多