Well summarized object-oriented. Regret

Source: Internet
Author: User

What Is Object-Oriented:
Personal analysis: in programming, object-oriented is a relative statement, relative to process-oriented;
Oriented to "object": focuses on "object", while oriented to process: focuses on "process". Simply put, we consider things differently. For example: we judge whether a girl is beautiful or not. Some people value appearance, while others value "mind", but with different focuses.
For example, put an elephant in a refrigerator!

Process-oriented

Focus on a series of actions, that is, the process.) focus on the operator's ["object"])

"Open" refrigerator "Open
Mount an elephant
"Close" refrigerator "off

Class: Generally speaking, a class is the abbreviation of a class of things, and these things have at least a "common" feature. For example, an animal is a class.

Object: something that can be specifically described. For example, "dog" in an animal (class) is an object. (In my opinion, the object is relative to the class, because the dog can be divided into many different types, and the 'Dog' is a class relative to the 'husky, 'husky 'is a specific object of the 'Dog' class)

In our daily life, we all know that cars have four wheels and all have the running function.
It can be described as follows:
Vehicle type
Attribute: four rounds
Function: Run
So how to describe cars in Java?
ClassCar {
Int wheel = 4; // attribute: 4 rounds
Void run () {// method (function): run
System. out. println ("cars all run ");
}
}
It can be seen that "taxi" should be a concrete and real example of "car", that is, "instance") object, naturally, taxis also have the "common" attribute functions of cars!
How to Create a taxi object? JavaNew KeywordTo create an instance of a class, that is, object, or class instantiation, is to embody the class into an instance!
Format:Class Name object name = mew Class Name ()
CarTexi =NewCar ();
After the "taxi" object instance is created, it will have the followingSelf-ownedAttributes and functions.
Int wheel = 4; // attribute: 4 rounds
Void run () {// function: run
System. out. println ("I Am a taxi and I will also run ");
}
How can we access its own attributes and methods in an object?
Format:Object. OwnerObject. Method Name (); this also verifies that object-oriented programming focuses on "object;
Texi. wheel; texi. run ()
**************************************** **************************************** **************************************** **************************************** *****************

1. encapsulation:
It can be literally interpreted as sealing up and then installing it. To get the content, you must have a corresponding method to open it.
For example:If the door of a room is not locked, everyone can go in and get something, but now we don't want anyone to come in and get things. We canLock the door and provide only one key to the outside. Only those with the key can come in. This is a "encapsulation" phenomenon!

In java programs, how does one implement the "encapsulation" phenomenon?
This can be achieved:Modify some attributes and methods in the class with the private keyword, and "private means are not made public )", make other external classes unable to directly access these attributes and methods by creating such objects or using class names; only public methods are provided externally, other external classes must access the corresponding members of the class through the public methods provided by the class. For example:
Class Person {
Private String name;

// Private and encapsulate the name attribute) [equivalent to locking] so that other external classes cannot directly access
Public SetName (String name ){

// The public method provided externally is equivalent to the door key) for other external classes to access this class attribute
This. name = name;
}

}

2. Inheritance:
Literally, Inheritance refers to the abbreviation of the child-parent relationship, that is, the son and daughter can do the same.) It inherits some attributes and behaviors of Dad.
How to describe the inheritance relationship in java?
Note:First, you must know that the inheritance in java is between the class and the class or between the interface and the interface. The inheritance relationship is described using the extends keyword.
Example: Dad
Class Dad {
String sex = man;
Void smoke (){
System. out. println ("I will smoke! ");
}
}
So how can we make the son class inherit the father class?
Class Son extends Dad {// use the extends keyword Son. This class inherits the father class.
String name; // define your own attributes
String sex = man; // The inherited attributes, which need not be defined. It is written here for better understanding.
Void smoke () {// The inherited method, which is written here for better understanding.
System. out. println ("I will smoke! ");
}
Void playBall () {// define your own Method
System. out. println ("the son will play and smoke again! ");
}
Void somke () {// rewrite Method
System. out. println ("I don't smoke less than 20 cigarettes ");
}
Void smoke (int n) {// reload Method
System. out. println ("I smoked + n +" years of smoke ");
}
}
So what are the characteristics of the two classes with inheritance relationships?
1) The subclass inherits the attributes and methods of the parent class. Of course, the subclass can also define its own attributes and methods.
2) When the methods defined by the subclass are the same as those inherited by the parent class, the subclass will "Overwrite!
3) the method that the parent class uses the private keyword cannot be inherited!
4) sub-classes can use super to access the attributes and methods of the parent class.
5) The subclass modifies the function of the parent class by overwriting the parent class method.
6) The subclass constructor must call the parameter-free constructor of the parent class.
7) the constructor cannot inherit

3. Polymorphism:
The literal meaning is a variety of forms, that is, a thing is a variety of local forms at the same time.
Note::In case of polymorphism, the Class and Class must have an inheritance relationship, or the class and interface must be an implementation relationship.
In my opinion, polymorphism in java is relative to an object, that is, the parent type reference variable references the subclass object, and the parent type reference variable can accept objects of different subclasses.
Example: Take the above example
Dad uncle = new Son (); the parent class references the variable uncle, which is a member of the Son class and a member of the parent class; this is also called "upward Styling ";
Characteristics of Polymorphism
1) access member variables such as uncle. name during polymorphism;
When compiling, check the left side of the equal sign. If the parent class has this member variable, it is compiled. Otherwise, the compilation fails!
When running, check the left side of the equal sign. If yes, run the result.
2) access methods for polymorphism, such as uncle. smoke ()
When compiling, check the left side of the equal sign. If the parent class has this member method, it is compiled. Otherwise, the compilation fails!
When running, check the right side of the equal sign. The parent class has this method. The subclass does not have this method to execute the parent class method. The parent class has this method, and the subclass also has this method to execute the subclass method.
3) Access static methods during Polymorphism
For compilation and running, see the equal sign on the left.
**************************************** **************************************** **************************************** **************************************** *****************

