zoukankan      html  css  js  c++  java
  • 不用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;
      }
    }

  • 相关阅读:
    django模型层(二)多表操作
    django模型层(一)单表操作
    模板系统
    HTTP协议web开发知识点
    视图函数
    URL路由系统
    web框架的本质
    MySQL多表查询
    MySQL单标查询
    列表渲染
  • 原文地址:https://www.cnblogs.com/shouhutian/p/5966162.html
Copyright © 2011-2022 走看看