package BlockDemo;
public class BlockTest {
public static void main(String[] args) {
String s=Person.desc;
new Person();
}
}
class Person{
String name;
int age;
static String desc="我是一个人";
public Person() {
}
public Person(String name,int age) {
this.name=name;
this.age=age;
}
static {
System.out.println("hello,static block");
}
{
System.out.println("hello,非static block");
}
public void eat() {
System.out.println("吃饭");
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
public static void info() {
System.out.println("我爱小徐");
}
}
package BlockDemo;
public class LeafTest {
public static void main(String[] args) {
new Leaf();
new Leaf();
}
}
class Root{
static {
System.out.println("Root的静态代码块");
}
{
System.out.println("Root的非静态代码块");
}
public Root() {
System.out.println("Root的无参构造器");
}
}
class Mid extends Root{
static {
System.out.println("Mid的静态代码块");
}
{
System.out.println("Mid的非静态代码块");
}
public Mid() {
System.out.println("Mid的无参构造器");
}
public Mid(String s) {
this();
System.out.println("Mid 的带参构造器,参数值为:"+s);
}
}
class Leaf extends Mid{
static {
System.out.println("Leaf 的静态代码块");
}
{
System.out.println("Leaf的非静态代码块");
}
public Leaf() {
super("xt");
System.out.println("Leaf的构造器");
}
}