4. abstraction (controversial ):
What is abstraction?
Abstraction is not specific.
Generally, we define things that cannot be described as abstract things. For example, if we say a person is very "abstract", can you describe it in detail?
How to describe the concept of abstraction in JAVA?
In java, abstract classes are derived from abstract methods. Why?
Because Java requires that an abstract method is defined in a class, the class must be defined as an abstract class, instead of defining the abstract class first and then following the instructions that the methods in the abstract class must be abstract, the final result may be the same, but the nature is different.) Simply put, classes with abstract methods must be abstract classes, but not all abstract classes must be abstract methods, you can also use a specific method.
First, when I want to define a method in a class, I do not know what the specific function of this method is; when I think about it, I will describe it in detail when I use it to implement a function. In this way, we can use abstract keyword to modify this method and define it as an abstract method.
When is the abstract class defined? It can also be said that abstract classes are extracted from the top, that is, several classes have common attributes and Methods. If you do not want each class to be defined once, define a public class, that is, an abstract class) other classes only need to inherit the abstract class and then rewrite the method !)
For example, I have one hundred yuan, but now I don't know how to use it. When I think about it, how can I use it!
Abstract class Money {// The abstract class must be defined as an abstract class because of the abstract method.
Public abstract void buySomthing (); // if you do not know how to use it, remove the function code body.
}
// Now I am hungry and want to buy something to eat. I will know how to use it.
Class Hungry extends Money {
Void buySomething (){
System. out. println ("I'm hungry. Now I want to buy something to eat! ")
}
}
Abstract class features
1) The abstract class cannot be instantiated.
2) abstract classes can inherit abstract classes.
3) The abstract method must be rewritten to inherit abstract classes.

5. What are the differences and similarities between abstract classes, interfaces, and common classes?
1) differences:
Ordinary Class interface abstract class
Objects can be directly instantiated. Objects cannot be directly instantiated. Objects cannot be directly instantiated.
Any member can be defined. Only abstract member methods and constant member methods can be defined as abstract. Others and common classes are the same.
An inherited class can implement interfaces, and multiple interfaces can be inherited.

Interfaces can inherit multiple interfaces. abstract classes have constructor methods, and interfaces cannot have
The abstract class has common member variables.
Abstract classes can have non-abstract methods, and interfaces cannot have
Abstract classes can be modified with public and protected. The interface can only be public by default)
The abstract class contains static methods. The interface does not contain
The variables in the interface can only be public static final, and the abstract class can be modified of any type.

2) similarities: they can all be used to describe (encapsulate) things,

What types of internal classes are there? How do I create objects separately?
Divided into local internal classes, anonymous internal classes...

1) External class name. Internal class name object name = new external class name). new internal class name)
2) External class name. Internal class name object name = External class name. new internal class name)
3)... I don't remember the 3rd types.
Anonymous internal class
Ii. Set
Set definition?
Set: the data structure used to save a group of data
What are the common methods for implementing the parent interface, implementation interface, and implementation class of the set? (We recommend that you use a tree chart to make your impression easier ).
Collection
List set
Arraylist rule list HashSet TreeSet


1. List Set
A List set is characterized by repeated and ordered elements. We can regard it as a dynamic array, which is generally accessed by subscript.
Implementation of the two most common sub-classes of ArrayList and rule list
1) list can be traversed using the for Loop
Forint I =; I <list. size (); I ++ ?) {
List. get (I );
}
2) list Replication
Call ArraryList. clone ()
Note: The list. clone () method cannot be used. The clone () method is a method of the Object class, and the list method is an interface that cannot inherit the Object class.

2. Set:
Non-duplicate and unordered set
Traversing a set can only be an iterator. Using an iterator to traverse a set follows the operation principle: first ask and then retrieve. Ask once.
Iterator <E> it = set. iterator ();
While (it. hashnext ()){
E e = it. next ();
}
3) map table
Map stores data using key-value pairs.
For Map, the key value must be unique. The value can be repeated.
When getting data, you can use the key to obtain its corresponding value.
There are three ways to traverse map
1) Traverse key
Set <String> keySet = map. keySet ();
For (String key: keySet ){
System. out. println ("key:" + key );
// Obtain the value corresponding to each key.
Int value = map. get (key );
2) traverse value
Collection <Integer> values = map. values ();
Iterator <Integer> it = values. iterator ();
While (it. hasNext ()){
Int value = it. next (); // map. next () cannot be used ()
System. out. println ("value:" + value );
Note: When traversing a set through the iterator, the number of elements in the set cannot be changed through the Set (that is, map. next ().
3) Traverse Key-value pairs
Set <Entry <String, Integer> entries = map. entrySet ();
For (Entry <String, Integer> entry: entries ){
// Obtain the keys and values of a set of key-value pairs
String key = entry. getKey ();
Int value = entry. getValue ();
System. out. println (key + "=" + value );

Collection interface method definition
Here I mainly list the use of the Iterator iterator () method
Collection provides the Iterator iterator () method to obtain the Iterator
Iterator is an interface that defines the methods that should be used to traverse a set.
Use the iterator to traverse the set following the operation principle: first ask and then take. Ask once.
Q: boolean hashNext ()
Get: Object next ()
The iterator can delete the retrieved data.
Delete: remove () deletes the currently obtained element.
Note: 1) the elements retrieved by next () are deleted. And can only be deleted once!
2) After one next () call, you can only call remove () once ()!

This article from the "gorgeous JAVA turn" blog, please be sure to keep this source http://teny32.blog.51cto.com/8027509/1337846

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.