java——声明一个对象

2019年2月16日19:36:59 发表评论 1,009 views
/**
 * Created by xabcd on 2019/2/16.
 */
public class test_shengming
{
    public static void main(String args[]) {
        star();
        System.out.println("I like Java!");
        star();
    }
        public static void star() {
            for (int i = 0; i < 19; i++) {
                System.out.print("*");
            }
            System.out.print("\n");

            }


    }






*******************
I like Java!
*******************




至于 在 star() 方法 之前 要 加上 static 关键字, 这是 因为 main() 方法 本身 也 声明 成 static, 而在 static 方法 内 只能 访问 到 static 成员 变量( 包括 数据 成员 和 方法 成员) 之故, 因 star() 方法 被 main() 方法 所 调用, 自然 也 要把 star() 声明 成 static 才行。

 


			

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: