public class d3 { public static void main(String[] args) { // TODO Auto-generated method stub Base1 a = new Base1(); Sub1 b = new Sub1(); Base1 c = new Sub1(); Base1 d = new Sub2(); System.out.println(a.getN()); System.out.println(b.getN()); System.out.println( c.getN()); System.out.println( d.getN()); } } class Base1 { int getN() { return 2; } } class Sub1 extends Base1 { int getN() { return 3; } } class Sub2 extends Base1 { int getN() { return 4; } }
时间: 2024-01-27 21:06:21 浏览: 33
这段Java代码中,输出的结果是:
```
2
3
3
4
```
解释如下:
在`main`方法中,分别创建了`Base1`、`Sub1`和`Sub2`三个对象,并将它们赋值给`a`、`b`、`c`和`d`四个变量。
对于`a`对象,它是`Base1`类型的,它的`getN()`方法返回值为2,因此输出2。
对于`b`对象,它是`Sub1`类型的,它的`getN()`方法被重写了,返回值为3,因此输出3。
对于`c`对象,它是`Sub1`类型的,但是被赋值给了`Base1`类型的变量,因此调用的是`Base1`类中的`getN()`方法,返回值为2,因此输出2。
对于`d`对象,它是`Sub2`类型的,但是也被赋值给了`Base1`类型的变量,同样调用的是`Base1`类中的`getN()`方法,返回值为4,因此输出4。
相关问题
纠错public class Test1_2 { void showData(){ System.out.println("中国,我爱你!"); } public static void main(String[] args) { // TODO Auto-generated method stub showData(); } }
在这个程序中,showData() 方法被定义为非静态方法,因此需要通过实例化对象来调用该方法。在 main() 方法中,没有创建对象就直接调用了 showData() 方法,导致编译错误。
正确的代码应该是这样的:
```
public class Test1_2 {
void showData(){
System.out.println("中国,我爱你!");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Test1_2 test = new Test1_2();
test.showData();
}
}
```
在 main() 方法中,首先创建了 Test1_2 的对象 test,然后通过 test 调用了 showData() 方法。这样就可以正确地输出 "中国,我爱你!" 了。
public class Text1 { public static void main(String[] args) { // TODO Auto-generated method stub th1 t1 = new th1("芜湖"); t1.start(); th2 t2 = new th2("芜湖2"); t2.start(); } } class th1 extends Thread { String name; public th1(String name) { this.name = name; } @Override public void run() { // TODO Auto-generated method stub System.out.println(Thread.currentThread().getName() + ":" + name); } } }
这也是一个Java程序,其中定义了一个名为"th1"的线程类和一个名为"Text1"的主类。在主类中,创建了两个线程对象,一个是"th1"对象,一个是"th2"对象,并启动它们。"th1"线程类中,定义了一个名为"name"的字符串变量和一个构造函数,构造函数将传入的参数赋值给"name"变量。在"run"方法中,输出当前线程的名称和"name"变量的值。
但是这段代码中有一个多余的"}",需要将其删除。除此之外,程序运行可能会存在的问题取决于您的具体需求和执行环境。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)