Android调用父类方法是什么意思演示一下
时间: 2023-08-04 08:09:17 浏览: 57
在 Android 中,如果我们想要在子类中调用父类的方法,可以使用 `super` 关键字来实现。
下面是一个简单的示例,演示如何在子类中调用父类的方法:
```java
public class Animal {
public void eat() {
System.out.println("Animal is eating.");
}
}
public class Dog extends Animal {
@Override
public void eat() {
super.eat(); // 调用父类的 eat() 方法
System.out.println("Dog is eating.");
}
}
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Dog dog = new Dog();
dog.eat(); // 输出:Animal is eating. Dog is eating.
}
}
```
在上面的示例中,`Dog` 类继承自 `Animal` 类,并重写了 `eat()` 方法。在 `eat()` 方法中,使用 `super.eat()` 调用了父类的 `eat()` 方法,然后再输出 `"Dog is eating."`。在 `MainActivity` 中,我们创建了一个 `Dog` 对象,并调用了它的 `eat()` 方法,最终输出了 `"Animal is eating. Dog is eating."`。