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()是私有的调不到

posted @ 2019-05-10 10:16  贩卖长江水  阅读(8700)  评论(0编辑  收藏  举报