-
encapsulation
An important principle of the object-oriented approach is Encapsulation, which combines the properties and operations (or services) of an object into an independent whole and hides the internal implementation details of the object as much as possible.
Public class Student {// private String name; private String id; private String sex; Public String getName() {return name; } public String getId() { return id; } public String getSex() { return sex; } public void setName(String name) {this.name = name; } public void setId(String id) { this.id = id; } public void setSex(String sex) { this.sex = sex; }}Copy the code
-
inheritance
Inheritance is the derivation of a new class from an existing class, which can absorb the data attributes and behavior of the existing class and extend the new capabilities.
The Person the parent class
public class Person { public int a=1; public void talk(){ System.out.println("Person"); }}Copy the code
Student subclass
public void say(){ System.out.println("Student"); }}Copy the code
The test case
public static void main(String[] args) { Student student=new Student(); System.out.println(student.a); // Inherits the parent class member variable student.say(); // Subclass extension method student.talk(); }} output: 1 Student PersonCopy the code
-
polymorphism
There are three necessary conditions for the existence of polymorphism:
- inheritance
- rewrite
- A parent class refers to a subclass object
The Person the parent class
public void say(){ System.out.println("Person"); } public void run(){ System.out.println("run"); }}Copy the code
Student subclass
Override public void say() {system.out.println ("Student"); } public void talk(){ System.out.println("myStudent"); }}Copy the code
The test case
Public class Application {public static void main(String[] args) {public static void main(String[] args) {Student Student =new Student(); Person person=new Student(); Person person1=new Person(); student.say(); // Call Student's say() person.say(); // Call Student say() person1.say(); // call Person say ()}} output: Student Student PersonCopy the code
Original link: www.sdk.cn/details/wAK…
SDK is a neutral communities, there are a variety of front knowledge, has the rich API, developers have a love of learning artificial intelligence, have humor developers take you learn python, there will be the next hot HongMeng, when various elements together, let us together to build professional, fun and imaginative valuable developer community, Help developers realize their self-worth!