Three object-oriented features of Java

#HashMap# 2022-01-26 22:58:06 阅读数:576

object-oriented object oriented features java

I finished learning the basic knowledge of object-oriented this week , This includes packaging , Inherit , Polymorphism and JDK8 Characteristics of , among Lambada Expressions and functional interfaces are the key points. Next, let's talk about my experience

1.Java Three characteristics of object-oriented in

1.1 encapsulation
1.1.1 background : First, let's understand what encapsulation is , My understanding is like a computer , It consists of a lot of hardware CPU, register , Memory modules and other circuits , Imagine what would happen if we exposed the shell of the computer , First, your computer is easily damaged inside , Second, it is very dangerous for people to touch it , So we will think of using a layer of shell packaging , Keep it simple , Expose what you need , Unnecessary hiding
effect : From the previous discussion, encapsulation realizes the division of labor , Second, hidden information , Implementation details . By controlling access, you can hide information that you don't want client programmers to see , Such as a customer's
The bank password needs to be kept confidential , You can only develop permissions for this customer .
1.1.2 private,default,protected,public Authority

keyword Same class Same package Subclass All classes
private Sure
default Sure Sure Subclasses under the same package
protected Sure Sure All subclasses
public Sure Sure Sure Sure

1.2 Inherit
Inheritance should be easier to understand , Is that a son inherits his father's work , Think you are Wang Sicong , Your father is Wang Jianlin , Then you can inherit your father's billions , Switch to java The point of view is that the child object can have all the properties and methods of the parent object , Here's an explanation if private Modification is not inherited , For inheritance extends keyword , Can greatly improve the reusability of code .

class Father{

private String name;
private boolean gender;
public void test(){

class Son extends Father{

// Have Father All attributes of 

1.3 polymorphic
understand : Polymorphism is a fuzzy strategy , Maintaining the status quo , It's like USB As the interface , The mouse can be used , Mobile phone charging can also , This is the mystery of polymorphism , The concept of polymorphism developed , It's based on encapsulation and inheritance , Children appear as parents , But do things in your own way . Subclasses appear as parent classes and need to be transformed upward (upcast), The upward transformation is caused by JVM Automatically , Is safe , But the transition down (downcast) It's not safe , Cast required . When a subclass appears as a parent, its own properties and methods cannot be used .
Conditions ; Inherit , There should be rewriting , The parent class reference points to the subclass object
The three principles :1) Compile to see type + Determine the method sheet , Run find object
2) The principle of nearest best : I didn't find my father
3) There is polymorphism , The base class is not visible to the new method of the subclass
1.4 Static binding versus dynamic binding
Static binding ( Static binding ): Complete at compile time , Can improve code execution speed . Static binding methods include :

  1. Static methods
  2. Constructors
  3. private Method
  4. With keywords super Method called
  5. Dynamic binding ( Dynamic linking ): Referring to “ During execution ( Not during compilation )” Determine the actual type of the referenced object , Call its corresponding method according to its actual type . This, though, gives us the flexibility to program , But it reduces the execution speed of the code . This is also JAVA Than C/C++ One of the main factors of slow speed

2. Abstract classes and interfaces

2.1 super,final keyword
super:this contrast ,this The current object ,super The parent object of the current object
final: decorator , Indicates that the class cannot be inherited
Modification methods , Indicates that the method cannot be overridden
Modify properties , Represents a constant , Once the value is determined, it cannot be changed
2.2 instance of
Move down — Coercive transformation , There are risks , Type conversion exceptions may occur ClassCastException, To reduce this risk , We can use Java Medium instance Operator , Make a judgment before strong turn .

class Father{

class Son{

public static void main(String[] args){

Son s = (Son)new Father();// Strong transfer error , So I want to use instance of
Son s1 = new Son();// Sure 
Father f = new Son();// Can be forced to turn 
// Remember this format 
if(f instance of Son){

Son s2 = (Son)f;//f Forced to Son

2.3 abstract class
use abstract Keyword to modify a class , This class is called abstract class
Condition of occurrence :1) Classes with abstract methods must be declared as abstract classes , Abstract classes must be inherited ( It makes sense ),2) Abstract methods must be overridden .3) Abstract classes cannot be instantiated. Abstract methods only need to be declared without implementation
Define format :A: The format of abstract method definition :public abstract return type Method name ( Parameters );
B: The format of the abstract class definition :
abstract class Class name {
2.4 Interface
Definition : essentially , Interface is a special abstract class , It's a function , A norm
features :i) The declared property in the interface defaults to public static final Of , It can only be public static final Of ;
ii) Only abstract methods can be defined in an interface , And these methods default to public Of , It can only be public Of
iii) Interfaces can inherit other interfaces , And add new properties and abstract methods
iiii) Interface can't implement another interface , But you can inherit multiple other interfaces
Be careful : Interfaces and abstract classes are implementation relationships

3.JDK New characteristics

3.1 Anonymous inner class
3.1.1 Anonymous inner class : If an instance of a class is used only once , You can compare the definition of a class with that of a class The creation of , Put it together with , Or create a class while defining a class , An unnamed class defined in this way becomes an anonymous inner class .
(1) structure ;
public Interface Inner{
void test();
public static void main(Srting[] args){
Inner i = new Inner(){
public void test(){
(2) Evolved from anonymous inner classes Lambda expression , Need a parent class or interface to exist , And there is only one abstract method ( Be careful : use default Modification is a common method )
3.2 Lambda expression
(1)lambda An expression is a method without a name , use ( )->{} Express
(2)Lambda Expressions require support from functional interfaces
(3) It is a simplified form of an anonymous inner class
(4) classification
I) No parameter , No return value ( )->{ }
II) With parameters , No return value ( Parameters )->{ }
III) With parameters , With return value ( Parameters )->{ return Return type }
IIII) No parameter , There is a return value ( )->{ return Return type }
3.3 Functional interface
3.3.1 There is only one abstract method interface in the interface , This interface is called a functional interface . Can pass @FunctionalInterface To verify that
3.3.2 Four core functional interfaces
I) Consumer Consumer void accept(T t);
II) Supply type Supplier T get();
III) functional Function<T,E> T apply(E e);
IIII) Break the pattern Predicate boolean test(T t);
3.4 Method reference
3.4.1 object :: Instantiation method ( Non static methods )
3.4.2 Class name :: Static methods
3.4.3 Class name ::new
3.5 Object(Object Is the root and base class of all classes )
3.5.1 toString( Print out the address code of the object )
3.5.2 equals( Compare whether the contents of the two objects are consistent , It's usually rewritten )
3.5.3hashCode( The value returned by a certain calculation ) 4.clone( Shallow cloning of an object )

copyright:author[#HashMap#],Please bring the original link to reprint, thank you.