(06-03)下面Java代码的运行结果是。
class Penguin {
private String name=null; // 名字
private int health=0; // 健康值
private String sex=null; // 性别
public void Penguin() {
health = 10;
sex = "雄";
System.out.println("执行构造方法。");
}
public void print() {
System.out.println("企鹅的名字是" + name + "健康值是" + health + "性别是" + sex+ "。");
}
public static void main(String[] args) {
Penguin pgn = new Penguin();
pgn.print();
}
}
class Penguin {
private String name=null; // 名字
private int health=0; // 健康值
private String sex=null; // 性别
public void Penguin() {
health = 10;
sex = "雄";
System.out.println("执行构造方法。");
}
public void print() {
System.out.println("企鹅的名字是" + name + "健康值是" + health + "性别是" + sex+ "。");
}
public static void main(String[] args) {
Penguin pgn = new Penguin();
pgn.print();
}
}
举一反三
- 【单选题】(06-03) 下面 Java 代码的运行结果是 ( ) 。 class Penguin { private String name=null; // 名字 private int health=0; // 健康值 private String sex=null; // 性别 public void Penguin() { health = 10; sex = " 雄 "; Syst
- class Person{ static{ System.out.println(name); } private static String name = "hello"; } class Demo{ public static void main(String[] args){ Person p = null; } }
- 下列程序的运行结果是( )class Demo{private String name;Demo(String name){this.name = name;}private static void show(){System.out.println(name)}public static void main(String[] args){Demo d = new Demo(“lisa”);d.show();}}
- 中国大学MOOC: 下列代码中构造方法的返回类型是()public class Village { Village () { System .out .println(“hiding in Village”) ; } public static void main( String args [ ]) { Village c =new Village ( ) ;}class Village { public static void main( String args [ ]) { Village c =new Village ( ) ; } Village () { System .out .println(“hiding in Village”) ; } }
- 【填空题】public class ThisTest{ private String name; private int age; public ThisTest() { System.out.println("产生一个新的Person对象。"); } public ThisTest (String name, int age) { this(); this.name = name; this.age = age; } public String getInf() { return "姓名:" + name + ",年龄:" + age; } public static void main(String[] args) { ThisTest per = new ThisTest("张三", 20); System.out.println(per.getInf()); } } 运行结果为:____