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) {
时间: 2024-04-27 17:20:39 浏览: 27
super(a);
this.a = a * 2;
}
public void print() {
System.out.println(a);
super.print();
}
}
以上是一个父类Father和一个子类Child的代码。当执行下面的代码时,会输出什么?
Father f = new Child(10);
f.print();
请问输出的内容是什么?
相关问题
class father {\n int a;\n public father(int a) {\n this.a=a;\n }\n public void print() {\n system.out.pr
这是一段 Java 代码,定义了一个父类 father,包括一个整型变量 a 和一个带有一个参数的构造函数 father(int a),以及一个方法 print()。在 print() 方法中,使用系统输出语句输出一些内容,但该代码未被完整地呈现出来。
请按注释写出代码有如下父类和子类的定义,请根据要求填写代码。 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); // 将形参a的数值赋给父类成员变量a this.a=super.a*10; // 将父类成员变量a的值*10赋给本类的成员变量a } public void print() { System.out.println( super.a ); // 输出父类成员变量a的值 System.out.println( this.a ); // 输出子类成员变量a的值 } } public class Main { public static void main(String[] args) { Child child=new Child(10); child.print(); } }
```
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); // 将形参a的数值赋给父类成员变量a
this.a = super.a * 10; // 将父类成员变量a的值*10赋给本类的成员变量a
}
public void print() {
System.out.println(super.a); // 输出父类成员变量a的值
System.out.println(this.a); // 输出子类成员变量a的值
}
}
public class Main {
public static void main(String[] args) {
Child child = new Child(10);
child.print();
}
}
```
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)