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

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

问题描述

最近我决定看看Java,所以我还是很新的,也是OO编程的方法,所以我想在学习更多内容之前先做一些事情,(我想它永远不会很快开始好的做法)。



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



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



我有一个类描述主角(Survivor),因为它非常大,我想用不同的函数编写一个接口,这样我就可以轻松地看到并分享它的结构。这是好的做法吗?或者只是浪费空间和时间?



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

解决方案

你可以将界面视为契约。您正在定义一组实现此接口的类必须实现的方法。



另一方面,当您有一些可能的代码时,将使用抽象类。您想要实现的所有子类都是通用的。所以你可能有一个名为Shape的抽象类,它有一些公共代码,在你的派生类(Circle,Square等)中,你可以拥有特定于那些形状的代码( getArea 就是一个例子)。但是像颜色这样的东西可能对所有形状都很常见,所以你可以在你的Shape抽象类中放一个 getColor 方法。



<你可以把这两个想法结合起来。你可以有抽象的类来实现接口,这给你两全其美。



这些概念在OO中反复使用,所以理解它很重要他们。你似乎很顺利:)。



所以如果你的僵尸类有一些适用于所有类型的僵尸的常见行为,它听起来像是一个很好的候选者是一个抽象的类。你还可以考虑创建一个界面(可能是 GameCharacter 界面),如果你的游戏中有其他角色(可能是 UndeadMice 或者其他的东西 :))。然后你的 Zombie 抽象类和 UndeadMouse 抽象类将实现 GameCharacter interface。


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).

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.

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.

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 :).

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天全站免登陆