简体   繁体   English

Java中的抽象类

[英]Abstract classes in Java

So, I have an object-oriented assignment to do. 因此,我有一个面向对象的任务要做。 Small part of it is to make a graph, made out of nodes, which can be either a type category or type product. 它的一小部分是制作由节点组成的图,该图可以是类型类别或类型乘积。

The category has only a name ([a-zA-Z0-9]+) as parameters. 该类别只有一个名称([a-zA-Z0-9] +)作为参数。

The product hast a name ([a-zA-Z0-9]+) and an id (>=0). 产品具有名称([a-zA-Z0-9] +)和ID(> = 0)。

At the beginning a made just a node, with two objects category in product, which where both null, and depending on the input I created an instance of one of the two things, and everything was fine. 刚开始时,它只是一个节点,在产品中有两个对象类别,其中两个都为null,并且根据输入,我创建了两个事物之一的实例,一切都很好。

I really put a thought in it and after I checked the lectures I found about the thing abstract classes :D . 我真的考虑了一下,在检查了讲课后,我发现了有关抽象类:D的东西。 No my idea is to make Node abstract and Product and Category extending Node,since: 我的想法不是使Node抽象,而使Product和Category扩展Node,因为:

Category has setter and getter for Name and toString Product has setter and getter for Name AND ID (of course) and toString. 类别具有Name和toString的setter和getter产品具有Name和ID(当然)和toString的setter和getter。

Setter,getter and toString for Name are identical. Name的Setter,getter和toString相同。 There's a differnce in the constructor for Product, because it sets also the ID. Product的构造函数有所不同,因为它还会设置ID。

So, Is it going to work that way, is it better? 那么,它会那样工作吗,会更好吗? Can I create a Node and then after the input say -> this node is from type category 我可以创建一个节点,然后在输入后说->此节点来自类型类别

You cannot just create a Node if it is abstract. 如果节点是抽象的,则不能仅仅创建它。

You should be getting the input, and in that probably the type that wants to be created, and create the appropriate non abstract class for that one. 您应该获取输入,并在其中输入可能要创建的类型,并为该输入创建适当的非抽象类。

Alternatively you can create them as Category and if the user types an Id create a new Product and copy the data across from the first object to the second. 或者,您可以将它们创建为“类别”,如果用户键入ID,则创建一个新产品,然后将数据从第一个对象复制到第二个对象。 But that doesn't makes much sense. 但这没有多大意义。

Seems like category should just be part of product. 似乎类别应该只是产品的一部分。 That is you have a category class, and a product class which has two properties: category class and id. 那就是您有一个类别类,以及一个产品类,它具有两个属性:类别类和id。 That way, you can create a category out of any point on your graph, and then if it is a product, pass it to a second constructor to give it an id. 这样,您可以在图形上的任何点之外创建一个类别,然后将其传递给第二个构造函数以为其指定ID,然后将该类别传递给产品。

I think this should be an abstract method validate that each node type can implement independently. 我认为这应该是一种验证每种节点类型可以独立实现的抽象方法。

Node suggests that this is a graph of states that you're navigating. Node建议这是您正在浏览的状态图。 Perhaps you want a method to fire on entering or leaving that state. 也许您想要一种在进入或退出该状态时触发的方法。 Each type can put whatever they need into it. 每种类型都可以放入他们需要的任何内容。 Interfaces and strategy patterns will help here. 界面和策略模式将对此有所帮助。

Keep the graph separate from what's done. 将图形与所做的工作分开。

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

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