Java面向对象短信完整代码
时间: 2023-09-24 08:11:08 浏览: 52
以下是Java面向对象短信的完整代码示例:
```
// 定义短信类
public class Message {
private String sender; // 发件人
private String receiver; // 收件人
private String content; // 短信内容
// 构造方法
public Message(String sender, String receiver, String content) {
this.sender = sender;
this.receiver = receiver;
this.content = content;
}
// 发送短信方法
public void send() {
System.out.println("发送短信给 " + receiver + ",内容为:" + content);
}
// 获取发件人
public String getSender() {
return sender;
}
// 设置发件人
public void setSender(String sender) {
this.sender = sender;
}
// 获取收件人
public String getReceiver() {
return receiver;
}
// 设置收件人
public void setReceiver(String receiver) {
this.receiver = receiver;
}
// 获取短信内容
public String getContent() {
return content;
}
// 设置短信内容
public void setContent(String content) {
this.content = content;
}
}
// 测试短信类
public class TestMessage {
public static void main(String[] args) {
// 创建短信对象
Message message = new Message("张三", "李四", "你好,我是张三。");
// 发送短信
message.send();
// 获取发件人和收件人
System.out.println("发件人:" + message.getSender());
System.out.println("收件人:" + message.getReceiver());
// 修改短信内容
message.setContent("你好,我是张三,你收到我的短信了吗?");
// 重新发送短信
message.send();
}
}
```
以上代码定义了一个短信类Message,包含了发件人、收件人和短信内容三个属性,并提供了发送短信、获取和设置发件人、收件人、短信内容等方法。同时,还提供了一个测试类TestMessage,用于测试短信类的各个方法。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)