如何在Java中实现使用依赖、枚举数据类型?实现聚合、合成的混乱
我必须在java代码中实现下面的类图。这个图表非常复杂,有些部分会造成混乱。这个问题肯定会对我和其他读者有很大帮助,因为它包含了UML图的几个重要方面如何在Java中实现使用依赖、枚举数据类型?实现聚合、合成的混乱,java,oop,uml,aggregation,class-diagram,Java,Oop,Uml,Aggregation,Class Diagram,我必须在java代码中实现下面的类图。这个图表非常复杂,有些部分会造成混乱。这个问题肯定会对我和其他读者有很大帮助,因为它包含了UML图的几个重要方面 class Book{ String isbn; String publisher; String publishDate; int pages; } class BookItem extends Book{ String barcode; boolean isReferenceOnly; } class Au
class Book{
String isbn;
String publisher;
String publishDate;
int pages;
}
class BookItem extends Book{
String barcode;
boolean isReferenceOnly;
}
class Author{
String name;
String biography;
Collection<Book> book;
}
class Account{
String number;
List<History> history;
String openDate;
AccountState state;
public Account(AccountState state){
this.state = state;
}
}
enum AccountState{
Active,
Frozen,
Closed
}
class Catalog implements Search, Manage{
List<BookItem> bookItem;
/* Implement the methods of Manage interface */
void add(BookItem item){ }
void remove(BookItem item){ }
/* Implement the methods of Search interface */
int search(BookItem item){ }
}
class Account{
String number;
List<History> history;
Student student = new Student();
void setStudent(Student student){
this.student = student;
}
}
interface Search{
int search(BookItem item);
}
interface Manage{
void add(BookItem item);
void remove(BookItem item);
}
class Student{
String name;
String address;
Search searchBook = new Catalog();
}
class Librarian{
String name;
String address;
String position;
Search searchBook = new Catalog();
Manage manage = new Catalog();
Account account = new Account();
void setAccount(Account account){
this.account = account;
}
class Library{
String name;
String Address;
List<BookItem> bookItem = new ArrayList<BookItem>();
Catalog catalog = new catalog();
List<Account> accounts = new ArrayList<Account>();
Library(Catalog catalog){
this.catalog = catalog;
}
void setBookItem(List<BookItem> bookItem){
this.bookItem = bookItem;
}
void setAccounts(List<Account> accounts){
this.accounts = accounts;
}
}
教材{
字符串isbn;
字符串发布器;
字符串发布日期;
整版;
}
类BookItem扩展了Book{
字符串条形码;
布尔值仅为参考值;
}
类作者{
字符串名;
弦乐传记;
藏书;
}
类别帐户{
字符串编号;
列出历史记录;
字符串openDate;
会计国家;
公共帐户(AccountState){
this.state=状态;
}
}
枚举帐户状态{
活跃的,
冻结的
关闭
}
类目录实现了搜索、管理{
列出书目;
/*实现管理接口的方法*/
作废添加(簿记项目){}
作废删除(簿记项){}
/*实现搜索接口的方法*/
整数搜索(图书项目){}
}
类别帐户{
字符串编号;
列出历史记录;
学生=新生();
无效设置学生(学生){
这个。学生=学生;
}
}
界面搜索{
整数搜索(图书项目);
}
接口管理{
作废增加(记账项目);
作废删除(记账项目);
}
班级学生{
字符串名;
字符串地址;
searchBook=新目录();
}
班级图书馆员{
字符串名;
字符串地址;
串位置;
searchBook=新目录();
Manage=新目录();
账户=新账户();
作废设置帐户(帐户){
这个账户=账户;
}
类库{
字符串名;
字符串地址;
List bookItem=新建ArrayList();
Catalog Catalog=新目录();
列表帐户=新的ArrayList();
图书馆(目录){
this.catalog=目录;
}
作废setBookItem(列表簿记项){
this.bookItem=bookItem;
}
作废设置帐户(列出帐户){
这个账户=账户;
}
}
我以以下方式实施,但在各种情况下出现混淆:
(a) 库由许多帐户组成。{Aggregation}
(b) 许多图书项目是图书馆的一部分。{Aggregation}
(c) 帐户是学生的一部分。{Aggregation}
(d) 库必须有目录。{Composition}
请给我一些宝贵的建议,让我能学好。谢谢
- Java中的枚举是通过使用实现的
- 聚合/组合意味着拥有另一个类的字段。如果是弱关联(聚合),则应由setter或其他方法初始化。如果是强关联,则应在类构造函数中初始化,因为类需要它才能生存/工作
class WeakAssociation { } class StrongAssociation { } class NeedWeekAndStrongAssociation { private WeakAssociation weakAssociation; private StrongAssociation strongAssociation; public NeedWeekAndStrongAssociation(StrongAssociation strongAssociation) { this.strongAssociation = strongAssociation; } public void setWeakAssociation(WeakAssociation weakAssociation) { this.weakAssociation = weakAssociation; } }
- 使用依赖性意味着类/接口将在其一个或多个方法中使用另一个类/接口:
class WantToBeUsed { public void methodToBeUsed(String data) { //fancy implementation } } class CannotDoThisAlone { public void cannotDoItAlone(String data) { WantToBeUsed wantToBeUsed = new WantToBeUsed(); wantToBeUsed.methodToBeUsed(data); } }
enum
数据类型?那我们为什么要在这里使用它……因为他们告诉你要这样做。它就在图中。我有关于聚合和组合的基本概念,并且已经看了几个例子。但我的问题是在这种情况下如何实现它。好吧,这是两个聚合的示例还有构图。你在当前的设计中真正需要什么来实现它?谢谢你的帮助。现在我明白你想说什么了,我终于完成了。
class WantToBeUsed {
public void methodToBeUsed(String data) {
//fancy implementation
}
}
class CannotDoThisAlone {
public void cannotDoItAlone(String data) {
WantToBeUsed wantToBeUsed = new WantToBeUsed();
wantToBeUsed.methodToBeUsed(data);
}
}