package Demo4;
public class Student {
private int id;
private String name;
private int age;
static String room;
private static int idcount = 0 ;
public Student ( ) {
idcount ++ ;
}
public Student ( String name, int age) {
this . name = name;
this . age = age;
this . id = ++ idcount;
}
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 Demo4;
public class DemoStaticMethod {
public static void main ( String[ ] args) {
MyClass obj = new MyClass ( ) ;
obj. method ( ) ;
obj. methodStatic ( ) ;
MyClass. methodStatic ( ) ;
myMethod ( ) ;
DemoStaticMethod. myMethod ( ) ;
}
public static void myMethod ( ) {
System. out. println ( "自己的方法!" ) ;
}
}
package Demo4;
public class DemoStaticFied {
public static void main ( String[ ] args) {
Student s1 = new Student ( "郭靖" , 19 ) ;
Student s2 = new Student ( "黄蓉" , 18 ) ;
s1. room = "101教室" ;
System. out. println ( "姓名:" + s1. getName ( ) + ", 年龄:" + s1. getAge ( ) + ", 教室" + s1. room + ",学号:" + s1. getId ( ) ) ;
System. out. println ( "姓名:" + s2. getName ( ) + ", 年龄:" + s2. getAge ( ) + ", 教室" + s1. room + ",学号:" + s2. getId ( ) ) ;
}
}
package Demo4;
public class DemoStaticStudent {
public static void main ( String[ ] args) {
Student. room = "101教室" ;
Student one = new Student ( "郭靖" , 18 ) ;
System. out. println ( "one的姓名:" + one. getName ( ) ) ;
System. out. println ( "one的年龄:" + one. getAge ( ) ) ;
System. out. println ( "one的教室:" + Student. room) ;
System. out. println ( "========================" ) ;
Student two = new Student ( "黄蓉" , 16 ) ;
System. out. println ( "two的姓名:" + two. getName ( ) ) ;
System. out. println ( "two的年龄:" + two. getAge ( ) ) ;
System. out. println ( "two的教室:" + Student. room) ;
}
}
package Demo4;
public class MyClass {
int num;
static int numStatic;
public void method ( ) {
System. out. println ( "这是一个成员方法" ) ;
System. out. println ( num) ;
System. out. println ( numStatic) ;
}
public static void methodStatic ( ) {
System. out. println ( "这是一个静态方法" ) ;
System. out. println ( numStatic) ;
}
}
package Demo4;
public class Person {
static {
System. out. println ( "静态代码块执行!" ) ;
}
public Person ( ) {
System. out. println ( "构造方法执行!" ) ;
}
}
package Demo4;
public class DemoStatic {
public static void main ( String[ ] args) {
Person one = new Person ( ) ;
Person two = new Person ( ) ;
}
}