首頁 > Java > java教程 > 主體

Java程式設計思路方法的總結

巴扎黑
發布: 2017-07-17 16:17:41
原創
2931 人瀏覽過

  總覺得書中太囉嗦,看完總結後方便日後回憶,本來想偷懶網上找別人的總結,無奈找不到好的,只好自食其力,盡量總結得最好。

 

物件導論

看到物件導論覺得這本書

 

目錄:

1.1 抽象過程
1.2 每個對象都有一個介面
1.3 每個物件都提供服務
1.4 被隱藏的具體實作
1.5 復用具體實作
1.6 繼承
1.7 伴隨多型的可互換物件
1.8 單一繼承結構
1.9 容器
1.10 物件的建立與生命期
1.11 例外處理:處理錯誤
1.12 並發程式設計
1.13 Java與Internet
1.14 總結


覺得看完終於要精通Java了,然而本章只是介紹開發方法概述在內的OOP的基本概念,了解對象的重要性。


1.1 抽象過程

#透過其他語言的缺點來說明面向對象語言的好。

彙編語言是對底層機器的輕微抽象、命令式語言(如C、BASIC)是對彙編語言的一種抽象,彙編語言直接控制電腦的硬件,命令式語言則基於電腦結構解決問題。 OOP語言基於問題的結構解決問題,不會受限於任何特定類型的問題。

1.2 每個物件都有一個介面

#介面:確定了對某一特定物件所能發出的請求     物件:型別名稱

看文字描述已經上升到哲學問題,從下面例子很好理解。

Light lt = new Light(); //对象lt.on;//接口向对象发出请求
登入後複製

 

1.3 每個物件提供服務

好處:1、有助於提升軟體的內聚性  2、每個物件都可以很好的完成一項任務,但它並不試圖做更多的事情。

理解:設計一個音樂播放器,有歌詞顯示、播放,暫停、背景顯示(服務),這時不要只提供一個物件(它不試圖做更多的事情),可以提供三個對象,完成三個服務,三個對象提供三個服務完成一個音樂播放器(內聚性)。

1.4 被隱藏的具體實現

#從Github下載一個框架,我的目標是實現快速應用開發,框架只需提供我方法的呼叫即可,其他的隱藏了也不會影響我的呼叫

存取權:public > protected(套件+基底類別) > 套件存取權限(沒有關鍵字時預設) > private

#1.5 重複使用具體實作

複用指在一個類別中使用繼承或組合。

  • 繼承----is a 的關係     荔枝是水果

  • ##組合- ---has a 的關係   有一種睡覺的方式是趴著

#1.6 繼承

################################ ###從父類衍生出子類,子類能吸收父類的資料屬性和行為,並能擴展新的能力。 ###############1.7 伴隨多態的可互換物件############ ######
class Shape{ 
  draw();
  erase();
  move();
  getColor();
  setColor();
}
登入後複製
#######
void doSomething(Shape shape){
shape.erase();//...shape.draw();
}

Circle circle = new Circle(); //父类为ShapeTriangle triangle = new Triangle();  //父类为ShapeLine line = new Triangle();  //父类为ShapedoSomething(circle);
doSomething(triangle);
doSomething(line);
登入後複製
###

对doSomething的调用会自动地正确处理,而不管对象的确切类型(可互换对象)。

doSomething(Shape shape)的执行是指你是Shape类或者父类为Shape,而不是你是Circle类就执行这样,你是Triangle 类就执行那样。理解了可以去看设计模式之策略模式。

这里还涉及到向上转型,如下图:

 

1.8 单根继承结构

 1、所有类都继承自单一的基类

public class JianCheng extends Object {  
}
登入後複製
public class JianCheng {  public static void main(String[] args) {  
        JianCheng jiancheng= new JianCheng();  
        System.out.println(JianCheng instanceof Object);  
    }  
}
登入後複製

Output: true //ExplanationJianCheng class inherits by defaultObject

2. Ensure that all objects have certain functions

Object methods will be inherited by subclasses, such as: clone(), equals( Object obj), toString() and other methods.

3. Garbage collection becomes easy

The object is guaranteed to have its (Object) Type information, so you don't get stuck in an inability to determine the type of an object. This is important for system-level operations (such as exception handling).

1.9 Container

holds access to other objects References are called containers (collections), such as List (used to store sequences), Map (also known as associative array, used to establish associations between objects), Set (only one of each object type is held), and such as Queues, trees, stacks and more.

Comparison between ArrayList and LinkedList, the former is the shape of an array, randomly accessing elements has a small overhead, but insertion and deletion operations have a high overhead. The latter is in the shape of a linked list, insertion and deletion operations are easy to operate.

1.10 Object Creation and Lifetime

Understanding The difference between objects placed on the stack and the heap

  • Allocation and release between stack-- are given priority Position,sacrifice flexibility, becausemust know the exact number, lifetime and type of objects.

  • ##Heap--dynamically created objects in the memory pool at runtime Know the number, lifetime and type of objects. Dynamic management requires a lot of time to allocate storage space in the heap, but creating storage space and releasing storage space is very convenient.

#Java adopts By using the new keyword to create an object using dynamic memory allocation, the compiler can determine how long the object will survive and automatically destroy it through the "garbage collector mechanism".

##1.11 Exception handling: handling errors

Exception is an object that is thrown from the error location and is caught by a specific type of error exception handler, through try--catch or throw. Exception handling is like another path that is executed when an error occurs, parallel to the normal execution path of the program.

If the Java code does not write the correct exception handling code, you will get a compile-time error message. For example: IOException, ClassCastException (class conversion exception), NullPointerException (null pointer exception), etc.


1.12 Concurrent ProgrammingProcess multiple processes at the same time The idea of ​​tasks is to run in multiple threads.


There is a hidden danger in synchronous multi-threaded operation, shared resources. A originally wanted to use a=Love You, but a certain thread led to a=hate you and then A used it, so A's confession will definitely fail.

1.14 Summary


##The first chapter is all theoretical knowledge, and many knowledge points are obviously easy However, the long explanation makes it difficult to understand. There is some practical information but it is mixed with too much fluff.

以上是Java程式設計思路方法的總結的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板