public和private區別


public和private區別

1、public:聲明公共類,公共類其他類可以調用 (其它類中也可以調用

2、private:聲明私有類,私有類自己的類可以使用(只能本類之中使用),其它類不可使用。

 例如:

1.service層:


@Service
1 public class UserService{ 2 @Override//私有的userId 3 private Object userId(int id){ 4 return null; 5 } 6 @Override//公有的username 7 public Object username(String name){ 8 return null; 9 } 10 @Override 11 public Object userAge(int age){ 12 return userId(int id); 13 } 14 }
userAge中可以使用userId因為userId和userAge在一個類中

2.controller

1 public class UserController{
2 @Autowired
3     private UserService userService;
4     
5     @PostMapping("/userId")
6    public Object userId(String name){
7         return userService.username(name);
8    }
9 }

在Controller中userService.userId無法點到,因為private Object userId()是私有的調不到


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM