简体   繁体   English

在我的java applet程序中添加另一个类

[英]adding another class to my java applet program

I am making a knight's tour implementation in java, and currently I have everything jumbled into one giant mess of code. 我正在java中制作一个骑士游览实现,目前我把所有内容混杂在一堆巨大的代码中。

I have a class called MainFrame which includes the code to solve the knights tour as well as methods for the menu, etc. 我有一个名为MainFrame的类,其中包括解决骑士之旅的代码以及菜单的方法等。

I want to create a new class called KT (for knights tour) which will contain the code for the algorithm, but I'm having lots of issues doing that. 我想创建一个名为KT的新类(用于骑士之旅),它将包含算法的代码,但是我有很多问题要做。

I don't want to post code here because i dont want someone from my class copying or something, so I will just briefly explain. 我不想在这里发布代码因为我不希望我的班级中有人复制或者其他什么,所以我只是简单地解释一下。

In class KT, I have declared the variables, arrays, etc. I have methods such as printSolution, move, redo (the backtracking), etc. 在类KT中,我已经声明了变量,数组等。我有printSolution,move,redo(回溯)等方法。

However I am unsure how to tie in the code for the buttons (which is declared in MainFrame). 但是我不确定如何绑定按钮的代码(在MainFrame中声明)。 For example, I have a loop in the print method that prints the correct solution on the 8x8 board. 例如,我在print方法中有一个循环,它在8x8板上打印正确的解决方案。 Right now I'm being asked to create a new method for the button even though I have the button in class MainFrame. 现在我被要求为按钮创建一个新方法,即使我在MainFrame类中有按钮。

I have a KT k = new KT(); 我有一个KT k = new KT(); and then I'm launching MainFrame. 然后我启动MainFrame。 Is that where I am doing it wrong or is it something really simple that I'm being too dumb to figure out? 这是我在做错的地方,还是一些非常简单的东西,我太愚蠢了?

tl;dr program works well when i have everything in one class, but i want to make two classes and make everything "look" nice tl; dr程序在我在一个班级中拥有所有内容时效果很好,但是我想制作两个班级并使一切“看起来”不错

In the actionPerformed method of your MainFrame class just call the appropriate methods to get the solution from KT (which, by the way, I would rename to KnightsTour ...readability counts). MainFrame类的actionPerformed方法中,只需调用适当的方法从KT获取解决方案(顺便说一下,我将重命名为KnightsTour ...可读性计数)。

Ideally you want all your logic (the model) broken up into sensible methods in KnightsTour , and all your display and button-handling code (the view and controller) in MainFrame . 理想情况下,你希望所有的逻辑(模型)分解成合理的方法KnightsTour ,和所有的显示和按键处理代码中(视图和控制器) MainFrame If that's difficult, it's a good sign that you need to rethink how you divided things into methods (and what you're doing with global state...which is frowned upon). 如果这很困难,那么你需要重新思考如何将事物划分为方法(以及你对全球国家所做的事情......这是不赞成的),这是一个好兆头。

I'm sorry I can't be more specific--I'm kind of hand-tied since you didn't post code. 对不起,我不能说得更具体 - 因为你没有发布代码,所以我有点紧张。

First of all, give your KnightTour class an actual name. 首先,给你的KnightTour类一个实际的名字。 Like, you know, KnightTour. 就像你知道的,KnightTour。 If you were coding this for cash money, the next guy who had to read your code would want to punch the guy who called a class something like KT. 如果你用现金来编写这个,那么下一个必须阅读你的代码的人就会想要打电话给那个叫KT类的人。

Think about creating this class so that you can use it from a GUI controller like your button and menu laden applet. 考虑创建这个类,以便您可以从GUI控制器(如按钮和菜单载荷小程序)中使用它。 But so that it can ALSO be used from, say, a character based application where you type commands at a prompt and have your new class evaluate those commands. 但是,它也可以用于,例如,基于字符的应用程序,您可以在提示符下键入命令并让新类评估这些命令。

The reason I suggest this is because it will force you to create your KnightTour class so that it is PURELY the "business logic" for your app. 我建议这样做的原因是因为它会迫使你创建你的KnightTour类,以便它是你应用程序的“业务逻辑”。 By that I mean that your KnightTour class should not know anything about buttons, menus, GUIs, text interfaces, or anything like that. 我的意思是你的KnightTour类不应该知道关于按钮,菜单,GUI,文本界面或类似的东西。 You need to think about your KnightTour class in terms of what it must accomplish. 你需要根据它必须完成的事情来考虑你的KnightTour课程。

I don't really know what KnightTour does. 我真的不知道KnightTour的作用。 So I'll just throw out some ideas of what kind of functionality it might need to support. 因此,我只想提出一些可能需要支持的功能的想法。 I'm assuming everything takes place on a chess board? 我假设一切都发生在国际象棋棋盘上?

  1. Get the state (occupied, unoccupied) for a given board location (x,y) 获取给定电路板位置(x,y)的状态(占用,未占用)
  2. Put a chess piece (piece enumerator) on a given location (x,y) 将棋子(片段枚举器)放在给定位置(x,y)
  3. Validate placement of a piece (piece enumerator, location x,y) 验证一块(片段枚举器,位置x,y)的位置
  4. Suggest a move, returning a Suggestion object with a piece enumerator and location 建议移动,返回带有片段枚举器和位置的Suggestion对象
  5. Reset the board to start all over. 重置电路板以重新开始。

Now, when you push a button that says "place a piece on 5,5" then you'll handle that event in your GUI controller, and then call the "set piece" method (#2 above) to do that work. 现在,当你按下一个“放置5,5”的按钮时,你将在GUI控制器中处理该事件,然后调用“set piece”方法(上面的#2)来完成这项工作。 If you have a character based application, when you type "put knight at 5,5" then you'll parse that text, and then invoke #2 above. 如果你有一个基于角色的应用程序,当你输入“把骑士放在5,5”时,你将解析该文本,然后调用上面的#2。 The key point is that both of those user interfaces access the same KnightTour methods to do the same work. 关键是这两个用户界面都使用相同的KnightTour方法来完成相同的工作。

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

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