package cn. itcast. day09. demo01;
public class Student {
private String name;
private int age;
static String room;
private int id;
private static int idCounter = 0 ;
public Student ( ) {
this . id = ++ idCounter;
}
public Student ( String name, int age) {
this . name = name;
this . age = age;
this . id = ++ idCounter;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public int getAge ( ) {
return age;
}
public void setAge ( int age) {
this . age = age;
}
public int getId ( ) {
return id;
}
public void setId ( int id) {
this . id = id;
}
}
package cn. itcast. day09. demo01;
public class Demo01StaticField {
public static void main ( String[ ] args) {
Student one = new Student ( "gj" , 20 ) ;
one. room = "101" ;
System. out. println ( "name:" + one. getName ( ) + " age:" + one. getAge ( ) + " room:" + one. room) ;
System. out. println ( "id is:" + one. getId ( ) ) ;
Student two = new Student ( "hr" , 21 ) ;
System. out. println ( "name:" + two. getName ( ) + " age:" + two. getAge ( ) + " room:" + one. room) ;
System. out. println ( "id is:" + two. getId ( ) ) ;
}
}
package cn. itcast. day09. demo01;
public class Demo02StaticMethod {
public static void main ( String[ ] args) {
MyClass obj = new MyClass ( ) ;
obj. method ( ) ;
obj. methodStatic ( ) ;
MyClass. methodStatic ( ) ;
myMethod ( ) ;
}
public static void myMethod ( ) {
System. out. println ( "自己的方法" ) ;
}
}
package cn. itcast. day09. demo01;
public class MyClass {
public void method ( ) {
System. out. println ( "这是成员方法" ) ;
}
public static void methodStatic ( ) {
System. out. println ( "这是静态方法" ) ;
}
}
package cn. itcast. day09. demo01;
public class Person {
static {
System. out. println ( "静态方法被调用" ) ;
}
public Person ( ) {
System. out. println ( "构造方法执行" ) ;
}
}
package cn. itcast. day09. demo01;
public class Demo03Static {
public static void main ( String[ ] args) {
Person one = new Person ( ) ;
Person twp = new Person ( ) ;
}
}