1. Overview of object-oriented thinking
· For :
The basic meaning: facing towards
Derived meaning: focus, attention
· Object :
Everything in the world
· Process-oriented thinking:
Process-oriented idea is to focus on the one thing or the steps involved in an activity (ie process) thinking on
Object-oriented thinking:
Object-oriented thinking idea is to focus on the things that a person or a thing or activity involved (that is, objects) on the -
- Features: more in line with people's thinking habits of thought, the complex things simple, then people become commander from the executor
- wherein: encapsulation, inheritance, polymorphism
2. Classes and Objects
Describe things from two aspects :
1. Properties
2. Behavior
java how to describe a thing?
Java to describe things through the "class", mainly composed of attributes and behaviors
The concept of class
That classification, classification, are collectively referred to a series of things that have the same properties and behavior, with properties and behavior
abstract
The common attributes and behavior of a series of related things extracted from the process
Objects - What is an object?
A specific class of things there is a
The relationship between classes and objects
Class: a collection of attributes and behavior, it is an abstract concept
Object: a concrete manifestation of such things, there is a specific
3. The class definition and use
If you define a class?
Process class definition is the common attributes and behavior of a series of related things drawn out process
Properties of things, called member variables in the class
Behavior of things, called member methods in a class
If you create an object?
Object class name name = new class name ();
How to use an object?
Object name variable name
Object name. Method name ()
Example: the definition and use of mobile phones in class
1. Definitions:
Class Name: Phone
Member variables:
String brand; // brand
String model; // Model
String name; // Name
Member method:
call (); // call
sendMessage (); // send text messages
playGame (); // play games
2. Use:
Phone p = new Phone();
// define a cell phone class public class Phone { // member variables: variable outside the class definition, the method // brand String Band; // type String Model; // name String name; // member method // call public void call (String name) { System.out.println ( "a" + name + "call" ); } // texting public void the sendMessage (String name) { System.out.println ( "a" + name + "texting" ); } // play the game public voidplayGame () { System.out.println ( "play" ); } }
public class TestPhone { // main function is the main entry to the program, all the code is executed from here public static void main (String [] args) { // 1. Create Object Phone P = new new Phone (); // 2. call the member variables and prints // to the member variable assignment p.band = "Apple" ; p.model = "the X-" ; p.name = "a students" ; // print the value of member variables System.out.println (p.band); System.out.println (p.model); System.out.println (p.name); // 3. invoke a member method System.out.println ( "======== == "); P.call ( "B students" ); p.sendMessage ( "C students" ); p.playGame (); } }
JAVA variables used in the rule :
Use variables follow the principle of proximity, if the local position on the use of
Do not go to this location to find a class member, you have to use, not on the error
The difference between the member variables and local variables
1. Definitions position
Member variables: class, method outside
Local variables: the method or form parameters
2. initialization value
Member variable: there is a default initialization value
Local variables: no default initialization values, you must first assign reuse
3. Range:
Member variables: in class
Local variables: the process
4. location in memory:
Member variables: heap memory
Local variables: the stack memory
5. Life Cycle
Member variables: With the creation of the object exists, the object disappear with the disappearance
Local variables: With call a method exists, along with the method call is completed disappear
Precautions:
Local variables and member variables table the same name, to take the principle of proximity
4. Package Overview
What is the package?
The series can be packed into a single device, it offers to use these features interface
Benefits package
- improve security
- improve reusability
- the complex things simple
java package in
1. Methods
Security: the caller does not know the specific implementation method
Reusability: process may be repeated using
Simplistic: the range of the code by way of a method of presentation, can be achieved only function, code maintenance becomes simple by calling the method
2. Class
Security: the caller does not know the specific implementation class
Reusability: object class can be reused
Simplification: object class contains more features, easier to use
5.private keyword
The basic concept of private
Private An access modifier for the modified class members
Features: Do not modified members are accessible only in this category
usage:
private data type variable name
private return type method name (parameter list) {}
Expand: public, public, access modifiers, pertaining to class, member variables, methods, members, the modified content can be accessed at any class
Examples: private member variable modification
Requirements: to the variable student class with the private modifier, then the proper use of the test class member variable
public class Student { // member variables // Name Private String name; // Age Private int Age; // member method // learning public void Study () { System.out.println (name + "learning" ); } // public access mode, the setting value, respectively, to obtain the value // set the name public void SetName (n-String) { name = n-; } // Get the name of public String getName () { return name; } // Set the age public void the setAge ( int A) { Age = A; } // Get Age public int getAge () { return Age; } }
public class TestStudent { public static void main (String [] args) { Student S = new new Student (); // call the method to set a member setName member variable name s.SetName ( "John Doe" ); // call the member method setAge to set the member variables Age s.setAge (15 ); // call the getName and getAge methods to obtain the names and ages System.out.println ( "name:" + s.getName () + "Age:" + s.getAge ( )); } }
operation result:
6.this keyword
It indicates that this class object reference to an object is essentially
Feature
This method has a method for each ordinary this, who will point to who to call
usage
this. attribute name
7. Standard Code: JavaBean
The basic concept of the method of construction
Build, create, also known as the constructor, to be exact, the role of the constructor is to initialize the object
Who will create an object?
new keyword, Java objects through the creation of the new keyword, and open space in memory, and then use the constructor (constructor) complete object initialization
Defined constructor
format
Modifier constructor name (parameter list) {
// method body
}
Precautions:
1. The method name must be the same class name (including the case)
2. no return value (but which can write return)
3. No return type (not void)
4. Failure to provide a method of any configuration, the system will be given a default constructor with no arguments
5. The method of any configuration provided Ruoyi, constructor with no arguments is no longer available
6. The method of construction can be overloaded
Standard Code: JavaBean
Guarantee specification written in Java language class. JavaBean meet standard class, it must be specific, common, and constructor with no arguments, to provide a method for operating a set and get member variables
The concept encapsulated in Java
The extracted common attributes and behavior of a series of related things out into a class, the hidden object's properties and implementation details, just outside the provision of public access methods
The key package
Class method must not be allowed direct access to other types of data (attributes), methods and procedures only the data object by object interaction
example:
// define a standard JavaBean class public class Student { // member variables: all with private modification // name private String name; // age of private int Age; // constructor: constructor with no arguments, the whole argument constructor // None configuration parameters public Student () { } // All parameters configured public Student (String name, int Age) { the this .name = name; the this .age = Age; } // public access mode: the getXXX (), the setXXX () / / set the name public voidthe setName (String name) { the this .name = name; } // Get the name of public String getName () { return the this .name; } // set the age public void the setAge ( int Age) { the this .age = Age; } // Age Get public int getAge () { return Age; } }
public class TestStudent { public static void main(String[] args) { Student stu = new Student(); stu.setName("张三"); stu.setAge(12); System.out.println("姓名:"+stu.getName()+"年龄:"+stu.getAge()); System.out.println("==================="); Student stu2 = new Student("李四",15); System.out.println("姓名:"+stu2.getName()+"年龄:"+stu2.getAge()); } }
operation result: