Anh ơi, có nên code inner class :
class A { class Dog { private String name; private String type; } }
Như vậy, việc sử dụng inner class đã phá vỡ quy tắc Single responsibility trong Solid, vậy khi nào dùng inner class là hợp lý nhất ạ
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Việc sử dụng inner class không hề phá vỡ quy tắc Single responsibility trong Solid bởi vì quy tắc này nói rằng: mỗi lớp mỗi hàm nên thực hiện 1 chức năng, như vậy việc em khai báo 2 lớp ở cùng 1 nơi nhưng chúng vẫn thực hiện đúng chức năng của mình thì vẫn ok.
Inner class nên dùng khi các lớp inner này có liên quan mật thiết và bổ trợ cho lớp public, thường sẽ là các lớp private.
Ví dụ: