定义类Block和类Demo,运行Demo的输出结果是。
class Block{
{//代码1
System.out.print("1");
}
static{//代码2
System.out.print("2");
}
{//代码3
System.out.print("3");
}
public Block()
{//代码4
System.out.print("4");
}
static void show()
{//代码5
System.out.print("5");
}
static {//代码6
System.out.print("6");
}
}
//定义测试类Demo
public class Demo {
public static void main(String[] args) {
new Block().show();
new Block()
}
}
class Block{
{//代码1
System.out.print("1");
}
static{//代码2
System.out.print("2");
}
{//代码3
System.out.print("3");
}
public Block()
{//代码4
System.out.print("4");
}
static void show()
{//代码5
System.out.print("5");
}
static {//代码6
System.out.print("6");
}
}
//定义测试类Demo
public class Demo {
public static void main(String[] args) {
new Block().show();
new Block()
}
}
举一反三
- (7-1)以下程序的运行结果是( )。 class A{ A(){ System.out.print(10); } } public class Demo extends A { public static void main(String[] args) { new A(); new Demo(); } }
- (6-7)下面()代码段属于普通代码块。 classBlock{ {//代码1 System.out.print("1"); } static{//代码2 System.out.print("2"); } {//代码3 System.out.print("3"); } publicBlock() {//代码4 System.out.print("4"); } staticvoidshow() {//代码5 System.out.print("5"); } static{//代码6 System.out.print("6"); } }
- import java.io.*; class Person{ public void print(){System.out.print("Person ");} public void printMyGender(String s){ this.print(); System.out.print(s+" "); } } class Gender{ String type="gender"; public void print(Person p){p.printMyGender(type);} } class Female extends Gender{ public Female(){ type="female"; } } class Male extends Gender{ public Male(){ type="male"; } } class Employee extends Person{ public void print(){ System.out.print("Employee ");} } class Manager extends Employee{ public void print(){ System.out.print("Manager ");} } public class Test{ public static void main(String[] args){ Manager man = new Manager(); Employee em = new Employee(); Gender gender1 = new Male(); Gender gender2 = new Female(); gender1.print(man); gender2.print(em); } } 对于以上代码,其运行结果是
- 中国大学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 Test public static void main ( String [ ] args ) int count = 0 for( int i = 1 i < 5 i = 2) for( int j = 1 j< = 10 j = 3) count System .out .print (count ) _