Java study notes of object-oriented, static keyword

One week Java learning summary

Summarize today to sort out what some of the different characteristics on object-oriented and process-oriented programming, as well as speaking at the static keyword.

Object-Oriented

  • Java is now in contact with an object-oriented, and now the developers are almost all object-oriented basis. I do not read books, memories alone to think about this feature interpretive language, can only think of very profound words when reading, object-oriented is a more detailed description of a design method of an event, like eating inside the company, Object-oriented, then that is a total that is similar to the person in charge of what the chef is ready recipes, go to a subordinate division of labor, each person is responsible for a dish, each person according to their own recipe to complete, the final total of the Feast table made out of form. Which food is not good, and that can be found directly responsible for inspection. This design can have the versatility and modular standards, efficiency and sometimes higher point.
  • Object-oriented programming on three features: encapsulation, inheritance, polymorphism. Encapsulation is the first object properties, behavior as an inseparable whole package in separate units, followed by an information hidden feature is inherited: expression and description of the parent class and subclass is a kind of inheritance, have reflected the general characteristics of the class of things, derived classes reflect the special things on its basis. (Class is composed of attributes and methods) is finally polymorphism, an object comprising a method overloading and polymorphism. Child and parent class may be interchangeable, depending on the completion of its subclasses using different functions.
  • Objects and classes are Java support from the basic concepts of object-oriented development unit. Template object class is actually operated, can not be directly used, should be used by way of example. To achieve a variety of dress up like a hair (category) means of the human (subject).

The static keyword

"Static method is not this way. You can not call non-static method inside the static method, in turn, is possible. And you can not create any objects under the premise, only to call the static method through the class itself. This is in fact what the main purpose of static methods. " -" the Java programming ideas "

  • Talk about the static keyword does not affect the scope of a variable or method. And c / c ++ is different from static. In the C / C ++ is the static scope of local variables, but in Java Remember: static is not allowed to modify local variables.

  • Java variables into local variables and member variables; local variables further divided into three types: parameter, local variables in a method, local variables (see the name within the scope of the blocks will be appreciated that, as in the code block, begin to fail after the end of the die block). Member variables: class within the definition of variables, including the non-static variable, also known as instance variables, and static variables, also known as class variables.
  • static literally means static; but in a Java program, it is a sign that role is an instance member becomes a member of the class. And only the modified portion of the members defined in the class, including fields, methods, inner classes, initialization block. Static class without modification to these members, they belong to instances of the class.
    eg: static int num2 = 20; ( which is a class variable)
    EG: int num1 = 238; (instance variables)
  • modified static members belong to class A, class variables are initialized with the initialization; without modified static member variable belongs example, instance variables initialized with the initialization of the object. Because before an object before initializing, certainly you must first initialize the class the object belongs, so the timing is certainly the more static modification of instance variables early.
  • static method generally referred to as the static method does not rely on any object can be accessed, so for static methods, there is no in this. Note You can not access non-static member methods and member variables in a static method. As shown below
  • printIn method here call getInfo () to be wrong, because static can not call non-static. There will be error "Can not make a static reference to the non-static method getInfo () from the type BookJava (603979977)", which in English is also well understood.

Here Insert Picture Description

The following example of a insert, have to understand better static

 public class statictest {//考察这个代码的打印情况    
 //3 第三步    
 Person person = new Person("Test");    
 //这里是第一个person参数:test 5运行完4后就开始通过构造器来生成对象,这里Person没有加载过,去找Person   
  static {    
  //3有static块,直接运行这一步。所以一开始打印这一句        
        System.out.println("test static");    }
    public statictest() { 
                System.out.println("test constructor");
               //7打印第五句    }
    public static void main(String[] args) {
        //1主方法入口,第一先读取这里 
               new MyClass();  
             }
       }
class Person { 
        //6过来就运行static块,打印第三句 
      static {  
               System.out.println("person static");
                }
    public Person(String str) {
            //6打印第一个参数test,第四句
                System.out.println("person " + str);
                    }}
class MyClass extends statictest { 
        //2继承的是statictest类,所以先看上面的statictest
       Person person = new Person("MyClass");
                //8运行完stasictest,开始打印这里的person
       static{ 
                //4加载statictest完,回来加载MyClass,发现static,直接第二步打印
            System.out.println("myclass static"); 
                }  
       public MyClass() {  
                System.out.println("myclass constructor"); 
               //9最后运行到这一步,打印出最后一句  
              }
           }
/*答案
test static
myclass static
person static
person Testtest 
constructorperson 
MyClassmyclass constructor
*/

Reference website / blog

Park a blog Gangster

Guess you like

Origin www.cnblogs.com/yhycoder/p/11920943.html