如何为 Struts MVC 创建这个模型?
我有一个大学管理门户。我必须创建以下类来描述模型。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,我认为你应该有 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.
对于这些东西,你需要“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.