Encapsulation 封装
Encapsulation is hide the internal implementation, and stabilize the external interface. It can hide the implementation details and make the code modular.
eg.
public int max(int a, int b) {
return a > b ? a : b;
}
int a = max(1, 5);
int b = max(10, 15);
int max = max(a, b);
Inheritance
Inheritance can expand the code modular, it used for resue code.
eg.
public class Person {
String ID;
String Name;
Boolean Gender;
String Birthday;
}
public class Student extends Person {
String SID;
String major;
String Department;
}
The Student inherit attributes from the SuperClass -- PersonClass.
Polymorphism
1、overload
The functions have the same method name, but they have different parameters.
eg.
public int max(int a, int b) {
return a > b ? a : b;
}
public int max(int a, int b, int c) {
int max = a > b ? a : b;
return = max > c ? max : c;
}
2、 It just happened in the case of inheritance.
eg.
public class Person {
String ID;
String Name;
Boolean Gender;
String Birthday;
public void introduction() {
System.out.println("My name is " + Name ", my Birthday is " + Birthday + ".");
}
}
public class Student extends Person {
String SID;
String major;
String Department;
//override the function
public void introduction() {
System.out.println("My name is " + Name + ".");
}
}
Delegation
The delegation has two functions: one is transmit values
, and one is transmit the event
.
It can reduce the complex of the code.
eg.
public class A {
B b = new B();
}
public class B {
//B want to transmit a value to A.
//But B can't communicate with A by directory.
//so here use the delegation to help transmit value.
}