First, the package
Encapsulation refers to the properties of hidden objects and implementation details, only provide interface visible. The encapsulation realizes the information hiding, facilitates the software reuse. The advantage is that it achieves the modular standard, which improves the reusability of the code. In a way, encapsulation also greatly improves the maintainability of software development and reduces the risk of building complex software systems.
public class TV {
private int volume;
private int color;
public int Getvolume () {
return volume;
}
public void SetVolume (int volume) {
This.volume = volume;
}
public int GetColor () {
return color;
}
public void SetColor (int color) {
This.color = color;
}
public void TurnOn () {
Turnon_impl ();
}
Boot internal implementation
private void Turnon_impl () {
//...
}
}
The code above defines a TV class TV with two properties: Volume volume, color colour, and some methods defined. As you can see from the properties of a class, the volume volume, the color colour attribute is defined as private, and can only be manipulated by related methods. Class TV encapsulates its attribute volume, color.
Ii. inheritance
Inheritance refers to the derivation of a class from another class, which automatically has all the attributes of the inherited class. The inherited class is called the parent class, and the class that inherits the parent class is called a subclass. Inheritance can be a subclass with various properties and methods of the parent class, without having to write the same code again. Inheritance also greatly facilitates the reuse of software, and the form of inheritance can be more complex.
public class GENERALTV extends tv{
public static void Main (string[] args) {
GENERALTV TV = new Generaltv ();
Tv.setvolume (1);
System.out.println (Tv.getvolume ());
}
}
GENERALTV inherits TV, so it has the properties and methods of TV.
Three, polymorphic
Polymorphism means that for a service function, it can be implemented in different ways, that is, multiple forms. Polymorphic image describes the complex form of the real world, which makes the program have good expansibility. On the basis of inheritance, method overloading is one of the ways to implement polymorphism.
public class Human {
public void SayHello () {
}
}
Class Chinese extends human{
public void SayHello () {
System.out.println ("Hello");
}
}
Class American extends human{
public void SayHello () {
System.out.println ("Hello");
}
}
Class testclass{
public static void Main (string[] args) {
Chinese c = new Chinese ();
C.sayhello ();
American a = new American ();
A.sayhello ();
}
}
The above code first defines the parent class human, only has one method, the method does not have the concrete implementation. Then, we define two subclasses Chinese and American respectively, inherit the class human, and all of the SayHello methods are rewritten and have their own different implementation modes.
Three features of Java: Encapsulation, inheritance, and polymorphism