java方法与构造
public class StaticClass {
	  static int i = 50;
	     int y;
	  // 构造只能有访问修饰符public、protected、private 修饰 (访问修饰符也可以不用),不能出现static、final、
	  // 构造必须和类名一样
	   // 最简单的构造 方法没有这样的格式
	  StaticClass() {
}
	  // 方法名可以和类名一样(首字母大小写区别)方法名首字母一般小写、
	  public int StaticClass(int y) {
		    return y;
	  }
	  // 方法访问修饰符、static、final、synchronized等可以任意调换顺序 然后是void、intd等+方法名。
	  static final public synchronized void StaticClass() {
}
	  // 最简单的方法组成也要有 返回值类型+方法名
	  String StaticClass(Integer x) {
		    return null;
}
	  // 普通方法
	  public void print1() {
		  // 实例化对象
		  StaticClass st = new StaticClass();
		  // 非静态方法中可以直接调用该类的一切方法(静态和非静态的),调用方式有以下三种:1、直接方法名 2、this.方法名 3、对象.方法名
		  // 4、类名.方法名(4适用于静态方法)
		  // this 只能用在非静态的方法调用中 (用在静态方法中会报错) 对象可以调用一切方法
		  // 静态的方法调用顺序 最好4、类名.方法名(首选)>1、直接方法名>2、this.方法名 > 3、对象.方法名 (因为4和1没有警告
		  // 2和3有警告 ) 因此首选 类名.方法名
		    print3();
		    this.print3();
		    print2();
		    this.print2();
		    print4();
		    this.print4();
		    StaticClass.print4();
		    st.print1();
		    st.print2();
		    st.print3();
		    st.print4();
}
	  // 普通方法
	  private void print3() {
		    Integer a = 100;
		    System.out.println("普通方法" + a);
	  }
	  // 静态方法
	  public static void print2() {
		  // 静态方法 中可以直接调用静态方法,若调用非静态方法必须用对象调用 而且静态方法中不能用this .
		  // 静态的方法调用顺序 最好4、类名.方法名(首选)>1、直接方法名> 3、对象.方法名 (因为4和1没有警告 3有警告 ) 因此首选
		  // 类名.方法名
		    print4();
		    StaticClass.print4();
		    StaticClass st = new StaticClass();
		    st.print1();
		    st.print2();
		    st.print3();
		    st.print4();
	  }
	  // 静态方法
	  public static void print4() {
		    System.out.println("静态方法");
	  }
	  public static void main(String[] args) {
		    StaticClass st = new StaticClass();
		    print2();
}
}
 
                    
                
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号