-2
public class CellPhone {

    //REMINDER: protected fields can be accessed directly by any
    //          class which extends this one
    protected String ownerName;




    public CellPhone(String ownerNameIn) {
        //initialize ownerName as ownerNameIn
ownerName = ownerNameIn;

    }

    public String receiveCall(CellPhone sender) {
        //return a String of the form:
        //    receiver's name " is receiving a call from " sender's name
        //you can implement this by using the receiver to invoke receiveCall
        //  while passing in the current phone

        String receiveCall = sender.ownerName + " is receiving a call from " + ownerName;
        return receiveCall;

    }

    public String call(CellPhone receiver) {
        //return a String by using the receiver to invoke receiveCall 
        //  while passing in the current phone

        return this.receiveCall(receiver);


    }
}


package cellPhones;

public class TextMessagingPhone extends CellPhone {
    //number of messages owner can send and receive
    //REMINDER: private fields can't be accessed by class which extends this one
    private int availMessages;

    public TextMessagingPhone(String owner) {
        //Initialize ownerName as owner and availMessage as 15 by invoking the
        //   two-parameter constructor of this class.


this(owner,15);
    }

    public TextMessagingPhone(String owner, int messageLimit) {
        //initialize ownerName as owner and availMessage as messageLimit
        //part of this will require invoking the superclass constructor
        // and then setting the new instance variable
super(owner);
availMessages = messageLimit; 





    }

    public String receiveText(TextMessagingPhone sender, String message) {
        //The owner receives message from sender.

        //decrease the number of messages available to receive

        //return a String of the form:
        //    owner's name " has received TEXT from " sender's name ":" message
availMessages --;
        String receivedText = ownerName + " has received TEXT from " + sender + ":" + message;

        return receivedText;


    }

    public String sendText(TextMessagingPhone receiver, String message) {
        //decrease the number of messages available to send

        //return a String by using the receiver to invoke receiveText 
        //  while passing in the current phone and the message
availMessages --; 
          String invokingReceiveText = receiver.receiveText(receiver, message);

            return invokingReceiveText;
    }
}

package cellPhones;

public class SmartPhone extends TextMessagingPhone {

    public SmartPhone(String ownerIn) {
        //Invoke the super class' copy constructor and send it owner

        //NOTE: There's nothing else to do since SmartPhone adds no
        //      new fields.
        super(ownerIn);

    }



    public String displayPicture(String pictureSubject) {
        //return a String of the form:
        //    owner's name " now displaying picture of " pictureSubject


        String picture = ownerName + " now displaying picture of " + pictureSubject;
        return picture;
    }


    /* 
     * This method OVERRIDES the inherited receiveCall method.
     * Smartphones "display" a photo of the caller.
     */
    public String receiveCall(CellPhone sender) {
        //return a String built from:
        //    the result of calling displayPicture with the sender's owner's name 
        //    concatenated with a dash and then concatenated with the
        //    result of invoking the superclass' receiveCall with the sender
String call = this.displayPicture(ownerName) + "-" + sender.ownerName;
return call;

    }



    public String receivePictureAndTextMessage(
            SmartPhone sender, String messageText, String picDescription) {
        //owner receives messageText from sender with picDescription

        //return a String built from:
        //    the result of calling displayPicture with the picDescription 
        //    concatenated with a dash and then concatenated with the
        //    result of invoking the receiveText method with the sender
        //    and the messageText

        String picText = this.displayPicture(picDescription) + "-" + this.receiveText(sender,messageText);
        return picText;
    }



    public String sendPictureAndTextMessage(
            SmartPhone receiver, String messageText, String picDescription) {
        //owner sends messageText to receiver with picDescription

        //return a String built by having the receiver invoke the
        //   receivePictureAndTextMessage method, sending in the 
        //   current phone, the messageText, and the picture description

        return receiver.receivePictureAndTextMessage(receiver, messageText, picDescription);
    }
}

我得到的错误是第二类和第三类,它说“从 [cellPhones.TextMessagingPhone@842e3b] 收到 TEXT:你在做什么?> 但是:<...从 [Cindy 的短信电话] 收到 TEXT:What ru在做什么?>”。有谁知道我该如何解决这个问题?

4

2 回答 2

2

您需要打印TextMessagingPhone.ownerName,而不是TextMessagingPhone对象本身。或者您可以覆盖该toString()方法以返回ownerName.

于 2013-05-08T17:08:19.313 回答
2

每次看到这样的打印: [cellPhones.TextMessagingPhone@842e3b] 表示您正在打印实例 ID(在这种情况下为发送者)。

您需要覆盖该类中的 toString() 方法并调用而sender.toString()不是仅调用sender

String receivedText = ownerName + " has received TEXT from " + sender + ":" + message;

于 2013-05-08T17:08:53.960 回答