主旨:面向对象的思想
static 方法:不需要对象,static表示这个类的属性,可以理解为类中的全局变量
构造块:类中用一对大括号包含起来的,就叫构造快。每创建一个对象前,都执行一次构造快,构造块要先于构造函数执行。
{
count++;
}
静态代码块:只执行一次。创建第一个对象之前,执行,并且只执行一次。
static { }
Oop5.java代码:class Person { static int count; String name; int age; String getName() { return "guangdong "+name; } static { System.out.println("static block"); } { System.out.println("construct block"); count ++; } /* construct method */ public Person () { System.out.println("construct method: Person 1"); name = "null"; age = 0; } public Person (String name) { System.out.println("construct method: Person 2"); this.name = name; } public Person (String name, int age) { System.out.println("construct method: Person 3"); this.name = name; this.age = age; } static void printPerson () { System.out.println("This is a class of Person"); } }; public class Oop5 { public static void main(String args[]) { Person p1 = new Person("zhangsan"); Person p2 = new Person("lisi"); Person p3 = new Person(); Person p4 = new Person("wangwu", 6); System.out.println(p1.getName()); System.out.println(p2.getName()); System.out.println(p3.getName()); System.out.println(p4.getName()); Person.printPerson(); System.out.println(Person.count); } }