简体   繁体   English

如何在抽象类中调用函数?

[英]How do I call a function in an abstract class?

I'm still new to Java and there are some concepts that still confuse me. 我对Java还是很陌生,有些概念仍然使我感到困惑。 I'd like to run vibrate in the strummer class from my main class. 我想在我的主要班级的大学生班上振动。 The vibrate method cannot exist in my main class as it is an abstract method (i assume). 振动方法在我的主类中不能存在,因为它是一种抽象方法(我认为)。 I want to call this method from my main class but i'm not sure how since i get a "static reference to a non-static method" error. 我想从我的主类中调用此方法,但由于我收到“对非静态方法的静态引用”错误,因此我不确定如何使用。 I have a very basic idea about why this doesn't work but i need to know how you would call this method assuming it wont work in the playTheSound method. 对于这个为什么不起作用,我有一个非常基本的想法,但是我需要知道如果在playTheSound方法中不起作用,您将如何调用该方法。

Heres the code 继承人代码

public void playTheSound() {
    // set up MediaPlayer
    MediaPlayer mp = new MediaPlayer();
    switch (i) {
    case 1:
        mp = MediaPlayer.create(this, R.raw.cmaj);
        mp.start();
        strummer.vibrate(pattern, repeat);
        break;
    }

}   
}

 abstract class strummer {

public abstract void vibrate (long[] pattern, int repeat);

 }

Firstly, Strummer an abstract class, therefore you cannot instantiate it directly. 首先, Strummer是一个抽象类,因此您不能直接实例化它。 You must create a subclass, say MyStrummer extends Strummer which overrides any abstract methods in Strummer . 您必须创建一个子类,例如MyStrummer扩展了StrummerMyStrummer重写了Strummer中的任何抽象方法。

public abstract class Strummer
{
    public abstract void vibrate(long[] pattern, int repeat);//This is only the signature.
}

public class MyStrummer extends Strummer
{
   public void playTheSound() {
      this.vibrate();
      //Do your stuff over here
   }
   public void vibrate(long[] pattern, int repeat){//Overriding the method
      //Write vibrate method details here.
   }
}

When you declare a method abstract inside an abstract class the class that inherits the abstract class must implement the abstract methods. 当您在抽象类中声明方法抽象时,继承该抽象类的类必须实现抽象方法。 Example below: 下面的例子:

public abstract class strummer
{
    public abstract void vibrate(long[] pattern, int repeat);
}

public class yourClass extends strummer
{
   public void playTheSound()
   {
      this.vibrate();
      ...
   }


   // The vibrate method MUST be implemented here
   // since we inherited from an abstract class!
   public void vibrate(long[] pattern, int repeat)
   {
      // write vibrate method here.
   }

}

Stummer ob = new Your_class_Name () ; Stummer ob = new Your_class_Name();

// Your_class_Name = inside which your playTheSound() method is created // Your_class_Name =在其中创建playTheSound()方法的地方

ob.playTheSound() ; ob.playTheSound();

You need at least two classes. 您至少需要两节课。

// Strummer.java
abstract class Strummer {
    public abstract void vibrate (long[] pattern, int repeat);
    protected long[] pattern;  // Not recommended, but this 
    protected int repeat;      // allows the vibrate call to work.
    public void playTheSound(int i) {
        MediaPlayer mp = new MediaPlayer();
        switch (i) {
            case 1:
                mp = MediaPlayer.create(this, R.raw.cmaj);
                mp.start();
                vibrate(pattern, repeat);
                break;
            case 2:
                // stuff
                break;
            // more stuff
        }
    }   
}

and (in same package) 和(在同一包装中)

//RealStrummer.java
public class RealStrummer extends Strummer {
    private int mode;

    public RealStrummer(int mode, long[] pattern, int repeat) {
        this.mode = mode;
        this.pattern = pattern;
        this.repeat = repeat;
    }
    public void vibrate() {
        // stuff
    }
    public void play() {
        playTheSound(mode);
    }
    public static void main(String[] args) {
        RealStrummer rs = new RealStrummer(1, new long[] {1L, 3L, 2L}, 10);
        rs.play();
    }
}

Now, you probably want to reorganize the pattern and repeat instance variables to avoid using protected . 现在,您可能想要重组模式并重复实例变量,以避免使用protected However, note that the non-abstract RealStrummer defines a concrete vibrate method (though I have no idea what it should contain). 但是,请注意,非抽象RealStrummer定义了一种具体的vibrate方法(尽管我不知道它应该包含什么)。
Strummer can invoke it, even when it only declares it. 即使仅声明它, Strummer也可以调用它。 Since you cannot create a Strummer instance, that's okay. 由于您无法创建Strummer实例,所以可以。

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

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