如何为 Struts MVC 创建这个模型?

发布于 2024-10-24 22:14:38 字数 528 浏览 1 评论 0原文

我有一个大学管理门户。我必须创建以下类来描述模型。

class Address
{
    String street;
    String city;
    
}

class Contact
{
   String phone;
   String mobile;
}

abstract class Person
{
   String name;
   String age;
   Address address;
   Contact contact;
}

class Student extends Person
{
   String course;
   String stream;
   String rollno;
}

class Faculty extends Person 
{
   String department;
   String faculty id;
}

现在我应该使用 getter-setter 方法进行实例初始化或构造函数吗?

Person 类中的聚合怎么样?

构造函数应该如何在那里工作?

I have a college management portal. I have to create the following classes for depicting the model.

class Address
{
    String street;
    String city;
    
}

class Contact
{
   String phone;
   String mobile;
}

abstract class Person
{
   String name;
   String age;
   Address address;
   Contact contact;
}

class Student extends Person
{
   String course;
   String stream;
   String rollno;
}

class Faculty extends Person 
{
   String department;
   String faculty id;
}

Now should i use the getter-setter methods for instance initialisation or constructors?

What about the aggregation in class Person?

How should the constructor work there?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼前雾蒙蒙 2024-10-31 22:14:38

是的,我认为你应该有 getter &模型对象的 setter 方法。您的类的属性应该设为私有。
构造函数用于允许为对象构造传递强制信息,没有这些信息就无法构建对象(或者对象是无用的)。就像你无法创造一个没有名字和名字的人一样。年龄。在你的情况下,地址和联系方式很多是可选的。因此,您可以使用 mutator 方法设置地址/联系信息。

正如 Srinivas 提到的,构造函数并不是 mutator 方法的替代品。
构造函数的目的是为对象构造提供强制性的东西。就像没有原材料就无法建造建筑物一样。

Yes I think you should have getter & setter methods for the model objects. Your attributes of the class should be made private.
The constructors are used to allow the mandatory information to be passed for object construction without which the object cannot be build (or the object is useless). Its like you cant create a person without name & age. In your case the address & contact many be optional. So you can set the address/contact information using mutator methods.

As Srinivas mentioned the constructors are not the alternatives for mutator method.
The purpose of constructor is make mandatory things available for object construction. Its like without raw material you cant build a building.

世俗缘 2024-10-31 22:14:38

对于这些东西,你需要“beans”=私有成员变量,每个变量都有 getter 和 setter。根据您的需要,您可以创建一个构造函数,该构造函数在实例化时在对象中设置某些参数,但也可以不直接通过设置器设置(因为某些设置器可能以某种方式对设置值进行操作,因此最好不要直接搞乱像这样的东西。学生=“名字”)。

对于教员案例,我不明白为什么它会扩展 Person,而且我认为它不应该扩展。正如您所说,这些模型应该反映数据库中表的结构,因此它们最多都会扩展一个抽象类或实现某个接口,但彼此之间不会扩展。

对于人员聚合,我也认为联系人和地址应该是整数,因为它们代表另一个表中的外键,并考虑多对多关系的情况。

模型首先是数据库在应用程序中的反映。

For this stuff these you need "beans" = private member variables with getters and setters for each. Depending on your needs you could create a constructor that has certain parameters to be set in the object at instantiation but also through the setters not directly (as certain setters might operate on the set value in a certain way so it is best that you do not mess up directly with stuff like this.student = "name").

For the faculty case I do not see why it extends Person and I do not think it should. As you say these are models they should reflect structures of tables in a database so at most each of them would extend an abstract class or implement a certain interface but not each other.

For the person aggregation also I believe that Contact And Address should be Integers as they represent a Foreign Key in another table and think about the case of many-to-many relation.

Models are first of all a reflection of the database in the application.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文