完善代码class Father { int a; public Father(int a) { this.a=a; } public void print() { System.out.println(a); } } class Child extends Father { int a; public Child(int a) { // 将形参a的数值赋给父类成员变量a // 将父类成员变量a的值*10赋给本类的成员变量a } public void print() { System.out.println( ); // 输出父类成员变量a的值 System.out.println( ); // 输出子类成员变量a的值 } } public class Main { public static void main(String[] args) { Child child=new Child(10); child.print(); } }
时间: 2023-12-10 18:41:37 浏览: 92
完善的代码如下所示:
```
class Father {
int a;
public Father(int a) {
this.a = a;
}
public void print() {
System.out.println(a);
}
}
class Child extends Father {
int a;
public Child(int a) {
super(a);
this.a = super.a * 10;
}
public void print() {
System.out.println(super.a);
System.out.println(a);
}
}
public class Main {
public static void main(String[] args) {
Child child = new Child(10);
child.print();
}
}
```
在子类的构造方法中,我们可以使用 `super(a)` 来调用父类的构造方法,将形参 `a` 的值传递给父类的成员变量 `a`。然后,我们可以使用 `super.a` 来获取父类成员变量 `a` 的值,并将其乘以 10 赋给本类的成员变量 `a`。
在子类的 `print()` 方法中,我们可以使用 `super.a` 来输出父类成员变量 `a` 的值,使用 `a` 来输出子类成员变量 `a` 的值。
相关推荐
![.zip](https://img-home.csdnimg.cn/images/20210720083646.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)