简体   繁体   English

Java类有哪些不同类型?

[英]What are the different types of java classes?

I'm new to SO, so if I've done this wrong, please point me in the right direction. 我是SO的新手,所以,如果我做错了此事,请指出正确的方向。

I think this is a bit of an awkward question, because I'm not so great at articulating my thoughts. 我认为这是一个尴尬的问题,因为我不太擅长表达自己的想法。

At university, we are taught that a java class which is written to be an object, with constructors, getters, setters, etc, are called "Container Classes". 在大学里,我们被教导将被编写为对象的Java类(具有构造函数,getter,setter等)称为“容器类”。 They contain data about themselves(a name attribute, for instance). 它们包含有关自己的数据(例如,名称属性)。

My question is what are other types of classes? 我的问题是什么是其他类型的课程? For instance, you have to have a class where you create and manipulate your objects. 例如,您必须拥有一个用于创建和操作对象的类。 For a small program this isn't a problem(just put it into the main class/method). 对于小程序,这不是问题(只需将其放入主类/方法中)。 For a larger program this would be silly and unmanageable, so obviously you create other classes. 对于较大的程序,这将是愚蠢且难以管理的,因此显然您要创建其他类。 I've taken to naming mine "Handler"s. 我已经开始命名“处理程序”了。 I have a "FileHandler" an "ObjectHandler", etc. 我有一个“ FileHandler”,“ ObjectHandler”等

What type of class are these? 这些是什么类型的课程? Are there other class types out there? 还有其他类型的课程吗?

Edit: To clarify, I'm wondering about what to call a category of classes, such as classes that are designed to do specific things. 编辑:为澄清起见,我想知道如何称呼一个类别的类别,例如旨在做特定事情的类别。 Helper classes? 助手班? Utility classes? 实用程序类?

Final Edit: I answered my own question here: https://stackoverflow.com/a/43964279/7985805 最终编辑:我在这里回答了自己的问题: https : //stackoverflow.com/a/43964279/7985805

At university, we are taught that a java class which is itself an object 在大学里,我们被告知一个Java类本身就是一个对象

I would have to disagree here, classes are templates for objects, you can think of them as containing a description of a particular object ie the object states, the behaviours that object can perform etc. An object is an instance of a class thus a class is not an object . 我在这里不得不不同意,类是对象的模板 ,您可以认为它们包含对特定对象的描述 ,即对象状态,对象可以执行的行为等。对象是类的实例,因此是类不是对象

My question is what are other types of classes? 我的问题是什么是其他类型的课程?

A class can be any type, it just depends on what you're attempting to accomplish eg when making a space invaders game, you could have a class of type Alien(enemies), a class of type Defender(the shooter), a class of type Bullet which the Defender can use to shoot the Aliens etc. 一个类可以是任何类型,仅取决于您要完成的任务,例如在制作太空侵略者游戏时,您可以具有Alien(敌人)类型的类,Defender(射击者)类型的类,后卫可以用来射击外星人等的子弹类型。

In my opinion if you call 'classes' 'container classes' you are only adding a redundant word. 我认为,如果您将“类”称为“容器类”,那么您只会添加一个多余的词。

In larger programs there could be some kind of classes that have the same purpose, eg there could be ModelData classes, which only hold data: like employee, contract, ComplicatedCalculationResult; 在较大的程序中,可能有一些具有相同目的的类,例如,可能有ModelData类,它们仅保存数据:例如employee,contract,ComplicatedCalculationResult; or there could be handler classes: FileHandler, MouseHandler, ... But all this is not set to a fixed wording. 或可能有处理程序类:FileHandler,MouseHandler等。但是,所有这些都没有设置为固定的措词。

You can name your class as it seems fit. 您可以根据自己的喜好命名课程。 At best name it so that someone else can guess what the class is for. 最好的名称是这样,以便其他人可以猜测该课程的用途。

So it turns out after a bit of research, the "class types" I was trying to find names for are actually called "Design Patterns". 因此,经过一番研究,事实证明,我试图为其找到名称的“类类型”实际上称为“设计模式”。 Things like Builders, Factories, Adapters, etc. 诸如建筑商,工厂,适配器之类的东西。

I found a handy tutorial explaining them in further depth here: https://www.tutorialspoint.com/design_pattern/design_pattern_overview.htm 我在这里找到了一个方便的教程,对其进行了更深入的解释: https : //www.tutorialspoint.com/design_pattern/design_pattern_overview.htm

Thanks for trying to answer my very badly worded question. 感谢您尝试回答我措辞不佳的问题。 As I said, I'm terrible at trying to explain what I want to say. 就像我说的那样,我很难解释我想说的话。

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

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