码迷,mamicode.com
首页 > 编程语言 > 详细

Java Static关键字之小程序

时间:2014-05-09 13:23:57      阅读:417      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   java   color   

对于学习static我们先来看两个例子:

bubuko.com,布布扣
 1 public class Cat {
 2     private static int sid = 0;
 3     private String name; 
 4     int id;
 5     Cat(String name) {
 6         this.name = name;  
 7         id = sid++;
 8     }
 9     public void info(){
10         System.out.println
11                ("My name is "+name+" No."+id);
12     }
13     public static void main(String arg[]){
14 
15         Cat.sid = 100;
16         Cat mimi = new Cat("mimi");17         Cat pipi = new Cat("pipi");
18         mimi.info(); 
19         pipi.info();
20     }
21 }
bubuko.com,布布扣

运行结果为: My name is mimi No.100      My name is pipi No.101

bubuko.com,布布扣

 

bubuko.com,布布扣
 1 public class Cat {
 2     private  int sid = 0;
 3     private String name; 
 4     int id;
 5     Cat(String name) {
 6         this.name = name;  
 7         id = sid++;
 8     }
 9     public void info(){
10         System.out.println
11                ("My name is "+name+" No."+id);
12     }
13     public static void main(String arg[]){
14 
15        // Cat.sid = 100;
16         Cat mimi = new Cat("mimi");
17         Cat pipi = new Cat("pipi");
18         mimi.info(); 
19         pipi.info();
20     }
21 }
bubuko.com,布布扣

运行结果为: My name is mimi No.0     My name is pipi No.0

bubuko.com,布布扣

 

 

Java Static关键字之小程序,布布扣,bubuko.com

Java Static关键字之小程序

标签:style   blog   class   code   java   color   

原文地址:http://www.cnblogs.com/ouczw/p/3718395.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!