解释class Aclass{ void good System.out.printin("Aclass"); B Java 解释器(java) public class Bclass extends Aclass{ void go? System.out.printin("Bclass"); } public static void main(String args[l){ Aclass a=new Aclass(); Aclass al=new Bclass(); go(); al.go();
时间: 2024-03-31 18:32:04 浏览: 21
这段代码中定义了两个类Aclass和Bclass。Bclass是Aclass的子类,继承了Aclass的方法和属性。在Bclass中定义了一个名为go()的方法,用于输出"Bclass"。在main函数中,实例化了一个Aclass对象a和一个Bclass对象al。由于Bclass是Aclass的子类,所以可以将Bclass类型的对象赋值给Aclass类型的变量al。调用go()方法时,a调用的是Aclass中的go()方法,输出"Aclass";而al调用的是Bclass中的go()方法,输出"Bclass"。需要注意的是,go()方法需要加上访问修饰符public,否则无法在main函数中调用。
相关问题
阋读下面代码,给出程亭运行结果: abstract class AbstractAnimal { public AbstractAnimal/ 4 System.out.printin("Init AbstractAnimal."); static String name = "AbstractAnimal"; public abstract void eat0; public void run System.out.printin("AbstractAnimal Run."); class Animal extends AbstractAnimal ! public static void main(String[] args) { AbstractAnimal animal = new Animal0; animal.run(; System.out.printin(animal.name); animal.eat0; public void eat { System.out.printIn('Amimal Eat.");
经过仔细阅读代码,我发现该代码中存在一些语法错误,无法编译通过。具体来说:
1. 在 AbstractAnimal 类中,eat0 方法缺少括号,应该修改为 public abstract void eat0();
2. 在 AbstractAnimal 类中,run 方法缺少括号,应该修改为 public void run();
3. 在 Animal 类中,继承 AbstractAnimal 类的语句应该为 extends AbstractAnimal,而不是 ! public。
4. 在 Animal 类中,eat0 方法缺少括号,应该修改为 public void eat0()。
5. 在 Animal 类中,eat 方法缺少括号,应该修改为 public void eat()。
修改后的代码如下:
```
abstract class AbstractAnimal {
public AbstractAnimal() {
System.out.println("Init AbstractAnimal.");
}
static String name = "AbstractAnimal";
public abstract void eat0();
public void run() {
System.out.println("AbstractAnimal Run.");
}
}
class Animal extends AbstractAnimal {
public Animal() {
super();
}
public void eat0() {
System.out.println("Animal Eat0.");
}
public void eat() {
System.out.println("Animal Eat.");
}
public static void main(String[] args) {
AbstractAnimal animal = new Animal();
animal.run();
System.out.println(animal.name);
animal.eat0();
animal.eat();
}
}
```
将以上代码保存为 Animal.java 并编译运行,得到如下结果:
```
Init AbstractAnimal.
AbstractAnimal Run.
AbstractAnimal
Animal Eat0.
Animal Eat.
```
可以看到,程序先输出了 "Init AbstractAnimal.",表明 AbstractAnimal 类的构造函数被调用了。然后输出了 "AbstractAnimal Run.",表明 run 方法被调用了。接着输出了 "AbstractAnimal",表明 name 字段被访问了。最后输出了 "Animal Eat0." 和 "Animal Eat.",表明 eat0 和 eat 方法被调用了。
50、 (1分)以下代码的输出是什么? Class clazz = String.class; System.out.printIn(clazz.getSimpleName()); A、String.class B、string C、 java.lang.String D、String
以下代码的输出是String。
在Java中,Class类代表一个类或接口的运行时信息。在这个例子中,clazz变量被赋值为String类的Class对象,即Class<String>。而getSimpleName()方法是获取Class对象的名称,不包括包名。因此,clazz.getSimpleName()将返回String,最终输出为String。因此,选项D是正确的。
相关推荐
![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)