英文:
How the inheritance of Java should be?
问题
目前我有管理员类、用户类和讲师类。然后我希要让用户类和讲师类从用户类继承。但问题是我的用户类构造函数涉及用户名、密码,而管理员和讲师类的构造函数我将其与他们的个人资料放在一起,例如他们的姓名、年龄、性别,所有这些东西。因此,讲师和管理员与用户类不同。所以在这方面我做错了吗?
英文:
Currently I have admin class, user class and lecturer class. Then I want user and lecturer inherit from user. But the problem is my user class constructor is about the username, password, but the admin and lecturer class constructor I put it with the their profile such as their name, age, gender all those things? Thus, lecture and admin are different with the user class. So is I have something wrong in this?
答案1
得分: 1
如果您想将类Admin(管理员)和Lecturer(讲师)定义为User(用户),那么可以让类Admin和Lecturer继承自User。但是,如果Admin类和Lecturer类不应该依赖于“username, password”,那么从User类继承就没有意义。这里有一种替代方案:
public interface User {
}
public class UserImpl implements User {
private final String username;
private final String password;
public UserImpl(String username, String password) {
this.username = username;
this.password = password;
}
}
public class Admin extends UserGroupAdmin implements User {
protected Admin(String name, int age, String gender) {
super(name, age, gender);
}
}
public class Lecturer extends UserGroupAdmin implements User {
protected Lecturer(String name, int age, String gender) {
super(name, age, gender);
}
}
/**为共同部分创建一个额外的抽象类:**/
public abstract class UserGroupAdmin {
private final String name;
private final int age;
private final String gender;
protected UserGroupAdmin(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getGender() {
return gender;
}
}
在涉及何时继承和何时不继承的情况下,可以阅读有关“Liskov替换原则”的额外资料:
“Liskov Substitution Principle”:
https://stackoverflow.com/questions/56860/what-is-an-example-of-the-liskov-substitution-principle
英文:
If you want define class Admin (admin) and Lecturer (lecturer) as User (user), then you can let class Admin and Lecturer inherit from User. But if class Admin and Lecture are not suppose to depend on "username, password", then there is no point in extend from User class. Here is an alternative:
public interface User {
}
public class UserImpl implements User {
private final String username;
private final String password;
public UserImpl(String username, String password) {
this.username = username;
this.password = password;
}
}
public class Admin extends UserGroupAdmin implements User {
protected Admin(String name, int age, String gender) {
super(name, age, gender);
}
}
public class Lecturer extends UserGroupAdmin implements User {
protected Lecturer(String name, int age, String gender) {
super(name, age, gender);
}
}
Make an extra abstract class for common parts:
public abstract class UserGroupAdmin {
private final String name;
private final int age;
private final String gender;
protected UserGroupAdmin(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getGender() {
return gender;
}
}
Extra reading when it comes to when and when not to inherit: "Liskov Substitution Principle":
https://stackoverflow.com/questions/56860/what-is-an-example-of-the-liskov-substitution-principle
答案2
得分: 0
有多种方法可以实现您的目标。以下是其中一种:
// 基类
abstract class User {
User(String name, int age, ...) {
}
}
class AuthenticatedUser extends User {
AuthenticatedUser(String name, int age..., String username...) {
super(name, age, ...);
// 接下来进行身份验证设置
}
}
class SystemUser extends User {
// 等等等等
}
英文:
There is more than one way you can accomplish your goal. Here's one :
//base class
abstract class User {
User(String name, int age, ...) {
}
}
class AuthenticatedUser extends User {
AuthenticatedUser(String name, int age..., String username...) {
super(name, age, ...);
//authentication setup next
}
}
class SystemUser extends User {
//etc etc
}
答案3
得分: 0
我猜你想要将用户属性继承到管理员和讲师类中。
所以你需要在这里扩展基类用户 Base class user。
public class User {
private String username;
private String password;
public User(){
}
}
class Admin extends User{
private String name;
private Integer age;
private String gender;
public Admin(){
// 构造函数代码在这里
}
}
class Lecturer extends User{
private String name;
private Integer age;
private String gender;
public Lecturer(){
// 构造函数代码在这里
}
}
英文:
I Guess you want to inherit the user properties to both admin and lecturer class.
So you need to extend the Base class user here.
public class User {
private String username;
private String password;
public User(){
}
}
class Admin extends User{
private String name;
private Integer age;
private String gender;
public Admin(){
// Constructor code here
}
}
class Lecturer extends User{
private String name;
private Integer age;
private String gender;
public Lecturer(){
// Constructor code here
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论