简体   繁体   English

覆盖子 class 中的父方法。 子方法不做任何事情

[英]Overriding parent method in child class. Child method not doing anything

I am having some trouble.我遇到了一些麻烦。 I followed every guide online showing how to override a parent method in a child class.我按照每个在线指南展示了如何覆盖子 class 中的父方法。 I have done everything I was told to do, yet my child function does nothing.我已经完成了我被告知要做的一切,但我的孩子 function 什么也没做。

My MainActivity(Parent) class:我的 MainActivity(父)class:

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();
        changeText2();
    };
    public void changeText(){}
    public void changeText2(){}
        String [] columns = new String[] {
                DatabseHelper.FLASHCARD_QUESTION,
                DatabseHelper.FLASHCARD_ANSWER
        };

    @Override
    public void onClick(View v) {

    }
}

My child class (TextC)我的孩子 class (TextC)

package com.example.flashcards;

import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class TextC extends MainActivity {

    @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]);
            }
        });
    }
}

My changeText() function does nothing.我的 changeText() function 什么都不做。 I am not getting any errors, so I can not tell what I am doing wrong.我没有收到任何错误,所以我不知道我做错了什么。 Do I need to create an onCreate method for the child class?我是否需要为子 class 创建 onCreate 方法? But I am extending MainActivity which has it.但我正在扩展拥有它的 MainActivity。

Any ideas on why my method overriding is not working?关于为什么我的方法覆盖不起作用的任何想法?

With inheritance and overriding concepts, you need to override onCreate function in your child class.使用 inheritance 和重写概念,您需要在您的孩子 class 中重写 onCreate function。 And from that overridden method, you can make a call to super.onCreate or you can do this.chnageText and this.changeText2 from child class.从那个被覆盖的方法中,您可以调用 super.onCreate 或者您可以从子 class 执行 this.chnageText 和 this.changeText2。

Otherwise when you call onCreate function, it will call changeText and changeText2 from super class only.否则,当您调用 onCreate function 时,它将仅从超级 class 调用 changeText 和 changeText2。

In your child class在您的孩子 class

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        this.changeText();
        this.changeText2();
    };

Modify parent class修改父 class

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(){}

}

And add some code to your child class并为您的孩子添加一些代码 class

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]);
            }
        });
    }
}

In the above method there is no point declare changeText();在上述方法中没有任何意义声明 changeText(); and changeText2();和 changeText2(); in parent activity.在家长活动中。 For the sake of reusability, we can use abstract classes and methods.为了可重用性,我们可以使用抽象类和方法。

Do some changes to your parent activity as you see below.如下所示,对您的父活动进行一些更改。

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();

}

And in child activity, you have to implement those methods.在子活动中,您必须实现这些方法。

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]);
            }
        });
    }
}

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

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