Java的使用【接口/抽象类】 [英] Use of Java [Interfaces / Abstract classes]

查看:26
本文介绍了Java的使用【接口/抽象类】的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我决定看一看 Java,所以我对它和面向对象编程的方法还是很陌生,所以我想在学习更多之前直接了解一些东西,(我想它永远不会很快开始良好的做法).

Lately i decided to take a look at Java so i am still pretty new to it and also to the approach of OO programming, so i wanted to get some things straight before learning more, (i guess it's never to soon to start with good practices).

我现在正在编写一个小 2D 游戏,但我认为我的问题适用于任何非平凡的项目.为简单起见,我将提供游戏中的示例.

I am programming a little 2D game for now but i think my question applies to any non trivial project. For the simplicity I'll provide examples from my game.

我有不同种类的僵尸,但它们都有相同的属性(x、y、健康、攻击等),所以我写了一个界面僵尸,我通过 WalkingZombie、RunningZombie TeleportingZombie 等实现.这是最好的做法吗?我更喜欢抽象类?还是超类?(我不打算部分实现功能 - 因此我选择接口而不是抽象类)

I have different kinds of zombies, but they all have the same attributes (x, y, health, attack etc) so i wrote an interface Zombie which i implement by WalkingZombie, RunningZombie TeleportingZombie etc. Is this the best thing to do? Am i better of with an abstract class? Or with a super class? (I am not planning to partially implement functions - therefor my choice for an interface instead of an abstract class)

我有一个描述主角(幸存者)的类,因为它非常大,我想编写一个具有不同功能的界面,以便我可以轻松查看和共享它的结构.这是好的做法吗?还是只是浪费空间和时间?

I have one class describing the main character (Survivor) and since it is pretty big i wanted to write an interface with the different functions, so that i can easily see and share the structure of it. Is it good practice? Or is it simply a waste of space and time?

我希望这个问题不会被认为是主观的,因为我认为有经验的程序员不会反对这种话题,因为接口/超类/抽象类的使用遵循逻辑规则,因此不仅仅是个人选择.

I hope this question will not be rated as subjective because i thought that experienced programmers won't disagree about this kind of topic since the use of interfaces / super classes / abstract classes follows logical rules and is thereby not simply a personal choice.

推荐答案

您可以将界面视为合同".您正在定义一组实现此接口的类必须实现的方法.

You can think of an interface as a "contract". You are defining a set of methods that classes which implement this interface must implement.

另一方面,当您有一些代码可能对您要实现的所有子类通用时,将使用抽象类.因此,您可能有一个名为 Shape 的抽象类,它具有一些通用代码,并且在您的派生类(圆形、方形等)中,您可以拥有特定于这些形状的代码(getArea 将是一个例子).但是像颜色这样的东西可能对所有形状都是通用的,所以你可以在你的 Shape 抽象类中放置一个 getColor 方法.

An abstract class, on the other hand, is used when you have some code that could be common to all the child classes you want to implement. So you might have an abstract class called Shape that has some common code, and in your derived classes (Circle, Square, etc.) you could have the code that is specific to those shapes (getArea would be an example). But something like color might be common to all shapes, so you could put a getColor method in your Shape abstract class.

你可以将这两个想法结合起来.您可以拥有实现接口的抽象类,这为您提供了两全其美的优势.

And you can combine the two ideas. You can have abstract classes which implement interfaces, and this gives you the best of both worlds.

这些概念在面向对象中反复使用,因此理解它们很重要.你看起来很顺利:)

These concepts are used over and over again in OO, so it's important to understand them. You seem to be well on your way :).

因此,如果您的僵尸类有一些适用于所有类型僵尸的通用行为,那么作为抽象类听起来是一个不错的选择.如果您的游戏中有其他角色(可能是 UndeadMice 或其他东西 :)),您也可以考虑创建一个界面(可能是 GameCharacter 界面).然后你的 Zombie 抽象类和 UndeadMouse 抽象类将实现 GameCharacter 接口.

So if your zombie class has some common behavior that applies to all types of zombies, it sounds like a good candidate to be an abstract class. You could also consider creating an interface (maybe a GameCharacter interface) if you have other characters in your game (maybe UndeadMice or something :)). Then your Zombie abstract class and UndeadMouse abstract class would implement the GameCharacter interface.

这篇关于Java的使用【接口/抽象类】的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