Java的三大特性:多态、封装、继承。

Java程序设计尊崇的思想:高内聚、低耦合。

多态性:Java官方给出的定义:

  The dictionary definition of polymorphism refers to a principle in biology in which an organism or species can have many different forms or stages. This principle can also be applied to object-oriented programming and languages like the Java language. Subclasses of a class can define their own unique behaviors and yet share some of the same functionality of the parent class.

  它的大义是说:多态性的本义是指生物学中的一个定律,一个生物体或者物种可以有不同的形式或者阶段。这个定律同样适用于面向对象编程和像Java一样的语言,一个类的子类可以定义自己独特的行为并且共享一些父类中共同的功能。

  我们再来理解一下它的定义,“一个类的子类可以定义自己独特的行为并且共享一些父类中共同的功能”,我们就可以联想到它的实现方法:继承和接口实现,单一继承,多重实现,这样我们就把三大特性联系起来了,我们联系生活中的一些实例巩固和实践我们的定义,比如书,书有很多种,但他有一些共同的特性,比如书名、作者、前言、目录等和公共方法,比如读,我们就可以放在父类中,子类通过继承直接使用,但是不同的书有不同的读法和它自己的一些独有的属性,比如数学书,独特的属性:数学公式,自然读数学书跟读哲学书的方法就不一样,我们来看一个例子。

公共父类:Book.java

 1 /**
 2  * @Description: 书(父类)
 3  * @author: vioking
 4  * @date: 2017-7-12
 5  */
 6 public class Book {
 7 
 8     private String title;//书名
 9     private String author;//作者
10     private String bookType;//类型
11 
12     //默认构造方法
13     public Book() {
14     }
15 
16     public Book(String title, String author, String bookType) {
17         this.title = title;
18         this.author = author;
19         this.bookType=bookType;
20     }
21 
22     /**
23      * 公共方法
24      */
25     public void read(){
26 
27         System.out.println("公共父类的方法");
28     }
29 
30     public String getTitle() {
31         return title;
32     }
33 
34     public void setTitle(String title) {
35         this.title = title;
36     }
37 
38     public String getAuthor() {
39         return author;
40     }
41 
42     public void setAuthor(String author) {
43         this.author = author;
44     }
45 
46     public String getBookType() {
47         return bookType;
48     }
49 
50     public void setBookType(String bookType) {
51         this.bookType = bookType;
52     }
53 }
View Code

相关文章:

  • 2021-07-05
  • 2021-05-15
  • 2021-11-04
  • 2022-12-23
  • 2022-12-23
  • 2021-11-01
  • 2021-08-14
  • 2022-02-24
猜你喜欢
  • 2021-06-17
  • 2022-12-23
  • 2021-12-08
  • 2022-12-23
  • 2021-08-28
  • 2021-05-17
  • 2022-12-23
相关资源
相似解决方案