简体   繁体   English

Java中Bean类的继承

[英]Inheritance for Bean class in Java

I am working on Jsf2 project with primefaces. 我正在使用Primefaces开发Jsf2项目。 I have used some thing like this: Bean class: 我用过这样的东西:Bean类:

Student Its the main Bean class with id and name. 学生它是具有ID和名称的主要Bean类。 I have created two more bean class. 我又创建了两个bean类。 Technical student and non technical student both these are extending student bean class. 技术学生和非技术学生都在扩展学生豆类。 My question is am I doing Right? 我的问题是我在做对吗? Is it right design ? 这是正确的设计吗? I want to keep id and name common which is reusable across technical student and nontechnical students page 我想保持ID和名称的通用性,从而可在技术学生和非技术学生页面之间重复使用

class student{
        id, name
        getter and setter
}

class Technicalstudent extends student{
        technicaldetails, 
        get and set
}

class NonTechnicalstudent extends student{
        nontechnicaldetails
        get and set
}

Yes your design is correct from inheritance perspective. 是的,从继承角度来看,您的设计是正确的。 Subclass/child need to inherit the common properties from parents and they can have their own properties. 子类/子级需要从父级继承公共属性,并且它们可以具有自己的属性。

I found nothing wrong with the code based on what you want to achieve. 我发现基于您要实现的代码没有错。 What you did here is called is-a relationship . 您在这里所做的称为is-关系 Meaning a TechnicalStudent and a NonTechnicalstudent is also a Student. 意味着技术学生和非技术学生也是学生。 Therefore it will inherit every fields from Student. 因此,它将继承Student的每个字段。 Hope that helps. 希望能有所帮助。

Your design is correct but As on Ui you have to map your enity with backend object. 您的设计是正确的,但在Ui上,您必须将实体与后端对象进行映射。 Map object with Student object and take aboolean is student object like Boolean isTechnicalStudent and based on the option set it true and false . 将带有Student对象并带aboolean的对象映射为像Boolean isTechnicalStudent类的学生对象,并基于将其设置为truefalse的选项。

On UI end toggle your form of technical and non-technical student details and now due to this boolean you always have a knowledge that wether it is technical or non technical so you can easily type cast. 在UI端,切换您的技术和非技术学生详细信息形式,现在由于使用此布尔值,您始终了解是技术性还是非技术性的,因此您可以轻松地键入强制转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM