简体   繁体   English

java类和子类

[英]java classes and subclasses

On my next step of learning Java, I'm now on Classes and Inheritance. 在我学习Java的下一步,我现在正在进行类和继承。

I have one homework to solve but I'm a little stuck so I'm here not asking for code but to see if you agree with the way I'm thinking and if not, help me with some guidance. 我有一个功课要解决,但我有点卡住所以我在这里不是要求代码,而是看你是否同意我的思考方式,如果没有,请帮助我一些指导。 Please understand that unfortunately I do not understand all of Java and I'm taking baby steps on this language. 请理解,遗憾的是我并不了解所有的Java,而且我正在采用这种语言。

So, here is the problem: 所以,这是问题所在:

It is intended to create a program that supports the orgazation of conferences. 它旨在创建一个支持会议组织的计划。 A conference has a name, a location and a date. 会议有名称,位置和日期。 In addition, each conference has a organization committee and a program committee. 此外,每个会议都有一个组织委员会和一个计划委员会。 Members of both committees are identified by name, email address and institution. 两个委员会的成员都通过姓名,电子邮件地址和机构来识别。 The organization committee members are also characterized for their role in the conference (finance, spaces or meals). 组织委员会成员的特点还在于它们在会议中的作用(财务,空间或餐饮)。 The program committee members are characterized by a parameter that reflects their performance in the previous edition of the conference (poor, fair, good, excellent). 计划委员会成员的特点是反映他们在上一届会议中表现的参数(差,公平,良好,优秀)。 In addition, program committee members are also associated with a list of articles that need review. 此外,计划委员会成员还与需要审查的文章列表相关联。

Each article is characterized by it's title, list of author's and the evaluation result (note between 1 and 5, with 5 being the best). 每篇文章的特点是它的标题,作者列表和评估结果(注意在1和5之间,其中5是最好的)。 The final evaluation of each article is obtained by the average of the evaluations conducted by two members of the program committee. 每篇文章的最终评估是通过计划委员会两名成员进行的评估的平均值得出的。 The authors are characterized by name, email address, institution and registration fee. 作者的特点是姓名,电子邮件地址,机构和注册费。 This fee can be of two types: senior (teachers or researchers) and students. 这笔费用可以分为两类:高级(教师或研究人员)和学生。 Students should consider the degree (bachelor, master or doctorate). 学生应该考虑学位(学士,硕士或博士学位)。

The conference registration fee is 400$ for seniors and 200$ for students. 会议注册费为老年人400美元,学生200美元。 A subscription entitles you to submit one article. 订阅授权您提交一篇文章。 Senior authors who want to submit more than an article must pay 50$ for each additional article. 想要提交超过文章的高级作者必须为每篇附加文章支付50美元。 It is considered that the first author of each article will be responsible for its presentation at the conference. 据认为,每篇文章的第一作者将负责在会议上的发言。

With this I have to implement some functions but that's not why I'm here. 有了这个我必须实现一些功能,但这不是我在这里的原因。

Reading the problem i can see that 阅读问题我可以看到

name
email
institution

is used to identify member's of committee and authors. 用于识别委员会成员和作者。

So I was thinking of creating an 所以我在想创造一个

public class Identification{
    ---------------
    name
    email
    institutionName
    ---------------
}

and then everything gets complicated because I know I have to create some that extend that class. 然后一切都变得复杂,因为我知道我必须创建一些扩展该类的东西。

I can create 我可以创造

public class Conference{
    ---------------
    name
    location
    date[]
    ---------------
}

and then 接着

public class Organization extends Conference{
    ---------------
    role[] // 0 for finance, 1 for spaces and 2 for meals
    ---------------
}

and

public class Program extends Conference{
    ---------------
    performance[] //0 poor, 1 fair, 2 good and 3 excellence
    articlesList[] // probably an id of and article
     ---------------
}

but both classes Organization and Program have members and this members are identified by the same thing that is on class Identification. 但是组织和程序这两个类都有成员,并且这些成员的标识与类标识相同。 As I now, Java can only extend one and only one class so now I'm stuck how this things relation between themselves. 就像我现在一样,Java只能扩展一个且只能扩展一个类,所以现在我已经陷入了这种事物之间的关系。

Reading the rest of the problem I identify 阅读我发现的其他问题

public class Article{
    ---------------
    title
    listOfAuthors[] //probably an id
    evaluation //this is done by two members on program committee but I can't figure out how they relationship between themselves.
     ---------------
}

public class Authors{
    ---------------
    type[][] // 0 for seniors and 1 for students on first column and 0 teachers, 1 researchers, 2 bachelor, 3 master and 4 doctorate on second column.
    numberOfArticles //how many articles author wants to submit
    conferenceFee[] // if on type[i][0] is 0 then Fee is 400 + (numberOfArticles * 50)  else is 200
    ---------------
}

