Object Oriented (a) - Encapsulated

Object Oriented (a)

Object-oriented - Encapsulated

Class declaration format

public  class Student {
 // Access Modifiers Modifier + class + class name 
    public String name;
     // class attribute
     // member variables, global variables
     // access modifier + + variable name data type 
    public  void info () {
     / / access modifier + + return value of the method name
     // similar method, you can call their own attributes 
        the this .name;
         // the this: current object  
         // If not this, then follow the principle of proximity 
    } 
}

In the main method call

  1. new Object / Attribute

    STU = Student new new Student ();
     // Object name = + object + object instance
  2. Assignment

    = stu.name "fr" ;
     // . exemplary object object attribute = value + attribute

    Stu assignment made by the object, by '' are connected.

  3. Object array assignment

Student [] stus = new Student[5];
stu[0] = new Student();
//或者
Student [] stus = {Student(),Student(),Student(),null,null}

 

Construction method

default:

public class name () {}
 // This method comes default

Features:

  1. No return type

  2. The same as the class name and method name

Precautions

  1. In a class, java compile time compile the value of the property, the value of the constructor call, that is, if there constructor parameter assignment, and its value will override the default setting of the initial value.

  2. Constructor arguments override the default constructor with no arguments, you write a constructor with parameters, it is best to write a constructor with no arguments

  3. In the configuration process may be alternatively constructed by the this method name, its parameter types retrieval, number, sequentially consistent constructor (only in this class)

  4. Constructors can not be rewritten, inheritance, and a final modification

  5. this () and super () must be placed on the first line of the constructor

Access modifier

Modifiers   Scope
private private Only in this category
default default The same package analogy public, there are differences in different packages
protected under protection Public access to different packages and different, inherit different packages, there are differences with the default
public public Can access anywhere
  1. It is noteworthy that private, default, protected between two unrelated classes in different packages is not accessible!

final

  1. Modifying a variable, a constant, must be given an initial value, generally associated with static

    public  static  Final data type uppercase constant names
  2. Modification methods, methods for the final method can not be overridden.

  3. Modified class, the class is final class can not be inherited

static

  1. Only a static variable value

  2. Static methods can only refer to a static variable, you can not refer to non-static (reference belong to the same area in memory)

  3. Static variables are placed in static storage block

  4. Static Property belongs to the class, only one value (call it belongs to the class as a whole rather than individual objects)

    General Dingding Yi assignment is: static class name property name

  5. Class initialization, the first initial static property or method

  6. If the property has been given too static value, not in the subsequent new creation (initialization process only once)

  7. String = "" value stored in the static area

  8. static not be used in this, super

  9. Static inoperable non-static

Static block

format:

static {
    
}

This class module loaded into the virtual machine when called before the constructor execution

Entity Block / statement block

{
    
}

Performed after static

javabean-

Writing data format specification

javabean private use defined attributes, which makes the barrier properties of the package can no longer be accessed.

In order to solve, using indirect methods, the definition of get, set method.

  1. Only modified data set name (parameter type) {} this data parameter = name;.}

  2. Get read-only data name () {return this. Data Name}

Guess you like

Origin www.cnblogs.com/-Archenemy-/p/11967566.html