Java Road from entry to advanced of (XII)

In the previous article we introduce a bit and rewrite the difference between Java classes and overloaded this chapter we look at the Java class private, static, final.

Before we introduced the concept of Java class when the cash register is introduced through the mall, as follows:

If we were using a form of credit card payment, we need to show a sufficient balance of bank card or membership card to make a payment when the payment is done we only pay by credit card, enter a password, in the process, cashier can not see these privacy of our data card number, password, balance, or it may occur stolen brushes or other problems, the cashier at the cash register when the only thing to do is when we enter the original password with the password we enter whether for horses and password when the account balance is greater than equal to the price of goods on it.

According to the above analysis we can define a class Card:

 

 1 class Card{
 2     private String cartId; // 卡号
 3     private String cardPwd; // 密码
 4     private double balance; // 余额
 5     public boolean payMoney(double money){ // 支付
 6         if(balance >= money){
 7             balance -= money;
 8             return true;
 9         }
10         return false;
11 
12     }
 13 is      public  Boolean checkPwd (String pwd) { // check the password 
14          IF (cardPwd.equals (pwd)) {
 15              return  to true ;
 16          }
 . 17          return  to false ;
 18 is      }
 . 19 }

In the above code, we will pre-qualifier variables written private, before the qualifiers method written public. Next we look at the difference between private and public.

modification can only be called from private member variables and methods in this class, public-modified member variables and methods can be called from anywhere.

private content is modified to achieve internal packaging, public content is modified to provide the external function can be called.

There are also two: protected by default and do not write, we call access control modifiers, their control are:

1) public: disclosed, any class

2) private: private, this class

3) protected: protected, this class, subclass, with bags

4) The default (nothing to write): This class, with bags

 

 

Next we look at the keyword static.

Before we can actually be called instance variables, there is a variable called a static variable in the class definition of variables, namely modified with the static keyword. Let's look at the differences between the two variables:

1, instance variables:

 1) belonging to the object, the presence of the counterweight

 2) There are a few objects have several instance variables

 3) must. Be accessed through the object name

2, static variables:

 1) belong to a class, there is a method in region

 2) Only one

 3) must. Be accessed through the class name

Through the following code to actually look at:

 1 public class HelloWorld {
 2     public static void main(String[] args) {
 3         Aoo aoo1 = new Aoo();
 4         aoo1.show(); // a=1 b=1
 5 
 6         Aoo aoo2 = new Aoo();
 7         aoo2.show(); // a=1 b=2
 8     }
 9 }
10 
11 class Aoo {
12     int a;
13     static int b;
14 
15     Aoo() {
16         a++;
17         b++;
18     }
19 
20     void show() {
21         System.out.println("a=" + a);
22         System.out.println("b=" + b);
23     }
24 }

In the above code, we define the instance variable and a static variable b, then instantiate Aoo twice, then call after call show Aoo instantiated by two () method can be seen that we call instance variables instantiated the value of a constant, that is, each instance will be copied once a a, b and static variables are instantiated once after each value change, ie after instantiated and will not re-copy a b, but continue to use the last .

 

Next we look at a static method static keyword.

Static methods and static variables above are substantially the same, but there are also special place.

 1) belong to a class, there is a method in region

 2) Only one

 3) must. Be accessed through the class name

 4) there is no implicit this transfer, the method can not directly access the static instance variables

. 1  class Aoo {
 2      int A; // instance variables of an object --- access point 
. 3      static  int B; // static class variable names --- access point 
. 4  
. 5      void test1 () { // Examples of methods 
. 6          A ++ ;
 . 7          B ++ ;
 . 8      }
 . 9  
10      static  void test2 () { // static method 
. 11          A ++; // compile error 
12 is          test1 (); // compile error 
13 is          B ++ ;
 14     }
15 }

In the above code, we pass the variable b becomes static keyword static variables, the test2 () method becomes static, when we test1 () instance method and a ++ b ++, the system will default we write this .a ++ and Aoo.b ++; in test2 (), since the this implicit method is not static, and so a ++ test1 () method does not.

 

Next we look at a static block static keyword.

. 1  public  class the HelloWorld {
 2      public  static  void main (String [] args) {
 . 3          Aoo = aoo1 new new Aoo (); // static block constructor 
. 4          Aoo = aoo2 new new Aoo (); // constructor 
5      }
 6  }
 7  
. 8  class Aoo {
 . 9      static {
 10          System.out.println ( "static block" );
 . 11      }
 12 is  
13 is      Aoo () {
 14          System.out.println ( "constructor" );
15     }
16 }

In the above code, we create a Aoo in a constructor, and by   static {}  creates a static block, we instantiate Aoo two classes, we found only one block loading static, and the static method each instantiation once loaded once.

Static blocks can be loaded in the practical application of static resources images, audio, video, etc., such as when we visit Taobao image is loaded only once, not every person synonyms Taobao station once loaded, the server can not stand the pressure.

 

Next we look at the final keyword.

1, final modifications member variables initialized in two ways:

  Colleagues 1) initialization statement

  2) constructor initializes

2, final modification of local variables, initialized before use as long as you can.

code show as below:

. 1  class Aoo {
 2      int A = 10 ;
 . 3      int B;
 . 4      Final  int C = 10; // declare initialized concurrently
 . 5      //     Final int D; // compile error statement uninitialized 
. 6      Final  int E;
 . 7  
. 8      Aoo () {
 . 9          E = 10; // constructor initializes 
10      }
 . 11  
12 is      void Test () {
 13 is          Final  int F; // local variables: prior to assignment to use, do not deliberately assignment 
14          A = 20 is ;
15  //         C = 20 is; // compile error, final modified variables can not be changed 
16      }
 . 17  
18 is }

 

final modification methods: final modification methods can not be overridden.

code show as below:

. 1  class Aoo {
 2      void Test () {}
 . 3      Final  void Show () {}
 . 4  }
 . 5  
. 6  class Boo the extends Aoo {
 . 7      void Test () {}
 . 8      void Show () {} // compile error, final method can not is rewritten 
9 }

 

Modified class final: final modified class can not be inherited, but can inherit from other classes

code show as below:

. 1  class Aoo} {
 2  class Boo the extends Aoo {}
 . 3  Final  class Coo the extends Aoo {} // Final modified class can inherit other classes 
. 4  
. 5  Final  class Doo {}
 . 6  class EOO the extends Doo {} // compile error, final modified class can not be inherited

Guess you like

Origin www.cnblogs.com/weijiutao/p/11188532.html