Is this the way or not. 是这样的方式。 I can't understand if I have to create all of this classes and what class extends the other class. 我无法理解是否必须创建所有这些类以及哪个类扩展了另一个类。 Any comment? 任何意见?

favolas favolas

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!UPDATE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! UPDATE !!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Thanks for all your feedback. 感谢您的所有反馈。 Let's see if I understood at least some of your suggestions. 让我们看看我是否至少理解了你的一些建议。

public class Member{
    ---------------
    memberID
    name
    email
    institutionName
    ---------------
}

public class Authors extends Member{
    ---------------
    authorsID
    conferenceFee
    ---------------
}

public class Seniors extend Authors{
    ---------------
    degree[] // 0 for teachers 1 for researchers
    numberOfArticles = // Some int
    fee = 400

    public int payment(int numberOfArticles ){
        return numberOfArticles * fee;
    }
    ---------------
}

public class Students extend Authors{
    ---------------
    degree[] // 0 bachelor, 1 master and 2 doctorate
    numberOfArticles = // Some int
    fee = 200

    public int payment(){
        return fee;
    }
    ---------------
}

public class Article extend Authors{
    ---------------
    articleID
    title
    listOfAuthors[] //probably an id
    ---------------
}

public class ProgramCommitteeMember extends Member{
    ---------------
    role[] // 0 for finance, 1 for spaces and 2 for meals

    ---------------
}

public class OrganizationCommitteeMember extends Member{
    ---------------
   lasYearPerformance[] //0 poor, 1 fair, 2 good and 3 excellence
   articlesList[] // probably an id of and article
    ---------------
}

public class Committee{
    ---------------
    committeeType[] // 0 for Organization Committee and 1 for Program Committee
    ---------------
}

public class OrganizationCommittee extends Committee{
    ---------------
    listOfMembers[] //member id
    ---------------
}

public class ProgramCommittee extends Committee{
    ---------------
    listOfMembers[] //member id
    ---------------
}

His this the way? 他这样的方式?

I think you're confusing inheritance and composition. 我认为你混淆了继承和构成。

Is an Organization a kind of Conference? 组织是一种会议吗? If not, don't inherit from it. 如果没有,请不要继承它。 One of them needs to contain the other - when the first one goes away, the second must. 其中一个需要包含另一个 - 当第一个消失时,第二个必须。 It may be also be true that the other one needs to refer back to the first - that the child needs to know their parent. 也可能是另一个需要回头看第一个 - 孩子需要知道他们的父母。 That's ok too. 那也没关系。

Is a Program a kind of Conference? 节目是一种会议吗? Same thing goes here, too. 同样的事情也在这里。

Take a look at the Liskov Substitution Principle . 看看利斯科夫替代原则

