Java Fundamentals Chapter 8 (Invocation and Encapsulation of Static Members)

First, two types of calls to static members.

     1. object.member variable = value;

         例:stu1 . name = "java";

     2. Class name. Member method; // You can directly call in this way without new, and you can also call the method (Student . getNum())

         例:Student .count++;

         Note:

               Static methods can only access static variables, and static can only access static.

               Non-static methods can access both static and non-static variables.

        例1:class Student{

                      static int count;

                      public int getNum(){ //Non-static methods can call both static variables and non-static variables

                          count++;

                          return count;

                      }

                 }

                 The main program call must first create an object before it can be called (because the getNum() method is non-static, it is not shared, and the object can be called only if the object is new)

                 Student stu1 = new Student();

                 int count1 = stu1.getNum();

         例2:class Student{

                       static int count;

                       public static int getNum(){

                           count++;

                           return count;

                       }

                  }

                 The main program calls int count2 = Student.getNum();

Second, the static block.

    static{

 

    }

    Static blocks can only be loaded and run once, first.

   例:class Student{

              public static int count;

              static{

                   count++;

              }

              public void studentNum(){

                   System.out.println(count);

              }

          }

         Main program call: new Student().studentNum(); // 1

                               new Student().studentNum(); // 1

Three, packaging.

    1. The function of the package code:

       Only provide a name to the outside world, so that the outside world can access these properties or functions.

    2. Example: Bank Security

        class creditCard{

            private double salary; // Salary is not accessible from the outside world, encapsulated

            public double getSalary(){

                return salary;

            }

           public void setSalary(double sal){

               if(sal <= 0){

                   return 0.0;

               }else{

                        salary = sal;

               }

              return salary;

           }

        }

        public class money{

            public static void main(String[] args){

                creditCard cc = new creditCard();

                cc.setSalary(100000000);

                double salary = cc.getSalary();

            }

        }

        Note: private is private, only owned by yourself and cannot be accessed by the outside world.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325298055&siteId=291194637