Java面向对象编程(2)-类变量,类方法

来源:互联网 发布:现在还能翻墙的软件 编辑:程序博客网 时间:2024/05/20 21:20

Java面向对象编程(2)-类变量,类方法

问题:一群小孩子玩堆雪人,不是有新的小孩子加入,请问如何知道现在有多少人在玩?请使用面向对象的思想,编写程序解决。

 一般做法

publicclassDemo9_17 {

    publicstaticvoid main(String[]args)

     {

      //一般的做法

      inttotal= 0;

      

      Childch1 =newChild(3,"小妞");

      ch1.joinGame();

      total++;

      Childch2 =newChild(4,"小小");

      ch2.joinGame();

      total++;

   

     }

}

 

 

//定义一个小孩的类

classChild

{

   intage;

   String name;

   publicChild(intage,Stringname)

   {

    this.age =age;

    this.name =name;

   }

   

   publicvoidjoinGame()

   {

    System.out.println("有一个小孩加入");

   }

}

 

如果,设计一个int total 表示总数,我们在创建一个小孩时,就把total加1,并且total是所有对象共享的。

 

publicclassDemo9_17 {

    publicstaticvoid main(String[]args)

     {

     

            Child ch1 = new Child(3,"小妞");

           ch1.joinGame();

 

           Child ch2 = new Child(4,"小小");

           ch2.joinGame();

           

           System.out.println("共有" +ch2.total +"");

           

           //随便用哪个对象访问这个变量,都是可以的。

           System.out.println("共有" +ch1.total +"");

   

     }

}

 

 

//定义一个小孩的类

class Child

{

   intage;

   String name;

  

   //total是静态变量,因此他可以被任何对象访问

   

  staticinttotal= 0;

   

   publicChild(intage,Stringname)

   {

    this.age =age;

    this.name =name;

   }

   

   publicvoidjoinGame()

   {

    total++;

    System.out.println("有一个小孩加入");

   }

}

 

 

什么是类变量?

是该类的所有对象共享的变量,任何一个该类的对象都是可以访问的,而且取出来的值是一

样的。

同样的任何一个该类的对象去修改它时,修改的也是同一个变量。

 

如何定义一个类变量

定义语法:

访问修饰符 static 数据类型变量名

 

如何访问类变量

类名.类变量名或者 对象名.类变量名

 

 

对静态变量的理解

publicclassDemo9_17_1 {

   

    //静态变量

    staticinti = 1;

   

    //静态代码区,只会执行一次

   static

   {

    System.out.println("执行静态代码");

    i++;

   }

   

   //构造方法还是会执行。

   publicDemo9_17_1()

   {

       System.out.println("执行方法");

    i++;

   }

 

    publicstaticvoid main(String[]args)

    {

        Demo9_17_1t1 =new Demo9_17_1();

        System.out.println(t1.i);

       

        Demo9_17_1t2 =new Demo9_17_1();

        System.out.println(t2.i);

       

    }

}

 

类方法

什么是类方法?为什么有类方法

类方法是属于所有对象的实例,其形式如下:

访问修饰符 static 数据返回类型方法名()

 

注意:类方法中不能访问非静态变量(类变量)

使用:类名.类方法名或者  对象名.类方法名

 

案例:统计学费的和

publicclassDemo9_17_1 {

   

   

    publicstaticvoid main(String[]args)

    {

       

       

        Stustu1=newStu(23,"小小",240);

        System.out.println(stu1.Gettotal());

       

        Stustu2=newStu(23,"大大",245);

       

        System.out.println(stu2.Gettotal());

               

       

    }

}

 

class Stu

{

   intage;

   String name;

   intfee;

   

   //所有学生共享变量

   staticinttotalFee= 0;

   

   publicStu(intage,Stringname,intfee)

   {

     this.age=age;

     this.name=name;

     

     totalFee +=fee;

   

   }

   

   public staticint Gettotal()

   {

    returntotalFee;

   }

}

 

每个类调用方法时,都会占用内存,所以加static,可以所有的对象都访问,节省空间

 

规则:静态变量(类变量)。原则上用类方法去访问或是去操作。

 

类变量总结

1, 什么时候需要用类变量

2, 类变量和实例变量的区别

加上static称为类变量或者是静态变量,否则称为实例变量

类变量是与类相关的,公共的属性

实例变量属于每个对象个体的属性

类变量可以通过类名.类变量名直接访问。

 

类方法总结

1, 什么时候需要用类方法

2, 类方法属于类相关的,公共的方法。

实例方法属于每个对象个体的方法

类方法可以通过类名.类方法直接访问。

0 0
原创粉丝点击