This article participated in the Sifu technical essay , and you are welcome to join.
In the two years that I have been working on the front end, I have also been in touch with the back end field, such as PHP
Node
, and have used them to do some things demo
and 私活
,
From the perspective of career development, it is good to understand the back-end. You can be proficient in the development process before and after. After the whole link is down, it is very clear. If you understand the back-end, it is good for you to start a business or take over private work in the future. Web
后台领域, Java
, ---0107660006b1310249d11dd4c7bface0 Spring
天下, Java
Now, from the front end to the full stack
This is what I spent a week reviewing recently Java 面向对象
summary, I hope it can help you.
Guide to reading
learning route
This learning route is summarized by me following the main video of an up, and it is very comprehensive.
Relearn Java
1. Object-oriented OOP
1. 1 The difference between super
and this
super
-
super
call the constructor of the parent class, it must be the first in the constructor of the subclass -
super
must only appear in subclass methods or constructors -
super
andthis
cannot both appear in a call to a constructor
this
The objects represented are different:
-
this
: The object of its own caller -
super
: represents the application of the parent class object
1.2 Polymorphism
The same method can behave in many different ways depending on the object being sent.
The actual type of an object is determined, but there are many types of references that can point to an object.
Polymorphism Existence Condition
- have an inheritance relationship
- Subclass override superclass method
- The parent class reference points to the child class object
Notice
- If the subclass overrides the parent class method, the parent class object is the rewritten method of the called subclass
type conversion
Purpose of type conversion: convenient method call, downgrade, upgrade, reduce code duplication, but also have disadvantages, loss of precision
- Upward transformation (child to parent) Low to high can be directly transferred
Downcast (parent rotor) High to low requires a cast
After high to low, and then forced conversion,
子类型 子对象 = (子类型) 父对象
Person per = new Student()
Student student = (Student) per // ((Student) per).方法/属性
per.子属性和方法
1.3 static
1.3.1 static
Use
Call methods and properties (static) without creating an object.
static
can be used to modify the member methods of the class and the member variables of the class, and can also write static code blocks to optimize program performance.
1.3.2 static
static method
-
static
Methods are generally called static methods. Static methods can be accessed without relying on any objects. Therefore, for static methods, there is no this. - Non-static member variables and non-static member methods of the class cannot be accessed in static methods, because non-static member methods/variables must depend on specific objects to be called.
- Static member methods/variables can be accessed in non-static methods.
1.3.3 static
Static variables
The difference between static variable and non-static variable is
- Static variables are shared by all objects and have only one copy in memory, which is initialized if and only when the class is first loaded.
- Non-static variables are owned by the object and are initialized when the object is created. There are multiple copies, and the copies owned by each object do not affect each other.
The initialization order of static member variables is initialized in the order in which they are defined.
1.3.4 static
code block
static
keywords to form static code blocks to optimize program performance,.
static
blocks can be placed anywhere in a class, and a class can have multiple static
blocks. When the class is first loaded, each ---ac20e6a43fd0e03b7a657e7114bd9431 static
block is executed in the order of the static
block, and only once.
You can put the constants in the method into the static block, so that it will not be recreated every time it is called, causing a waste of space.
public class Test {
private static Date date ;
private static SimpleDateFormat formatter;
static {
formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
date = new Date(System.currentTimeMillis());
}
public static void main(String[] args) {
getTime();
}
public static void getTime(){
System.out.println(formatter.format(date));
}
}
The above example is used to get the current time. When calling the getTime
static method, it will not be created repeatedly Date
SimpleDateFormat
Execution order : over constructors
1.3.5 static
NOTE
-
static
关键字不能会改变类中成员的访问权限, 能改变访问权限只有private
、public
、protected
. By
this
can access static methods and static members in non-static methodspublic class Test { private static Date date ; private static SimpleDateFormat formatter; private static String name = "前端自学社区"; public static void main(String[] args) { Test test = new Test(); test.getStatic(); } public static void func(){ System.out.println("我是静态方法"); } public void getStatic(){ System.out.println(this.name); this.func(); } }
In the example, in the non-static method, you can directly access the static methods and static members through
this
, and then through the class instantiation, the object can be obtained through the non-static method to obtain the static method. Static properties and methods.
1.4 Abstract class abstract
Let's start with an abstract method. An abstract method is a special kind of method: it only has a declaration, but no concrete implementation .
The declaration format of an abstract method is:
abstract int getNum();
1.4.1 What is an abstract class?
A class modified by the abstract
keyword is an abstract class, which contains attributes and abstract methods.
Abstract classes contain methods that have no concrete implementation, so you cannot use abstract classes to create objects.
When a subclass inherits an abstract class, it must implement the abstract methods of the abstract class, and the instantiated objects of the subclass have the attributes of the abstract class .
public abstract class Animal {
abstract String name;
abstract void eat();
}
1.4.2 What is the role of abstract class?
Abstract classes are mainly used to extract the commonality of things, extract attributes and methods, and subclasses inherit abstract classes to improve code efficiency.
1.4.3 Difference between abstract class and ordinary class
- The abstract method must be public or protected (because if it is private, it cannot be inherited by subclasses, and subclasses cannot implement the method), and the default is public by default.
- Abstract classes cannot be used to create objects;
- If a class inherits from an abstract class, the subclass must implement the abstract method of the superclass . If the subclass does not implement the abstract method of the superclass, the subclass must also be defined as an abstract class.
1.5 Interface interface
1.5.1 What is an interface?
It is an abstraction of behavior, and interfaces are usually declared with interface
. A class inherits the abstract methods of the interface by inheriting the interface.
When a class needs to use these abstract behaviors, it needs to implement the interface implements
, and it must implement the methods in the interface.
public interface person {
void study(){}
}
1.5.2 Interface Features
Each method in the interface is also implicitly abstract, and the method in the interface will be implicitly specified as
public abstract
(onlypublic abstract
, other modifiers will report an error). You can also not write it, the default ispublic abstract
.public interface Aniaml{ public abstract void eat(); // 抽象方法 == 等同于 void eat(); }
- The interface can contain variables, but the variables in the interface will be implicitly specified as
public static final
variables (and can only be public, and a compilation error will be reported when modified with private). - The methods in the interface cannot be implemented in the interface, only the class that implements the interface can implement the methods in the interface.
1.5.4 The role of the interface
- Using an interface allows you to inherit from more than one source, which allows you to combine different inheritance hierarchies for different needs.
- An interface specifies the methods and properties that the implementing class must implement.
1.6 Interfaces and Abstract Classes
In Java, the abstraction of ---a872aca5b224be1b737d2138354cf9e5 OOP
can be represented in two forms: 接口
and 抽象类
, which have many similarities.
1.6.1 Difference between interface and abstraction
- Abstract classes can provide the implementation details of member methods, while only
public abstract
methods exist in interfaces; - Member variables in abstract classes can be of various types, while member variables in interfaces can only be of type
public static final
; - Interfaces cannot contain static code blocks and static methods, while abstract classes can have static code blocks and static methods;
- A class can only inherit from one abstract class, but a class can implement multiple interfaces.
-
interface
Interface is to abstract the behavior [methods] of things, and abstract classes are to abstract the behavior and properties of things in common.
1.6.2 Examples
interface interface
public interface PeopleInterface {
void eat();
void study();
void play();
}
abstract class
public abstract class People {
String name = "人类";
abstract void watch();
}
Subclass
public class Test extends People implements PeopleInterface {
public static void main(String[] args) {
Test test = new Test(); // 子类拥有抽象类的属性
System.out.println(test.name);
test.eat();
}
@Override
public void eat() {
System.out.println("吃的功能");
}
@Override
public void study() {
System.out.println("学习功能");
}
@Override
public void play() {
System.out.println("玩的功能");
}
@Override
void watch() {
System.out.println("看的功能");
}
}
The above example talks about the relationship between the class and the interface abstract class. First Test
class inherits People
abstract class, and then implements PeopleInterface
interface.
When a subclass inherits an abstract class, it must implement the abstract methods of the abstract class. When a subclass is instantiated, it has the attributes of the abstract class.
When a subclass implements an interface, it must implement all the interface behaviors in the interface.
Epilogue
The above is a summary and sharing of Java 面向对象
, I hope it can inspire you, and comments are welcome!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。