Java object-oriented programming summary

Class variables:

Instance variables:

      A class object is used to store state values. Modifier is public or private. Use of the entire class, may be invoked in any of methods in a class. Instance variable is based on the existence of its objects, run the program creates an object at the same time, create its instance variables, the program run is complete, the object disappears, its instance variables disappeared.

E.g:

public class Person

{

      public String name;

      public char gender;

      public double height;

      .......

}

Static variables (class variables):

      Modified with the modifier static variables. Static variables are class variables, it does not belong to any specific object instance of a class. It is not saved in the memory space of an object instance, and stored in a public storage space of the memory unit class.

例如: static String language="Chinese";

      the difference:

      Examples of commonly used variables private / public as a modifier. Objects of each class has its own set of instance variables in the class definition. Each object can only modify or access their own instance variables and Russia, will not affect the instance variables of other objects.
      Static variable static modifier. All objects of each class has a static variable common set of classes. Class and its objects can modify or access static variables.

The final variable:

      Using the modifier final modified variables. Its saved data will not change throughout the execution of the program. Usually coupled with static, then the actual meaning of this variable is the constant in front of the final.

For example: static final double pi = 3.1415926;

Volatile variables:

      The volatile modifier modified. Description The variable may also be controlled and modified several threads, i.e., the variable is not only grasped by the current program, there may be other unknown program operating during operation to affect and change the value of the variable.

 

Methods of the class:

Examples of methods:

      Examples of the method belonging to each object can only be called by the object class. Examples of the method may if desired be accessed outside the class, using the public; instance methods if desired only be accessed within the class, use private.

Static methods:

      Static methods use static modification, belongs to a class, that the contents of the memory space will vary with the type declaration of the allocation and loading has not been any single object. Static methods can not handle, and manipulate an object belonging to the instance variables, but only the entire process is a static class variables, i.e., the static variable or method handles still call the static method. Static methods can not access instance variables, so you can not use the keyword this in a static method.

 

 Keyword this:

      Action is needed when an object instance of class method calls the instance when the points. This must be the case with the:

      1, local variables and instance variables of the same name. (The most common application of this is to solve the problem of local variables and instance variables of the same name in the constructor and instance methods class) in the parameter name or local variable names and instance variables of the same name, since the priority parameters and local variables is high, in the method the body is a local variable or parameter name hidden name instance variables of the same name. Use this area which can be distinguished. 

      2, other configurations calling methods in the constructor. Call another constructor in the same class use the keyword this in the first statement in the constructor.

Format: this (parameter list); Note: modified by the modifier static static variables can not use this call.

public class Person
{
	private String name;
	private char gender;
	private double height;
	public Person(String name, char gender)
	{
		this(name, gender, 1.65);
	}
	public Person(String name, char gender, double height)
	{
		this.name=name;
		this.gender=gender;
		this.height=height;
	}
}

  

Keyword super:

      It refers to super parent. Application of inheritance and class. Common methods are as follows:

      1, access to the parent class constructor. In the constructor subclass, using the super may call the constructor of the superclass. General parameters for use constructor parameter redundant parent class constructor subclass constructor simplified statement body. Constructor must be the first statement.

Format: super (parameter list);

      2, access to the variables and methods of the parent class. When faced with a subclass instance variables or instance methods with the same name as the parent class's instance variables or instance methods, it will be hidden by subclasses of the parent class. This time if you want to use an instance variable or instance method in the parent class, you need the super keyword.

Note: super can not be used in a static environment.

 

Class inheritance:

      Class produced after inheriting called the child class or derived class, subclass inherits state and behavior of the parent class, but can also modify the state of the parent class or heavy-duty behavior of the parent class and add a new state and behavior. In Java, every class can have only one parent class, a class can have multiple sub-categories. Subclass can not inherit the parent class access to private instance variables and instance methods.

 

Polymorphic class:

      Polymorphism refers to the case where a program of the same name in different methods coexist. Polymorphism can subclasses of the parent class coverage achieved by the method may also be utilized in the different methods Method overloading declaration of the same name in the same class.

      When the method is declared to cover subclass, using the parent class name and the same parameters. During program execution, the execution method subclass, while overwriting of the parent class.


Original: https: //blog.csdn.net/ast_224/article/details/88336585

Guess you like

Origin www.cnblogs.com/qbdj/p/10951124.html