There are three main features of object-oriented programming: encapsulation, inheritance, and polymorphism.
Packaging
A simple question.
A Java application that can output the area of a circle.
public class Computercirclearea {
public static void Main (String args[]) {
Double radius; Radius
Double area; Area
radius=163.16;
Area=3.14*radius *radius; Calculate area
System.out.printf ("Radius is%5.3f area of the garden: \n%5.3f\n", Radius,area);
}
}
In fact, suppose other Java applications also want to calculate the area of a circle. The same need to know to calculate the area of the circle algorithm. That is, you also need to write the same number of code here.
Questions such as the following are now raised:
Is it possible to encapsulate the data related to the circle and the code that calculates the area of the circle. So that the main class of Java applications that need to compute the circle area can calculate the area of the circle without having to write the code for the area of computation?
An important idea of object-oriented is to get classes by abstraction, and encapsulate some data and operations on those data in a class. Other words. The key points of abstraction are 2.1 data, and the other is operation on data.
We do for example the following abstractions on the observed circles:
The circle has a RADIUS attribute.
? ability to calculate the area of a circle using a radius.
Now, based on the abstractions above, the circle class, for example, is written as follows.
Class Circle {
Double radius; Radius of the Circle
Double Getarea () {//method for calculating area
Double Area=3.14*radius*radius;
return area;
}
}
(1) class declaration: Class circle in the first line of the above code is referred to as the declaration of classes. Circle is the class name.
(2) class body: A pair of curly braces "{" After a class declaration. "}" and the content between them is called the class body, and the content between the braces is called the content of the class body.
The following will create an object using the Circle class in the main class of a Java application. The object is able to complete the task of calculating the circle area, while the main class of the Java application that uses the object can calculate the area of the circle without knowing the algorithm that calculates the circle area.
Because a class is also a data type. So you can use a class to declare a variable, and in the Java language, a variable declared with a class is called an object.
Use circle to declare the code for an object with the name circle as follows:
Circle Circle;
The code for assigning variables to the Circle object declared by the Circle class is as follows:
Circle = new Circle ();
Object by using the "." operator to manipulate its own variables and invoke methods.
The format for an object to manipulate its own variables is: object. variable;
For example, circle.radius=100;
The format of the calling method is: Object. method;
For example, Circle.getarea ();
Class
Classes are the basic elements that make up a Java program. Class encapsulates the state and method of a class of objects.
A class is a template used to define an object.
The implementation of a class consists of two parts: a class declaration and a class body.
The basic format is:
Class Name {
The contents of the class body
}
Class is keyword, which defines classes.
Class name is the declaration part of the class. The class name must be a valid Java identifier.
The contents of the two curly braces and between them are the class body.
class declaration
class declaration: Class name
Like what:
Class Retangle {
... ...
}
The "Class Retangle" is called the class declaration; "Retangle" is the class name.
When naming classes, follow the following programming styles (this is not required by syntax, but should be followed):
(1) Assuming that the class name uses the Latin alphabet, then the first letter of the name is capitalized, as in.
(2) class name is best easy to identify, see the name of the understanding. When a class name is compounded by several "words", the first letter of each word is capitalized.
Class Body
The pair of curly braces "{", "}" and the content between them are called the class body, and the content between the braces is called the content of the class body.
The contents of a class body are constructed by two parts:
A part is the declaration of a variable. Used to characterize attributes;
There is also a part of the definition of the method used to characterize the function.
The following is a class named Retangle,
The variable declaration section of the class body content gives two variable width and heightr of type double.
The Getarea () method is defined in the method definition section.
Class Rectangle {
Double width; Variable declaration part, width of rectangle
Double height; Variable declaration part, the height of the rectangle
Double Getarea () {//define method for calculating area
return width*height;
}
}
member variables and local variables
The class body is divided into two parts: the declaration of a variable and part of the definition of the method.
Variables declared in the variables declaration section of the class body are called member variables of the class.
Variables and methods that are declared in the method body are referred to as local variables.
1. Type of member variable: the type of the member variable can be any data type in Java, including the base type integer, floating point, and character type. Reference types: Arrays, objects, and interfaces (see the contents of the objects and interfaces).
2. Valid range of member variables: the member variable is valid throughout the class, and its validity is independent of where it is written in the class body.
3. Programming style
(1) A row simply declares a variable.
(2) The name of the variable conforms to the identifier specified.
(3) The name of the variable is known. Avoid confusing variable names with easy.
Method
The class body is divided into two parts: the declaration of a variable and part of the definition of the method.
The definition of a method consists of two parts: a method declaration and a method body. The General format is:
Method declaration Section {
Content of the method body
}
1. The method declaration method declaration contains the method name and the return type of the method, such as:
Double Getarea () {
return width*height;
}
Focus on the return type of the method, the parameters of the method, the name of the method
2. Method body
A pair of curly braces "{" After the method declaration. "}" and the content between is called the method body of the method. The contents of the method body contain declarations of local variables and Java statements.
Variables and methods that are declared in the method body are referred to as local variables.
3. Distinguishing member variables from local variables
Suppose the name of the local variable is the same as the name of the member variable. The member variable is hidden, that is, the member variable is temporarily invalidated within this method.
Issues that need attention
The contents of a class body consist of two parts: a declaration of a variable and a part of the definition of a method.
The operation of a member variable can only be placed in a method. method is capable of manipulating member variables and local variables declared in the method body.
It is possible to give the initial value at the same time when declaring a member variable, but it is not possible to have a separate assignment statement in the class body (a statement other than the declaration of the variable and the definition of the method in the class body).
Class A {
int a=12;
float b=12.56f;
}
However, it is not possible to do this:
Class A {
int A;
float B;
a=12; Illegal. This is an assignment statement (statement is not a declaration of a variable, it can only be present in the method body)
b=12.56f; Illegal
}
UML Diagrams for classes
The UML (Unified Modeling Language Diagram) diagram belongs to the structure diagram. is often used to describe the static structure of a system.
A UML diagram with a class (class), an interface (Interface) UML diagram, a uml diagram of a generalization relationship (generalization), a UML diagram of an association relationship (association), a dependency relationship (Dependency). UML diagram for UML diagrams and implementation relationships (realization).
In a uml diagram of a class, a rectangular description is used to describe the main composition of a class, which is vertically divided into three layers.
The 1th layer is the name layer;
The 2nd layer is the variable layer, also called the attribute layer;
The 3rd layer is the method layer, also called the operation layer.
Basic structure of classes and Java applications
A Java application (also known as a project) consists of several classes that can be distributed in a single source file and in several source files.
Java agrees to write multiple classes in a Java source file, but multiple classes can have at most one class using public adornments.
Assuming that there are multiple classes in the source file, but there is no public class, the source file name is just the same as the name of a class, and the extension is. java is available.
Given that a class is a public class, the name of the source file must be exactly the same as the name of the class, and the extension is. java. Compiling the source file will generate multiple bytecode files with a. class extension. The name of each bytecode file is the same as the name of the corresponding class in the source file, and these bytecode files are stored in the same folder as the source file.
Construction method and object creation
A class is the most reusable data type in an object-oriented language that can be used to declare variables.
In an object-oriented language, a variable declared with a class is called an object.
Unlike the base data type, after declaring an object with a class, it is also necessary to create an object, that is, to assign a variable to the declared object (to determine which property the object has), when an object is created using a class. It is also said to give an example of this class. In layman's words. A class is a "template" that creates an object, and no class has an object.
Construction methods and object creation are closely related.
Construction method
The construction method is a special method. Its name must be exactly the same as the name of the class in which it resides. And there is no type.
Agree to write several construction methods in a class, but you must ensure that their number of parameters is different. The number of parameters is different. Or the type of the parameter is different.
It is important to note that
(1) Assuming that the class does not write a construction method, the system defaults to a constructor method. The default construction method is no parameters. And there are no statements in the method body.
(2) If one or more construction methods are defined in a class, Java does not provide a default constructor method.
Creating objects
1. The general format for declaring objects is:
The moniker name of the class;
such as: Rectangle Rectone;
Here rectangle is the name of a class. Rectone is the name of the declared object.
2. Allocating memory for declared objects
The object is created by assigning a variable to the declared object using the new operator and the constructor of the class. Assuming that there are no constructed methods in the class, the system calls the default constructor method.
such as: Rectone = new Rectangle ();
3. The memory model of the object
(1) memory model when declaring an object
(2) Memory model of object allocation memory
4. Create multiple different objects
Working with objects
objects can not only manipulate their own variables to change state, but also invoke methods in the class to produce certain behaviors.
By using the operator ".", an object can implement a call to its own variable access and method.
1. object to manipulate its own variables (properties of the object)
After the object is created. It has its own variables. That is, the entity of the object. By using the operator ".", the object is able to implement an access to its own variable, in the form of: object. Variable;
2. Object calls a method in a class (the function of an object)
After the object is created, you can use the operator "." Call the method in the class that created it. Thus generating certain behavioral functions. The call format is: Object. method;
3. Embodiment Package
When an object invokes a method, the member variable that appears in the method refers to the variable that is assigned to the object.
Example 3
Rectone.getarea ();
Recttwo.getarea ();
Note: When an object invokes a method, the local variable in the method is allocated memory space. The method runs and the local variable immediately frees up memory. It is important to note that the local variable declaration assumes that there is no initialization, there is no default value, so before using the local variable, it is assigned a value beforehand.
Object references and Entities
1. Avoid using empty objects: objects that have no entities are called empty objects. An empty object cannot be used, that is, you cannot let an empty object invoke the behavior of the method.
2. Garbage collection: The two objects declared by a class assume the same reference, and the two have exactly the same entity.
Class Point {
int x, y;
void SetXY (int m,int n) {
x = m;
y = n;
}
}
public class E {
public static void Main (String args[]) {
Point P1,p2;
P1 = new Point ();
P2 = new Point ();
System.out.println ("P1 's Citation:" +P1);
System.out.println ("P2 's Citation:" +P2);
P1.setxy (1111,2222);
P2.setxy ( -100,-200);
System.out.println ("P1 x, y coordinates:" +p1.x+ "," +p1.y ");
System.out.println ("P2 x, y coordinates:" +p2.x+ "," +p2.y ");
P1 = p2;
System.out.println ("After assigning a reference to P2 to P1:");
System.out.println ("P1 's Citation:" +P1);
System.out.println ("P2 's Citation:" +P2);
System.out.println ("P1 x, y coordinates:" +p1.x+ "," +p1.y ");
System.out.println ("P2 x, y coordinates:" +p2.x+ "," +p2.y ");
}
}
Parameter Pass Value
One of the most important parts of the method is the parameter of the method. A parameter is a local variable, and when the object invokes the method, the argument is allocated memory space and the caller is asked to pass the parameter, that is, when the method is called. Parameter variables must have a detailed value.
Value-Transfer mechanism
In Java, all parameters of a method are "passed", that is, the value of the parameter variable in the method is a copy of the value specified by the caller.
For example, if you pass an int value to the int parameter x of the method, then the value obtained by the parameter x is a copy of the value passed.
The value of the base data type parameter
For a reference to a base data type, the level of the value passed to the parameter cannot be higher than the level of the number of parameters.
Example. You cannot pass a float value to an int parameter, but you can pass a float value to a double parameter.
In the preceding example 4, the object calls setxy (int m,int n) to set its own x, Y coordinate value, so that when the object calls the SetXY (int m,int N) method, it must pass the values to the parameters m and N of the method.
Value of reference type parameter
When the parameter is a reference type. "Pass value" passes the "reference" stored in the variable, not the entity referenced by the variable.
Combination of objects
The member variable of a class can be any data type that is agreed by Java, so that a class can take an object as its own member variable, assuming that the object is created with this class, then there will be other objects in the object, that is, the object will have other objects as part of itself (this is often said Has-a).
Cone
Make an abstraction of the cone. Properties: Bottom Circle. High
function: Calculate the volume
The base circle of the cone should be an object. For example, the object declared by the Circle class. The height of the cone can be a variable of type double, that is, the cone takes the object of the Circle class as its own member.
UML diagrams for relational and dependency relationships
1. Association relationship: Suppose that the member variable in Class A is an object declared with Class B, then the relationship between A and B is an association, and a is associated with B or a combined B.
If a is associated with B, then UML uses a solid line with a and b UML diagrams, and the starting end of the solid line is a UML diagram. The end-point is a uml diagram of B. However, the end end of the solid line is represented by the direction arrows of a UML diagram pointing to B.
2. Dependency: Assuming that a method in Class A has a reference to an object that is declared with Class B or that the data type returned by a method is a Class B object, the relationship between A and B is a dependency, which is called a dependent on B.
Assuming a depends on B, then UML uses a dashed line with a and B UML diagram, the starting end of the dashed line is a UML diagram, the end point is a uml diagram of B. However, the end end of the dashed line is indicated by the direction arrows of a UML diagram pointing to B.
Instance members and class members
Declaration of instance variables and class variables
The member variables of a class can be subdivided into instance variables and class variables.
When declaring a member variable, it is called a class variable that is decorated with keywordstatic. Otherwise, it is called an instance variable (a class variable is also known as a static variable).
Like what:
Class Dog {
float x; Instance variable
static int y; class variables
}
In the dog class, X is an instance variable, and y is a class variable.
It is important to note that static needs to be placed before the type of the variable.
The difference between an instance variable and a class variable
1. Instance variables for different objects are not the same
2. All objects share class variables
3. Direct access to class variables by class name
Definition of instance methods and class methods
Methods in a class can also be divided into instance methods and class methods. Method
When declaring, the method
Types that are not keywordstatic decorated are instance methods, plus
Statickeyword modifies a class method (static method).
Classes and objects in Java