JAVA learning Lesson 6 (Object-Oriented) and java Learning Object-oriented
What Is Object-Oriented first?
Object-oriented: Constantly dealing with objects.
Early solutions to problems were process-oriented (simply put, how to do it step by step, and process-emphasizing actions), such as C language, using functions to call functions.
Thoughts:
Object-oriented: It emphasizes objects, that is, objects. C ++, java, c #
Example: put an elephant in a refrigerator
Process-oriented implementation: 1. open the refrigerator; 2. Store elephants; 3. Close the refrigerator.
Object-oriented implementation: (find the entity, and the entity encapsulates these functions, that is, find the refrigerator. The refrigerator has the function of opening, storing, and disabling)
1. open the refrigerator 2. Store the refrigerator 3. Close the refrigerator
Object-oriented features:
1. It is in line with people's thinking habits. 2. simplify complicated problems
3. role transition is aimed at objects, that is, executors in the process, and become object-oriented commanders.
(In other words, it is none of our business to say that the object has the function you need and how to do it)
To solve a problem, first find out if there are any objects to solve the problem. If not, write an object by yourself.
Example of Object-oriented thinking:
The interviewer recruitment is a good example of object-oriented. An enterprise accepts a project, designs, writes, and tests. The interviewer can do this by himself, but it wastes a lot of time and energy,
Therefore, he needs to recruit some personnel to help him complete the work. The target is the person to be hired. They have the design, writing, testing, and other functions, but how can they be implemented, interviewer No
You need to know. The interviewer only needs to find this object and instruct him to solve the problem.
In JAVA, everything can be an object.
Some objects are used to find objects, no objects are used to create objects, and the relationships between objects are constantly maintained.
Object-oriented features:
1. encapsulation 2. Inheritance 3. Polymorphism
Rule: Which object is the most clear about an action? Define the action in the object. The moving company only needs to tell where the porter moves the house. As for how to move the house, the porter knows the most clearly and naturally defines the carrying action in the object of the porter.
(Directing objects to do things)
Code understanding:
/* The definition class is the member in the definition class, that is, the component * member function: corresponds to the function * member variable: the corresponding attribute is ** // specific embodiment/* description of the handling process * the ranking of porters consists of attributes and functions * attributes: 1. number of students 2. gender * function: handle */class Man {int num; // member String sex; // member void function () // member {System. out. println (num + ".... "+ sex);}/* public static void main (String [] args) {// Man class is only used to describe a certain thing and runs only when it is used, it does not need to run independently. Of course it can also be //. Therefore, to use this thing, you only need to use it in a class, create an object and call it.} */}/* description Car * attribute: quantity, color * function, start */class Car {int num = 3; string color = "blue"; void run () {System. out. println (num + "... "+ color) ;}} public class Main {public static void main (String [] args) {/* to create an object, define the object name * // create a Man entity and use the new keyword to implement Man BLF1 = new Man (); // define a Man-type variable BLF2, man is a class, and BLF2 is a variable defined by a class. It is a class type variable that points to the Class Object BLF1.function (); // uses the content of the object, format: object. to call Car BLF2 = new Car (); BLF2.run (); // object. component }}
Not complete .......
Java object-oriented Learning
This is the most common situation when you study at a training institution.
The reason is generally two.
The first is the teacher's question. It is irrelevant to the teaching or too many technical terms are used to give lectures to beginners. Therefore, it is more difficult for students to understand the knowledge points of the course.
Second, the teacher spoke very well, the students were not easy to ask, and there were many reasons not to ask questions, this is because the details of each course are not clear, and the accumulation of the course will not be solid.
If you want to learn the knowledge points of each class well, I recommend you a method that I used to learn.
How can I read the most basic books I have bought?
Every day, the teacher is willing to talk about new things. What do I learn today?
Listen to the teacher's class first, and never let go of any questions. Be sure to ask after the teacher's class is over.
When I go home to flip the books, the teacher will read the chapters carefully.
There may be something the teacher did not talk about in the book. Don't let me go back and ask the teacher the next day
This will be of great help!
In addition, if you are far behind, ask the school to see if it can be re-studied. Generally, the school will accept it.
What is the java object-oriented learning method?
When the new subclass is created, the constructor of the parent class without parameters is called by default.
When a subclass calls a method, it first looks for it from the subclass. If the subclass does not have this method, it will look for it from the parent class, and the new parent class will not look for it from the subclass.
Abstract classes do not need to implement the methods in the interface, but if there are common subclasses under this abstract class, you must rewrite the methods in the interface in the ordinary subclass, if the abstract class has implemented the abstraction in Several Interfaces method, then the common subclass of this abstract class does not need to overwrite the method that the parent class in the interface has already rewritten,
Why do methods in an abstract class not need to be rewritten in the interface, because it is an abstract class, and the interface is full of Abstract METHODS? If the abstract class implements this interface, the abstract class has actually abstracted the interface. the methods are inherited, so the ordinary sub-classes under this abstract class must implement the methods in the interfaces not implemented by the abstract class.
Abstract classes must be abstract-modified, the methods in must also be abstract-modified, static methods can be defined in abstract classes, but cannot be simultaneously modified with abstract (1. Because static-modified methods and attributes it can be called directly through the class name, but it makes no sense to use static to modify the abstract method. Because the abstract method does not have a method body. 2. Because the subclass cannot override static modification method) and can only be modified by public and protected.
In a project, the top-level interfaces, the middle layer abstract classes, and the bottom layer general classes are generally designed.
Abstract class: class inheritance relationships are reversed, and classes become more common. The more the parent class is abstract, the more common the subclass is (specific ).
1. Use abstract modification. It cannot be new (instantiated ).
2. The parent class only declares methods without operating the logic in them. For example, this method is called an abstract method. Abstract methods are methods without method bodies.
3. If an abstract method exists in a class, the class must be an abstract class. Abstract classes do not necessarily have abstract methods.
4. A Class inherits the abstract class. If the parent class has an abstract method, the subclass must override the abstract method of the parent class ., If the subclass of an abstract class does not want to override the parent class method, the subclass must also become an abstract class.
5. abstract modification methods and static objects cannot be simultaneously modified.
6. abstract methods can only be modified using public and protected.
Interface:
1. An interface is a structure similar to an abstract class. It is more abstract than an abstract class. It is a pure abstract class.
2. It is modified by interface. The methods in the interface are abstract methods by default,
3. All attributes in the interface are static Constants by default. Use final static modification. It can only be a local variable. Initialization is required. Otherwise, an error is returned.
4. interfaces support multi-inheritance and cannot generate instances,
5. The methods in the interface cannot be modified using private or protected. The methods are all public abstract by default. When sub-classes implement interfaces, use the implements keyword. The abstract method must be overwritten. The interface cannot contain constructor methods.
Write more code about abstract classes and interfaces based on this document,