Java Basics - 05 Object-Oriented

1.final keyword

 After complete description of the class, you do not want to be inherited, or part of some class method function is fixed, do not want subclasses override.

 

 Keywords Final , Final meant for the final, immutable. final is a modifier that can be used to modify the class, members of the class, and local variables. Constructors can not be modified.

 final features:

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

   The final modification methods can not be rewritten subclasses , but not the parent class final modification methods, subclasses override post can add final.

    final modified variables are called constants, these variables can only be assigned once. And life-long

 Variable is a reference type object address value, the address value can not be changed, but the address of the object property values ​​can be modified.

  Modified member variable, you need to create an object in front of the assignment, otherwise an error. (When there is no explicit assignment, assigning values require multiple constructors.)

 

  

2.static keyword 

  static it is static modifier, generally used to modify the class members.

 

 static key features:

 1) was modified static member variables belong to the class, not part of a class of objects. ( In other words, access or modify multiple objects when modified static member variables, one of the objects will be static member variables values were modified, static member variables in other objects of value change with that multiple objects share the same static member variables )

  Code demonstrates:

class Demo {

 

public  static  int  num  = 100;

 

}

 

 

 

class Test {

 

public static void main(String[] args) {

 

Demo d1 = new Demo();

 

Demo d2 = new Demo();

 

d1. a  = 200;

 

. The System OUT .println (D1. NUM ); // result 200

 

. The System OUT .println (D2. NUM ); // result 200

 

}

 

}

 

2) be modified static members and suggested that direct access by the class name

Access static members of format:

The class name . Static member variable name

The class name . Static member method name ( parameters )

Object name . Static member variable name      ------ do not recommend using this mode, there will be a warning

Object name . Static member method name ( parameter ) ------ do not recommend using this mode, there will be a warning

Code demonstrates:

class Demo {

// static member variables

public  static  int  num  = 100;

// static methods

public static void method(){

. System OUT .println ( " static method " );

}

}

class Test {

public static void main(String[] args) {

System.out.println(Demo.num);

Demo.method();

}

}

 

static precautions

1) Static content is a priority for object exists, can only access static, you can not use the this / Super . Static modified content stored in the static area.

2) the same class, static members can only access static members, code demonstrates:

class Demo {

 

// member variables

 

public  int  a = 100;

 

// static member variables

 

public  static  int  count  = 200;

 

// static methods

 

public static void method(){

 

//System.out.println ( NUM ); static method, you can only access static member variables or static member method

 

System.out.println(count);

 

}

 

}

 

 

. 3) main method is a static method procedure is performed only for the inlet, which does not belong to any object, can be defined in any class.

 

4) polymorphic method calls, the compiler see = left, there is the parent class, compiled successfully, the father did not, fail to compile

 

Run, static method, run the parent class's static methods,

 

Overriding methods run, non-static method, run subclass

 

Member variables, compile and run all the parent class

 

 

 

Define static constants

  Development, we want to define a static constant, often used in the class public static final modification of variables to complete the definition . In this case the variable name in all caps, use multiple words underscore.

Definition Format:

 

public static final data type of  the variable name  = value ;

Code demonstrates:

class School {

public  static  Final  String SCHOOL _NAME  = " Peking University ";

public static void method(){

. System OUT .println ( " a static method " );

}

}

 

When you want to use a static member of the class you do not need to create an object, use the class name to access directly to:

System.out.println(School.SCHOOL_NAME);

. School Method, (); // call a static method

 

 

note:

 

Each member variable interfaces use the default public static final modification.

 

All member variables in the interface has a static constant, because the interface has no constructor, it is necessary to display assignment. You can directly access the interface name.

 

interface Inter {

 

public static final int COUNT = 100;

 

}

 

Access interface Static variables

 

Inter.COUNT

 

 

3. anonymous object

  Anonymous object means the object is created, only to create an object statement, but did not address the value of the object is assigned to a variable.

E.g:

Create a generic object

 

Person p = new Person();

 

 

 

Create an anonymous object

 

new Person();

 

It features an anonymous object

 

1) create anonymous objects directly, not the variable name.

new Person (). eat () // eat method is a no-name Person object is called.

 

2) does not specify an anonymous object when its reference variables can only be used once.

 

new Person () eat (); . create an anonymous object, call eat method

 

new Person () eat (); . would like to call again eat method to re-create an anonymous object

 

3) the received anonymous object as a method parameter value for use in the method returns

 

class Demo {

 

public static Person getPerson(){

 

// normal way

 

//Person p = new Person();

 

//return p;

 

 

 

// anonymous object as the method returns a value

 

return new Person();

 

}

 

 

 

public static void method(Person p){}

 

}

 

 

 

class Test {

 

public static void main(String[] args) {

 

// call getPerson method to obtain a Person objects

 

Person person = Demo.getPerson();

 

 

 

// call the method method

 

Demo.method(person);

 

// anonymous object as a method parameter received

 

Demo.method(new Person());

 

}

 

}

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/sunlangui/p/11479171.html