1, understand object-oriented programming ideas and object-oriented characteristics;
Object: Everything is object, all things are objects, in the program, we can think of classes, interfaces, methods, properties and so on as objects; object-oriented: is a programming method that constructs a system with objects as a basic unit. It uses objects to hide the complexity of the system in objects (that is, encapsulation) to build large industrial-grade systems and large systems. Object-oriented consists of three processes: Object-oriented analysis (OOA), Object-oriented design (OOD), Object-oriented programming (OOP). Object-oriented three features: inheritance, encapsulation, polymorphic object-oriented, and process-oriented differences? process-oriented is to analyze the steps required to solve the problem, and then use the function to implement these steps step by one, using a one-time call to be able.
2, the use of classes, attributes, methods and return keywords;
class: Java is Object-oriented,class(Class) can be seen as an object in Java, a class that contains: A method (what this object can do), a property (what is the nature of the object) class: Car properties: Color, brand, engine, etc.: Start, drive, brake we can classify different things with different characteristics into different classes; a declaration of a class: [Access modifier]classclass Name {} For example: Public classperson{//access modifier public class person//class name PrivateString name;//properties of the class PrivateString City; PrivateString Cardno; PrivateString Eyescolor; PrivateString Facecolor; Public voidWakeUp (String name,string time) {//how to get upSystem.out.println (name+ "Wake Up Time:" +Time ); } Public voidSleep (String name,string time) {//Sleeping MethodsSystem.out.println (name+ "Sleep Time:" +Time ); }} Property: The property of a class is a variable declared in a class, also called a class variable, a member variable, a global variable, can be called by multiple methods, to master: the use of global variables, local variables in development;1) to define no return value method: [ Public|Private|protected|Final]voidmethod Name (parameter optional) {} (2) defines a return value method: [ Public|Private|protected|Final] Return value type method name (parameter optional) {operation ...returnThe return value (must be the same as the method name before the type); p.s. void keyword: used for the declaration of a method, void means no return value, indicating that the method does not reverse any results after it has finished executing;returnkeywords: Used in the method body, indicating that the method executes, will return a specified type of results or data values;return、 Break, continue differences:returnjumps out of the entire method body, indicating that the method has a return value, and that any statements after the return are not executed; Break: Indicates exiting the current loop for use in a loop or switch case statement segment;Continue: Can only be used in a loop, which means exiting the current loop and continuing the following loop;
3, construction method, instantiation (use of new keywords);
Construction Method: The construction method must meet the following conditions:1, the method name must be consistent with the current class name when declaring;2, cannot use return, that is, the construction method has no return value, but also cannot use the void keyword;3, can not be static,Final、synchronized、Abstract, native modification; Any class has an implicit, parameterless construction method, for example: Public classtest{PrivateString x, y; PublicTest () {}//Non-parametric construction method PublicTest (String x,string y) {//a method of constructing parameters This. x=x; This. y=y; The constructor method is mainly used to initialize the member variables, and to construct the method execution process: The new instantiation is executed directly; for example: Public classperson{PrivateString username; Private intAge ; PublicPerson () {} PublicPerson (String username,intAge) {//constructor Methods initialize member variablesSystem.out.println (username); System.out.println (age); This. age=Age ; This. username=username; } Public Static voidMain (String args[]) {person Person1=NewPerson ("Zhang San", 23); System.out.println (Person1.username); Person Person1=NewPerson ("Zhang San", 23); System.out.println (Person2.username); }} is instantiated (New): In object-oriented, the instantiation process implements the creation of the object through the class (the class creates the object's process); During instantiation, the constructor of the corresponding class is executed; The object is a reference to the class, and the class is the template for the object;1The person is a class, and the name is called "Zhang San" of this person, he is a concrete example of human;2. Instantiate a String object: String str=NewString ("admin"); 3. Instantiate a Class object: Public classperson{PrivateString Cardno;PrivateString name; PublicPerson () {} PublicPerson (String cardno,string name) { This. name=name; This. cardno=Cardno; } Public voidsleep () {System.out.println ("sleeping ..."); } Public Static voidMain (String args[]) { person person=NewPerson ("2323123123", "SMITH");//instantiate the Person class object, at which time the construction method with two parameters is executed;Person.sleep (); } }
4, the use of static, this keyword;
StaticKey Words:1.Staticmodification method, which is called static method (also called class method), can directly call through the class name of the class;2, the static method cannot use the This/super keyword, the static method cannot directly call the non-static method in the current class (or non-static property), must be instantiated through new after the call, and for a non-static member method, it accesses the static member method/The variables are obviously unrestricted. 3, static declared methods and properties, the object has been instantiated, and can only be instantiated once4, static modified properties are shared by different instances of the same object; Example 1: Public classperson{ Public Static intAge ; Public voidrun () {System.out.println ("Run"); } Public Static voidsleep () {System.out.println ("sleep"); } Public Static voidTest () {run ();//errors, non-static methods or non-static variables cannot be called directly in a static method; intAge= This. Age;//error, current method is static method, cannot use the This keywordPerson person=NewPerson (); Person.run (); Sleep ();//correctPerson.sleep ();//correct}} Example 2: Public classTest {//static property: Name Private StaticString name; //non-static properties: Age PrivateString age; Public Static voidMain (string[] args) {Test test1=NewTest (); Test1.name= "Ho Tung Fei"; Test1.age= "12"; System.out.println (test1.age);//The result isSystem.out.println (Test1.name);//Why did you fly East?Test test2=NewTest (); Test2.age= "14"; System.out.println (test2.age);//The result isSystem.out.println (Test2.name);//Why did you fly East?//Test1 and Test2 have the same name, which explains the "static modified properties, shared by different instances of the same object."}}static code block: The static keyword has a more critical role to play in forming static blocks of code to optimize program performance. A static block can be placed anywhere in the class and can have more than one static block in the class. When the class is first loaded, each static block is executed in the order of the static blocks and is executed only once. Why static blocks can be used to optimize the performance of a program because of its nature: it executes only once when the class is loaded. Let's look at an example:classperson{PrivateDate birthDate; PublicPerson (Date birthDate) { This. birthDate =birthDate; } BooleanIsbornboomer () {Date StartDate= Date.valueof ("1946"); Date endDate= Date.valueof ("1964"); returnBirthdate.compareto (startdate) >=0 && Birthdate.compareto (endDate) < 0; }} Isbornboomer is used for this person whether it is 1946-1964 births, and each time Isbornboomer is called, will generate a startdate and birthdate two objects, resulting in space waste, if changed to such efficiency will be better:classperson{PrivateDate birthDate; Private StaticDate startdate,enddate; Static{StartDate= Date.valueof ("1946"); EndDate= Date.valueof ("1964"); } PublicPerson (Date birthDate) { This. birthDate =birthDate; } BooleanIsbornboomer () {returnBirthdate.compareto (startdate) >=0 && Birthdate.compareto (endDate) < 0; As a result, some initialization operations that only need to be performed once are placed in a static block of code.
5, the use of the package Package;import keywords;
6, access modifier public, private, final, protected;
8, the method of overloading;
9, inheritance, super keyword usage;
10, the method of rewriting (overwrite);
11, abstract class;
12, interface;
p.s. "Java Basics Comb-to Blossom" series by He Dongfei according to their own work for a few years to understand the understanding of Java, the author's leisure time will continue to update, there is no place please advise. qq:983489147
Java Basics Combing--picking up flowers (three)