Java Come on, come on, come on! ! ! ! !

Java basics
1, three major features, encapsulation, inheritance, polymorphism
2, exception
3, multithreading
4, collection
5, reflection
6, network
7, IO flow

1. Three major features
Encapsulation
Understanding: privatize class member variables and provide external access interfaces.
Features:
1. Set member variables in the class as private and only expose public access interfaces, which enhances the security of the class and avoids arbitrary modification by the outside world
2. Decoupling, able to pay more attention to details without changing the interface itself.
Disadvantages:
1. Easy to be broken, such as inheritance
Example:
/** * @author wys * @date 2021/1/12 5:27 PM / public class User {private String name; private Integer age; public String getName() {return name;} public void setName(String name) {this.name = name;} public Integer getAge() {return age;} public void setAge(Integer age) {this.age = age;}} Copy code
Inheritance
Understanding: Subclass inherits from parent class
Features:
1. Subclass inherits from parent class through the extends keyword
2. Through inheritance, class reuse is realized, avoiding a lot of code duplication
Disadvantages:
1. Inheritance destroys encapsulation. If the subclass depends too much on the parent class, it will cause coupling.
Example:
/
* * @author wys * @date 2021/1/12 5:46 pm* For example, our mobile phones can be divided into different brands, but each brand must have a name and price* so that a universal mobile phone can be abstracted To define the name and price / public class Phone {private String name; private BigDecimal price; public String getName() {return name;} public void setName(String name) {this.name = name;} public BigDecimal getPrice() {return price;} public void setPrice(BigDecimal price) {this.price = price;} @Override public String toString() {return “name:”+this.getName()+“price:”+this.getPrice();} } public class MiPhone extends Phone{ public void desc() {System.out.println("This is a Xiaomi phone");}} public class IPhone extends Phone{ public void desc() {System.out.println("This is iPhone"); } } /* * @author wys * @date 2021/1/12 5:50 PM*/ public class Main {public static void main(String[] args) {MiPhone mi = new MiPhone(); mi.setName("小米手机" ); mi.setPrice(new BigDecimal("2000")); System.out.println(mi.toString()); IPhone iPhone = new IPhone(); iPhone.setName("Apple mobile phone"); iPhone.setPrice( new BigDecimal(“8000”)); System.out.println(iPhone.toString());}}
Inherited rules:

Single inheritance: A class can only inherit one parent class.
Multiple inheritance: A class can have multiple hierarchical relationships, such as B inherits A and C inherits B
polymorphism.
Understanding: multiple forms of a thing, different instance objects of the same interface

Prerequisites: Inheritance, rewriting, and parent class references pointing to subclasses
Advantages:
1. Improved code expansion
2. Uncoupling relationships between types
Disadvantages:
1. Parent classes cannot use subclass-specific functions
Example:
/ ** * @author wys * @date 2021/1/13 10:29 AM*/ public interface Phone {void desc();} public class IPhone implements Phone{ @Override public void desc() {System.out.println( "Apple mobile phone");}} public class MiPhone implements Phone{ @Override public void desc() {System.out.println("小米手机");}} Copy the code
Keyword
this:
this refers to the object of the current class, For example, the subclass inherits the parent class and rewrites the method of the parent class. You can use the this keyword to call the subclass method
super:
super refers to the parent class object, and you can call the parent class method
final:
modified position:

Class: Class cannot be inherited
Method: cannot be overridden
Variable: cannot be changed
Modified reference type: The memory address of the reference type is immutable, and the value pointed to can be changed.
The final modified variable will be directly placed in the variable pool. If a class calls the final modified variable
, the difference between the abstract class and the interface will not be loaded :

Abstract, use extends keyword to inherit abstract class, subclass use, use implements keyword to realize interface.
Abstract class can have abstract method and non-abstract method, interface can only have abstract method, but interface has default keyword after jdk1.8. Method body can be written.
A class can only inherit one abstract class, and a class can implement multiple interfaces
. Neither interface nor abstract class can be instantiated.
Class instance initialization process:

Parent class static properties
Parent class static code block
Subclass static properties
Subclass static code block
Parent class ordinary code block
Parent class ordinary member variable
Parent class construction method
Subclass ordinary code block
Subclass ordinary member variable
Subclass construction method
2, Exception
3 , Multithreading
4, Collection
5, Reflection
6, Network
7, IO stream

Guess you like

Origin blog.csdn.net/dcj19980805/article/details/114932472