简体   繁体   English

我如何将代码移到另一个类?

[英]How would I move code to a different class?

In the SimpleApp tutorial, the author puts all of his code inside one class file. SimpleApp教程中,作者将所有代码都放在一个类文件中。 Causing the rain and the bucket to be within it. 使雨水和水桶进入其中。

I tried just taking the code and putting it into another class but then I would have duplicate methods in my code, and logically it would be incorrect. 我尝试仅将代码放入另一个类中,但是我的代码中将有重复的方法,从逻辑上讲,这是不正确的。 For example, I cannot have two create methods in a game. 例如,我不能在游戏中使用两种创建方法。

How would I take the rain or the bucket from the tutorial and put it into a different class? 我将如何从本教程中摘下雨水水桶 ,并把它放在另一个班上?

You'd abstract out the common functions into an Abstract class or Interface and then have both rain and bucket inherit from that class. 您可以将常用功能抽象到Abstract类Interface中 ,然后让rain和bucket都从该类继承。

So, for your particular example, go through both classes, find all the common functions and abstract them into a superclass. 因此,对于您的特定示例,遍历两个类,找到所有通用函数并将其抽象为超类。

It's very simple: you could benifit from OOP style coding: 这很简单:您可以从OOP样式编码中受益:
Just use a super class RainBucket that has all the methods in it with minimal code with each method. 只需使用包含所有方法的超类RainBucket ,每个方法的代码最少即可。
Extend the other two classes from this super class and override in it the methods that your class wants to use, this way you would be acomplishing the following OOP rules: 从该超类扩展其他两个类,并在其中重写您的类要使用的方法,这样您将完成以下OOP规则:

  1. Inheritence: when you derive from the super class two other objects: 继承:当从超类派生两个其他对象时:

    class Bucket extends RainBucket {

    class Rain extends RainBucket {

  2. polymorphism: you can declare two objects from the same super class but by assigning to the two different derived classes as values like this: 多态性:您可以从同一个超类中声明两个对象,但可以通过将两个不同的派生类分配为如下值:

    RainBucket bucket = new Bucket() and RainBucket bucket = new Bucket()

    RainBucket rain = new Rain()

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

相关问题 我如何将变量从一个班级移到另一个班级 - How would I move variables from class to class 如何将MotionListener分离到另一个类? - How would I separate the MotionListener to a different class? 如何在此代码中移动随机对象? - How would I make a random object move in this code? 我如何从 Java 中的不同类访问变量? - How Would I get access to a variable from a different class in java? 我将如何从不同的类调用方法? - How would I call a method from a different class? 如何在Button上使用ActionListener来更改其他类中的JLabel? - How would I use an ActionListener on a Button to change JLabels in a different class? 如果它们在同一个类中,如何从不同的方法获取字符串 - How would I get a string from a different method if they are in the same class 如何将参数传递给类,以便在构造函数中传递参数时JFrame会移动? - How do I pass a parameter into a class so that when its parameter is passed in the constructor the JFrame would move? 我如何将变量传递给类,然后返回给它使用不同值定义的类? - How would I pass a variable through to a class, then back out to the class it was defined in with a different value? 我将如何编码此Listview? - How would I code this Listview?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM