不用static,巧用对象.方法调用java中的函数

先生成一个对象,用“对象.方法()”的方式调用。
java中的main方法是静态的,用于程序的入口,在静态方法中无法调用非静态方法,只能调用静态方法。想调用静态方法的话就要先生成该类的一个对象,通过对象调用非静态方法。
如:
public class SquareIntTest {

 public static void main(String[] args) {
  int result;
  SquareIntTest m = new SquareIntTest();
  for (int x = 1; x <= 10; x++) {
   result = (int)m.Square(x);
   // Math库中也提供了求平方数的方法
   // result=(int)Math.pow(x,2);
   System.out.println("The square of " + x + " is " + result + " ");
  }
 }

 // 自定义求平方数的静态方法
  public int Square(int a)
  {
   return a*a;
  }
}

原文地址:https://www.cnblogs.com/shouhutian/p/5966162.html