protected string name; 为啥用protected?
时间: 2024-02-16 12:00:11 浏览: 25
查询结果保存到 request 中,并转发到 class_info.jsp 页面进行显示。
9. 成绩查询功能实现
成绩查询功能需要查询数据库中的成绩信息,并显示在页面上。下面是一个简单的 ScoreServlet 实现:
在C#中,protected是一种访问修饰符,用于限定成员的访问权限。被protected修```java
package com.example.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet饰的成员只能在该类的派生类中被访问,而不能在类的外部被访问。
;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.example.bean.Score;
import com.example在这个例子中,name字段被声明为protected,意味着它只能在Student类的派生类中访.bean.User;
import com.example.dao.ScoreDao;
@WebServlet("/ScoreServlet")
public class ScoreServlet extends HttpServlet {
private static final long serialVersionUID =问。这通常用于实现继承关系中的数据隐藏和封装。派生类可以继承基类 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
User user = (User) request.getSession().的成员,但是不能直接访问基类中被声明为private的成员。如果我们希望派生类getAttribute("user");
ScoreDao scoreDao = new ScoreDao();
List<Score> scoreList = null;
if (user.getType().equals("teacher")) {
scoreList = scoreDao.getAllScore();
} else {
scoreList = scoreDao.get能够访问基类中的成员,但是又不希望外部程序直接访问这些成员,ScoreByUserId(user.getId());
}
request.setAttribute("scoreList", scoreList);
request.getRequestDispatcher("score.jsp").forward(request那么就可以将这些成员声明为protected。
在这个例子中,name字段被声明为protected,可能是, response);
}
}
```
在 ScoreServlet 中,根据用户的权限查询数据库中的成绩信息,然后将因为它是一个基类中的字段,希望派生类能够访问并修改它,但是不希望外部程序直接访问它。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)