I see several points where I think you've gone off the rails. 我看到几点,我认为你已经脱轨了。

  1. You show date as an array in Conference. 您在会议中将日期显示为数组。 Each instance of Conference has only a single date. 每个会议实例只有一个日期。

  2. You show both Organization and Program as a subclass of Conference. 您将组织和程序显示为会议的子类。 They should not be. 他们不应该。 Instead there should be a superclass called Committee, with two subclasses, OrganizationCommittee and ProgramCommittee. 相反,应该有一个名为委员会的超类,有两个子类,OrganizationCommittee和ProgramCommittee。 Then your Conference class has two internal variables, one each of type OrganizationCommittee and ProgramCommittee. 然后你的Conference类有两个内部变量,分别是OrganizationCommittee和ProgramCommittee类型。 In other words, a "committee" is not a type of a "conference," it is a member of a "conference." 换句话说,“委员会”不是“会议”的类型,而是“会议”的成员。 (By member here, I don't mean "a person going to a conference," I mean, "an object that is owned by another object.") (在这里,我不是指“一个人去参加会议”,我的意思是“一个由另一个对象拥有的对象。”)

  3. Each committee has members. 每个委员会都有成员。 So you need a Member superclass, with two subclasses, OrganizationCommitteeMember and ProgramCommitteeMember. 所以你需要一个成员超类,有两个子类,OrganizationCommitteeMember和ProgramCommitteeMember。 Your ProgramCommittee will then contain a list (an array for exmaple) of ProgramCommitteeMembers, and your OrganizationCommittee will contain a list of OrganizationCommitteeMembers. 然后,您的ProgramCommittee将包含ProgramCommitteeMembers的列表(exmaple数组),您的OrganizationCommittee将包含OrganizationCommitteeMembers列表。

  4. One other thing, you show role[] as an array in Organization. 另一件事,你将role []显示为组织中的数组。 First, a role applies to a member of a committee, not an organization. 首先,角色适用于委员会的成员,而不是组织。 Second, each member has only one role. 其次,每个成员只有一个角色。 What you need is a way to define a prescribed list of roles, so that only one of several valid values can be assigned. 您需要的是一种定义规定的角色列表的方法,以便只能分配几个有效值中的一个。 But each member has only a single role. 但每个成员只有一个角色。

Key on where your instructions use the phrase "has a" (which implies that one class contains an instance of another class) versus "is a" (which implies that one class is a type of a parent class). 键入你的指令使用短语“有一个”(这意味着一个类包含另一个类的实例)与“是一个”(这意味着一个类是父类的类型)。

Hope this helps to get you on the right track. 希望这有助于让您走上正轨。

Have a close look at the "entities" you need to represent. 仔细查看您需要表示的“实体”。 Entites will probably become objects in your program. Entites可能会成为您程序中的对象。 What entities/objects do you need? 您需要哪些实体/对象? - Start, for instance, with the entities "commitee" and "committee member". - 例如,与实体“委员会”和“委员会成员”开始。

Once you found all your involved entities, you can start to look at and compare them: 找到所有涉及的实体后,您可以开始查看并比较它们:

Are there different entities which have things (properties) in common? 是否有不同的实体有共同的东西(属性)? These are candidates for a common super-class of those entities. 这些是这些实体的共同超类的候选者。 Your proposed Identification class is just the right way to do it. 您提出的Identification类是正确的方法。 Now take it a step further: Which entities share the property of being "identifiable"? 现在更进一步:哪些实体共享“可识别”的属性? Those should inherit from Identification . 那些应该继承自Identification

Also remember that "class B inherits from class A " usually means "B is a special kind of A". 还记得“ B继承A级”通常意味着“B是一种特殊的A”。 Is your organization a special kind of conference ? 您的组织是一种特殊的会议吗?

You're correct that Java does not allow multiple inheritance (that is, each class can only extend one other class). 你是正确的,Java不允许多重继承(也就是说,每个类只能扩展另一个类)。

In general, the idea with classes, subclasses and inheritance is that objects of particular subclasses are specific kinds of their super class. 通常,具有类,子类和继承的想法是特定子类的对象是其超类的特定种类。 For example, it would make sense to have a class Animal, and then create a subclass Horse that extends Animal. 例如,拥有一个Animal类是有意义的,然后创建一个扩展Animal的子类Horse。

Or, using the framework you've introduced, you could have an Author class, and then a Senior class and a Student class, both of which would extend Author. 或者,使用您介绍的框架,您可以拥有一个Author类,然后是一个Senior类和一个Student类,这两个类都将扩展Author。 A nice way to take advantage of this structure could be to include a pay() method in each of the three classes that returns an int. 利用这种结构的一种好方法可能是在返回int的三个类中包含一个pay()方法。 Since student authors only need to pay $200, the pay() method in the Student class would return 200 while the pay() method in the Senior class would return 400. The key observation here is that now you can call the pay() method on any object that is an Author, so you don't need to check in advance whether the author is a senior or a student. 由于学生作者只需支付200美元,Student类中的pay()方法将返回200,而Senior类中的pay()方法将返回400.这里的关键观察是现在你可以调用pay()方法作为作者的任何对象,因此您无需事先检查作者是高级还是学生。 You should be able to think of similar ways to handle the other areas of the problem. 您应该能够考虑类似的方法来处理问题的其他方面。

Not much inheritance in there, I think I'd be using aggregation and interfaces all the way through. 在那里没有太多的继承,我想我会一直使用聚合和接口。

Member (you called this identification) Then CommitteeMember extends Member and ProgramCommitteeMember extends Member 会员(您称此身份证明)然后委员会成员和会员委员会成员延伸会员

Look at inheritance as X is a Y 看看继承,因为X是Y.

Organisation is a Conference doesn't really make sense. 组织是一个没有意义的会议。 It's the role a committemeber fulfilled at a conference. 这是委员会在会议上履行的职责。 so Conference and Conferences then Role describes member and conference. 所以会议和会议然后角色描述成员和会议。

Program committee member hasa performance against a conferences so again you have Conference, Conferences and Performance describes memebr and conference. 计划委员会成员在会议上有表现,所以你再次召开会议,会议和表演会议,讨论会议和会议。

So you could define a base class conferencemember and then extend that for role and performance. 因此,您可以定义基类会议成员,然后针对角色和性能进行扩展。

It's all about has a and is a A book has an author. 这是关于有一个和一本书有一个作者。 An author is a writer. 作者是作家。

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

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