1.(静态代码块)
class Student{
static{System.out.println("Student的静态代码块1") ;}
public Student(){
System.out.println("Student的无参构造方法") ;
}
{System.out.println("Student的构造代码块1") ;}
{System.out.println("Student的构造代码块2") ;}
public Student(String str){
System.out.println("Student的有参构造方法") ;
}
static{System.out.println("Student的静态代码块2") ;}
}
class StudentTest{
static{System.out.println("高圆圆已经42了,我很伤心") ;}
public static void main(String[] args){
Student s = new Student() ;
Student s2 = new Student("hello") ;
}
}
高圆圆已经42了,我很伤心
Student的静态代码块1
Student的静态代码块2
Student的构造代码块1
Student的构造代码块2
Student的无参构造方法
Student的构造代码块1
Student的构造代码块2
Student的有参构造方法
2.(this super)
需要在控制台分别打印30,20,10
class Test{
public static void main(String[] args){Zi zi = new Zi();
zi.show() ;
}
}class Fu{
int num = 10;
}
class Zi extends Fu{
int num = 20 ;
public void show(){
int num = 30 ;
//补全代码
System.out.println(num);//30
System.out.println(this.num);//20
System.out.println(super.num);//10
}
}
3.(继承、对象构造过程)
有以下代码
class Meal{
public Meal(){
System.out.println("Meal()");
}
}
class Lunch extends Meal{
public Lunch(){
System.out.println("Lunch()");
}
}
class Vegetable {
public Vegetable(){
System.out.println("Vegetable()");
}
}
class Potato extends Vegetable{
public Potato(){
System.out.println("Potato()");
}
}
class Tomato extends Vegetable{
public Tomato(){
System.out.println("Tomato()");
}
}
class Meat{
public Meat(){
System.out.println("Meat()");
}
}
class Sandwich extends Lunch{
Potato p = new Potato();
Meat m = new Meat();
Tomato t = new Tomato();
public Sandwich(){
System.out.println("Sandwich()");
}
}
public class TestSandwich{
public static void main(String args[]){
Sandwich s = new Sandwich();
}
}
写出这段代码的输出结果。
Meal()
Lunch()
Vegetable()
Potato()
Meat()
Vegetable()
Tomato()
Sandwich()
4.
看程序,写结果:(请说明原因)
class Root {
static {
System.out.println("Root的静态初始化块");
}
{
System.out.println("Root的普通初始化块