Message reminder function implemented by Java programming
Abstract: With the popularity of the Internet and the use of mobile devices, people's demand for real-time messages is getting higher and higher. This article will implement a simple message reminder function through Java programming and demonstrate how to implement it through code examples.
2.1 Create a message class
First, create a class named Message, which contains the title, content and time attributes of the message. The code is as follows:
public class Message { private String title; private String content; private Date time; // 构造方法 public Message(String title, String content, Date time) { this.title = title; this.content = content; this.time = time; } // getter和setter方法 // ... }
2.2 Create a message reminder class
Next, create a class named Notification, which is responsible for sending message reminders. The code is as follows:
import java.util.List; public class Notification { private List<Message> messages; // 构造方法 public Notification() { this.messages = new ArrayList<>(); } // 添加消息 public void addMessage(Message message) { messages.add(message); } // 获取未读消息数量 public int getUnreadCount() { int count = 0; for (Message message : messages) { if (!message.isRead()) { count++; } } return count; } // 获取最近的一条消息 public Message getLatestMessage() { // 省略实现 } // 其他方法 // ... }
2.3 Test the message reminder function
Finally, create a class named Main to test the message reminder function. The code is as follows:
public class Main { public static void main(String[] args) { Notification notification = new Notification(); // 添加两条消息 Message message1 = new Message("消息1", "这是消息1的内容", new Date()); Message message2 = new Message("消息2", "这是消息2的内容", new Date()); notification.addMessage(message1); notification.addMessage(message2); // 输出未读消息数量 System.out.println("未读消息数量:" + notification.getUnreadCount()); // 输出最近的一条消息 Message latestMessage = notification.getLatestMessage(); System.out.println("最近的一条消息:" + latestMessage.getTitle() + " - " + latestMessage.getContent()); } }
The above is a message reminder implemented using Java programming Functional example. Through this example, we can understand how to use the object-oriented features of the Java language to implement real-time message push. I hope readers can benefit from it and have a deeper understanding of Java programming.
The above is the detailed content of Message reminder function implemented by Java programming. For more information, please follow other related articles on the PHP Chinese website!