Java Object-oriented

Source: Internet
Author: User


1. Object-oriented is relative to process-oriented.
(1) Process-oriented processes, performers. (2) process-oriented processes, conductors. I asked you to help me buy cigarettes, that is, object-oriented. I go to buy cigarettes myself is the process oriented. (3) Object-oriented features: A. Encapsulation B. Inheritance C. polymorphism
2. Classes and objects:
(1) A class is a description of the real world. (2) The object is an individual that exists in reality.
3. member variables and local variables
(1) Scope: Local variables within the method, member variables in the class. (2) storage space: Local variables in the stack, member variables in the heap. (3) initialization value: The local variable is manually initialized, and the member variable JVM is initialized by default.
4. Use of Anonymous objects
(1) used when the function in the object is performed only once, otherwise the moniker is used. (2) The courage to pass as the actual parameters.
5. Encapsulation: The property is hidden only to provide public access methods.
benefits: Easy to reuse, high security.
6.private:
(1) Access to member variables can be controlled early in the method. Private is just a form of encapsulation. (2) Common methods:no return value void Setage (int a) {}also returns the value int Getage () {}
Class demo{
int age;//provides a public access method to private property, and Controllability improves
public void Setage (int.) {//age =-20;
if (age>=0 && age<=200) {
This.age = age;
}
else {
System.out.println (' Return to Mars ');
}
}
public int Getage () {
return age;
}
}
Class demotest{r/> public static void main (string[] args) {
Demo d = new demo ();
D.setage (-20);
System.out.println (D.getage ());
}
}

7. Constructor Features:
(1) The method name must be the same as the class name(2) The constructor does not have a return value and cannot be defined as void, and the method name is not declared before the method type(3) The construction method cannot have a programmer call but is called by the system(4) A class can define multiple construction methods(5) If the constructor is not defined, the system will give a default parameterless constructor(6) to initialize the object, the system will invoke itself when the object is established.Column:class person{
int age;
String name;
Person () {}//argument-free constructor
person (int age,string name) {//age = 20,name = ' Zhangsan ';
This.age = age;
THIS.name = name;
}
public void Speak () {
SYSTEM.OUT.PRINTLN (age + ' ... ' + name);
}
}
*****************************************
Class demo{
public static void Main (string[] args) {
Person p = new person (' Zhangsan ');
Person p = new person ();
P.speak ();
}
}

8.this Keywords:
which object calls the function that this is located on represents that object. function:(1) When a function parameter or a local variable in a function has the same name as a member variable, the member variable is referenced by the this. Member variable name. (2) Use this directly when referencing the current object of the class to which the function belongs in the function(3) This (parameter list) calls another constructor in one constructor with this this statement (which can only be placed in the first row of the constructor). columns:
Person (String name) {
THIS.name = name;
}
Person (String Name,int age) {
This (' Zhang San ');//is to find a constructor that takes a string type argument
THIS.name = name;
This.age = age;
This (' Zhangsan ');
}
****************************************************class demo{
public static void Main (string[] args) {
Person p = new person (' Lisi ', 20);

}
}

9. Code block:
(1) Local code block definition in the method, you can control the life cycle of the variable(2) constructs a code block definition in a class that executes once for each object created(3) A static code block initializes the class and executes only once for the first time. Person P =new What is done in memory during the person's process:A. Loading the person.class into memoryB.P defines the function, which opens up the variable space p in the stack memoryc. Allocate space in heap memory and allocate memory address values,D. Default initialization amount for attributes in an objectE. Display initialization for values in an objectF. Invoking a construction code block to initialize an objectG. Invoking constructors for initializationH. Assign the memory address value of the object to the P variable, and let the P variable point to the object.
10.static Static is used to modify member variables, and members are statically modified to be used directly by: class name. static member Invocation .
Static Characteristics:loads with the load of a class and has a long life cycle. precedence over the existence of an object. shared by all Objects. Static variables are stored in the method area. Instance variables are non-static member variables, and class variables are static variables. The class variable is loaded with the class in the method area, and the instance variable is stored in the heap with the object. Class variables belong to the entire class, shared by all objects, and instance variables are unique data for the object. Note: 1) static methods can only access static members, and non-static methods can access static pages without access to static. 2) The This Super keyword cannot be defined in a static method because static takes precedence over the existence of an object, so this is not available in a static method. Static Advantages and disadvantages: The shared data of the object provides a separate storage space, without each object to save a copy of memory, you can class name. Called directly. Life cycle is too long, static is good, only access to static. when you can use static: The data stored by the member variable does not have this data, it is not necessary to store it in the object, so that all objects can share the data and be decorated with static. When to define a static function: When the member function does not have access to the object's unique data, the method is defined as static, and the method does not have access to the static unique data, the direct class name. Call.
11. Simple Interest Design mode:
a hungry man type:1) Private constructors2) Create an object of this class3) Define a public access method:class Single {
Private single () {}
private static Single S = new single ();
public static single getinstance (int num) {
return s;
}
Lazy Type:
1) Private constructors2) Declare a reference to this class3) Define a public access method
Class Single {
Private single () {}
private static single S;
public static single getinstamce (int num) {
if (s==null) {
s = new single ();
}
return s;
}

Java Object-oriented

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.