2013-05-08 44 views
-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); 
    } 
} 

我得到的錯誤是與第二和第三類,它說:「從[[email protected]]收到的TEXT:什麼ru做?>但是:< ...從[辛迪的文本電話]:什麼ru做?>「。有誰知道我該如何解決這個問題?我在測試中遇到了一些錯誤,但我無法弄清楚我做錯了什麼?

+0

調用sender.toString(),而不是僅僅sender你比較默認的'用你的手創造輸出toString'輸出,儘管你實際上沒有包含測試,所以這是一個猜測。閱讀你的代碼也是不可能的。如果您打算使用實際的Javadoc而不是將其嵌入到方法中,則要使用實際的Javadoc。 – 2013-05-08 17:09:17

+0

那麼你期望什麼?爲什麼它會輸出你所期望的內容? – ApproachingDarknessFish 2013-05-08 17:26:17

回答

2

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

2

每當您看到像這樣的打印:[[email protected]]意味着您正在打印實例ID(在這種情況下是發件人)。

你需要重寫該類中的toString()方法和

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

+0

我明白了,謝謝! – 2013-05-08 17:13:21

相關問題