读下面程序,写出程序运行结果。 class Father { int num1=0; public Father(){ System.out.println("num1:"+num1);} public Father(int num1){ this.num1=num1; System.out.println("num1:"+num1); } } class Son extends Father{ int num2=10; public Son(){System.out.println("num2:"+num2);} public Son(int i,int j){ super(i); num2=j; System.out.println( "num1:"+num1+",num2:"+num2); } } public class Demo{ public static void main(String []args){ Father father=new Father(); Son son=new Son(); Son son1=new Son(20,30); } }
时间: 2024-03-26 16:35:44 浏览: 52
程序运行结果如下:
```
num1:0
num1:0
num1:20
num2:10
num1:20,num2:30
```
解析:
1. 首先执行 `Father` 类的无参构造方法,输出 `num1:0`。
2. 接着执行 `Son` 类的无参构造方法,先隐式调用 `super()` 方法调用 `Father` 类的无参构造方法,输出 `num1:0`,然后输出 `num2:10`。
3. 最后执行 `Son` 类的有参构造方法,显式调用 `super(i)` 方法调用 `Father` 类的有参构造方法,输出 `num1:20`,然后输出 `num1:20,num2:30`。
相关问题
public class VariableDemo { int a = 111; final int b = 222; public void display() { System.out.println("执行display()"); int a = 333; System.out.println(a); System.out.println(this.a); System.out.println(b); System.out.println(this.b); } public static void main(String[] args) { // TODO Auto-generated method stub int a = 444; System.out.println("执行main()"); System.out.println(a); //System.out.println(this.a); {// 代码块1 int c = 666; System.out.println(c); } System.out.println(c); {// 代码块2 int d = 666; System.out.println(d); } int d = 888; System.out.println(d); VariableDemo vd = new VariableDemo(); vd.display(); vd.b = 999; System.out.println(vd.b); } }
该代码存在以下问题:
1. 在`main`方法中,不能直接访问非静态变量`a`和`b`,因为它们是属于对象的,必须先创建一个类的实例才能访问。
2. `final`修饰的变量`b`不能被修改,所以在`vd.b = 999;`中会编译错误。
3. 在代码块1中定义的变量`c`只能在代码块内部访问,超出代码块范围后就不能再访问了。在`System.out.println(c);`处会编译错误。
4. 变量名`d`在代码块2和`main`方法中都出现了,这是不合法的,因为在同一级作用域中变量名不能重复。
修正后的代码如下:
```
public class VariableDemo {
int a = 111;
final int b = 222;
public void display() {
System.out.println("执行display()");
int a = 333;
System.out.println(a);
System.out.println(this.a);
System.out.println(b);
System.out.println(this.b);
}
public static void main(String[] args) {
int a = 444;
System.out.println("执行main()");
System.out.println(a);
{
int c = 666;
System.out.println(c);
}
// System.out.println(c); // 编译错误
{
int d = 666;
System.out.println(d);
}
int d = 888;
System.out.println(d);
VariableDemo vd = new VariableDemo();
vd.display();
// vd.b = 999; // 编译错误
System.out.println(vd.b);
}
}
```
修正后的代码中,将`vd.b = 999;`注释掉,因为`b`是用`final`修饰的,不能被修改。同时,变量名重复的问题也得到了修正。
写出下列程序运行结果:class Base{ Base() { int i = 100; System. out.println(i) ; } } public class Pri extends Base{ static int i = 200; public static void main(Stringargv[]){ Pri p = new Pri() ;System. out. println(i); } }
该程序的运行结果为:
```
100
200
```
程序的执行步骤如下:
1. 运行main方法。
2. 创建`Pri`类的对象`p`,由于`Pri`类继承自`Base`类,所以首先会调用`Base`类的构造方法。
3. 在`Base`类的构造方法中,定义了一个整型变量`i`并初始化为100,然后将其打印输出。
4. `Base`类的构造方法执行完毕后,继续执行`Pri`类的构造方法。
5. 在`Pri`类中定义了一个静态整型变量`i`并初始化为200,然后将其打印输出。
6. 程序执行完毕,输出结果为`100`和`200`。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](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)