I'm still new to Java and there are some concepts that still confuse me. 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.
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. You must create a subclass, say MyStrummer
extends Strummer
which overrides any abstract methods in 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 () ;
// Your_class_Name
= inside which your playTheSound()
method is created
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
. However, note that the non-abstract RealStrummer
defines a concrete vibrate
method (though I have no idea what it should contain).
Strummer
can invoke it, even when it only declares it. Since you cannot create a Strummer
instance, that's okay.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.