2 回答
TA贡献1796条经验 获得超7个赞
有了继承和重写的概念,您需要在子类中重写 onCreate 函数。从该重写的方法中,您可以调用 super.onCreate 或者可以从子类中执行 this.chnageText 和 this.changeText2 。
否则,当您调用 onCreate 函数时,它将仅从超类调用changeText 和changeText2。
在你孩子的班级里
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.changeText();
this.changeText2();
};
TA贡献1993条经验 获得超5个赞
修改父类
package com.example.flashcards;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
DatabseHelper DB = new DatabseHelper(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//changeText(); freeze or remove these callings
//changeText2();
};
public void changeText(){}
public void changeText2(){}
}
并向您的子类添加一些代码
package com.example.flashcards;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class TextC extends MainActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
changeText();
changeText2();
};
@Override
public void changeText() {
super.changeText();
final String[] revertText = {"H2O", "What elements does water consist of?"};
final TextView textChange = findViewById(R.id.flashcard1);
Button change = findViewById(R.id.answer1);
change.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int random = (int) (Math.random() * revertText.length);
textChange.setText(revertText[random]);
}
});
}
public void changeText2() {
super.changeText2();
final String[] revertText = {"2,200° F", "How hot does lava get?"};
final TextView textChange = findViewById(R.id.flashcard2);
Button change = findViewById(R.id.answer2);
change.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int random = (int) (Math.random() * revertText.length);
textChange.setText(revertText[random]);
}
});
}
}
在上面的方法中没有必要声明changeText(); 和changeText2(); 在家长活动中。为了可重用性,我们可以使用抽象类和方法。
对您的家长活动进行一些更改,如下所示。
public abstract class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
changeText(); // they dont have default implimentation in parent so it will be invoked from child class where these methods implimented
changeText2();
};
public abstract void changeText(); //there is no default implimentation
public abstract void changeText2();
}
在子活动中,您必须实现这些方法。
public class TextC extends MainActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//changeText(); no need to call these methods bcz its already called in parent onCreate()
//changeText2();
};
@Override
public void changeText() {
super.changeText();
final String[] revertText = {"H2O", "What elements does water consist of?"};
final TextView textChange = findViewById(R.id.flashcard1);
Button change = findViewById(R.id.answer1);
change.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int random = (int) (Math.random() * revertText.length);
textChange.setText(revertText[random]);
}
});
}
public void changeText2() {
super.changeText2();
final String[] revertText = {"2,200° F", "How hot does lava get?"};
final TextView textChange = findViewById(R.id.flashcard2);
Button change = findViewById(R.id.answer2);
change.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int random = (int) (Math.random() * revertText.length);
textChange.setText(revertText[random]);
}
});
}
}
添加回答
举报