1. Introduction to Object-oriented programming object-oriented is now the mainstream programming sample, it replaces the previous C language used in the "structure", Java is an object-oriented language, so need to familiarize yourself with the concept of object-oriented. Object-oriented programs consist of many objects, each of which has special functions for the user. Many objects within the program can be directly used directly from the public library. No need to study how these functions are implemented. The traditional structure of programming consists of a series of algorithmic steps to solve the problem. Once these steps are identified, also determine how the data is stored. That is, the data structure begins to learn: algorithm + data structure = program. Determining the algorithm before deciding what structure to use to store the data is a good way to solve small problems, but it is necessary to use an object-oriented approach when encountering large problems. Using the original method, the global data is stored according to the procedure, and if there is a problem with the data, it is necessary to find it step-by-step. But if there are objects inside, and then the data can be manipulated by means of the method, it is only necessary to find the data from those methods. A class is a blueprint for object generation, and when you generate an object from a class, you can say that you created an instance of the class. Write code in the class, and there are thousands of kinds of standard Java libraries for different purposes, such as user interface, date and calendar, network programming. In addition, you can create your own class to describe the object according to the problem you want to solve. Encapsulation is a very important concept in class thinking, for users, encapsulation will have the data and behavior in a package, hiding the implementation of the details. The place where the data is stored becomes the instance area, and the operation of the data is called a method, which is a state of the object, which changes when you wake up the class of an object. The most important idea of encapsulation is to have a way to access the data through a method, prohibit direct access to the data area of the class and other places, making the object look like a black box, which is reusable and reliable. This means that the class completely changes the way the data is stored. 2. Class Examples: for example,
public class Circle {private static double radius;private static double PI = 3.14159;public void Settheradius (double r) {RA Dius = r;} Public double Getthearea () {return pi*radius*radius;}}
The state of the object is defined as the value of the data area, and the circle example in the example above has a radius as its state, there are many radii, and the value can be changed by the face reading method. Similar objects, defined by public classes, can then be generated, with different circles, and different values can be set. For example 1. Class name:circle1 Data Field:radius = 10;2. Class Name:circle2data Field radius = 15: That is to say, the circle class above generates two circles of objects, of which the radii of the two circles are 10 and 15, but the class in the example does not have a main function, so it cannot be run. Later in the use of classes and objects you can follow the constructor and method: the name of the constructor and the name of the class classname (Parametername:parametertype) method: MethodName (parametername: ParameterType) ReturnType3. Using constructors to construct an object constructor is a special method: 1 The constructor is named the same as the name of the class 2 the constructor has no return type-(void is returned as null) so not void 3 creates the object when the constructor uses the new operation, the constructor is used to initialize the object such as above Example of a circle
public class Circle {private static double radius; Circle () { radius = 0;}}
In the example above the initialization of the operation as
Circle Object1 = new Circle ();
In this case, the new operation is called when the object is generated, and if you add void before the circle () construct above, it becomes Method 4 pass the parameter through the method if you want to modify the state of each object, that is, the data area, you can modify it by means of the method, or get the data. Example
Class Student {String name;//name has the default value nullint age;//age have default value 0boolean issciencemajor; Sciencemajor has default value Falsechar gender; C has default value}
The above-mentioned class of students, the status of which is the student's name, age, is not elective science, gender, and then can be modified by means of these states, or to get these values
public void Setthename (String na) {name = NA;} Public String Getthename () { return name;}
Other state modifications are similar and can be used in the following example
public class Computeareaaa {public static void main (string[] args) {Circle object1 = new Circle (); Object1. Setthename ("Harker ' Circle"); System.out.println ("The name of the Cirlce is" +object1. Getthename ());}}
Now look at what the console wrote: This can be categorized as a problem: we define a class to solve a problem, and then generate an object, which calls some methods to modify the state of the object (the value of the data area), but also very much like the relationship between the state amount and the control amount, a description of the object we encountered, Then another volume to modify the state, at this time can also imagine the example of life: TV, the television has a state is on or off, the two states to have a certain operation can switch. Note the difference between the original amount and the reference amount of the original type of data such as int i = 1; The reference amount of the object type, such as Circle C, at this time the data, such as the radius, but when you assign a variable to another number, there will be the following: do c1 = C2 operation: Before: Operation: only is to change what C2 points to. 5 using classes in the Java library, you can use classes that are already in the Java library: 1 time class Java.util.Date
public class Timeclass {public static void GetTime () {java.util.Date Date = new Java.util.Date (); System.out.println ("The elapsed time since Jan 1, 1970 is" + date.gettime () + "milliseconds"); System.out.println (Date.tostring ());}}
2 classes that generate random numbers
Java Learning Notes-classes and objects