Thinking in Java——筆記(1)

来源:http://www.cnblogs.com/apolloqq/archive/2016/11/23/6092622.html
-Advertisement-
Play Games

Introduction To Obejct ___ The progress of abstraction + But their primary abstraction still requires you to think in terms of the structure of the co ...


Introduction To Obejct


The progress of abstraction

  • But their primary abstraction still requires you to think in terms of the structure of the computer rather than the structure of the problem youare trying to solve.
  • The alternative to modeling the machine is to model the problem you’re trying to solve.
  • We refer to the elements in the problem space and their representations in the solution space as “objects.”
  • when you read the code describing the solution, you’re reading words that also express the problem.
  • OOP allows you to describe the problem in terms of the problem, rather than in terms of the computer where the solution will run.
  • Five basic characteristics:
    1.Everything is an object.
    2.A program is a bunch of objects telling each other what to do by sending messages.
    3.Each object has its own memory made up of other objects.
    4.Every object has a type.
    5.All objects of a particular type can receive the same messages.
  • An object has state, behavior and identity: an object can have internal data (which gives it state), methods (to produce behavior), and each object can be uniquely distinguished from every other object(address in memory).

An object has an interface

  • Objects that are identical except for their state during a program’s execution are grouped together into “classes of objects,” and that’s where the keyword class came from.
  • Each object belongs to a particular class that defines its characteristics and behaviors.
  • You extend the programming language by adding new data types specific to your needs.
  • Any program is a simulation of the system you’re designing.
  • One of the challenges of object-oriented programming is to create a one-to-one mapping between the elements in the problem space and objects in the solution space.
  • The requests you can make of an object are defined by its interface, and the type is what determines the interface.
  • A type has a method associated with each possible request, and when you make a particular request to an object, that method is called.

An object provides services

  • Your program itself will provide services to the user, and it will accomplish this by using the services offered by other objects.
  • Thinking of an object as a service provider has an additional benefit: It helps to improve the cohesiveness of the object.
  • In a good object-oriented design, each object does one thing well, but doesn’t try to do too much.
  • If they can see the value of the object based on what service it provides, it makes it much easier to fit it into the design.

The hidden implementation

  • The goal of the class creator is to build a class that exposes only what’s necessary to the client programmer and keeps everything else hidden.
  • The first reason for access control is to keep client programmers’ hands off portions they shouldn’t touch.
  • Parts that are necessary for the internal operation of the data type but not part of the interface that users need in order to solve their particular problems.
  • The second reason for access control is to allow the library designer to change the internal workings of the class without worrying about how it will affect the client programmer.

Reusing the implementation

  • Code reuse is one of the greatest advantages that object-oriented programming languages provide.
  • You can also place an object of that class inside a new class. We call this “creating a member object.” this concept is called composition.
  • You can also change the member objects at run time, to dynamically change the behavior of your program.
  • You should first look to composition when creating new classes, since it is simpler and more flexible.

Inheritance

  • Two types can have characteristics and behaviors in common, but one type may contain more characteristics than another and may also handle more messages.
  • This new type contains not only all the members of the existing type (although the private ones are hidden away and
    inaccessible), but more importantly it duplicates the interface of the base class.
  • Since we know the type of a class by the messages we can send to it, this means that the derived class is the same type as the base class.
  • This type equivalence via inheritance is one of the fundamental gateways in understanding the meaning of object-oriented programming.
  • You have two ways to differentiate your new derived class from the original base class: Simply add brand new methods to the derived class Or to change the behavior of an existing base-class method.

Is-a vs. is-like-a relationships

  • A test for inheritance is to determine whether you can state the is-a relationship about the classes and have it make sense.
  • The new type can still be substituted for the base type, but the substitution isn’t perfect because your new methods are not accessible from the base type.
  • The interface of the new object has been extended, and the existing system doesn’t know about anything except the original interface.

Interchangeable objects with polymorphism

  • When dealing with type hierarchies, you often want to treat an object not as the specific type that it is, but instead as its base type. This allows you to write code that doesn’t depend on specific types.
  • This ability to easily extend a design by deriving new subtypes is one of the essential ways to encapsulate change.
  • If a method is going to tell a generic shape to draw itself, the compiler cannot know at compile time precisely what piece of code will be executed.
  • When the message is sent, the programmer doesn’t want to know what piece of code will be executed.
  • The function call generated by a non-OOP compiler causes what is called early binding, It means the compiler generates a call to a specific function name, and the runtime system resolves this call to the absolute address of the code to be executed.
  • Object-oriented languages use the concept of late binding. When you send a message to an object, the code being called isn’t determined until run time.
  • The compiler does ensure that the method exists and performs type checking on the arguments and return value, but it doesn’t know the exact code to execute.
  • In Java, dynamic binding is the default behavior and you don’t need to remember to add any extra keywords in order to get polymorphism.
  • We call this process of treating a derived type as though it were its base type upcasting.

The singly rooted hierarchy

  • Whether all classes should ultimately be inherited from a single base class?
  • It turns out that the benefits of the singly rooted hierarchy are many.
  • A singly rooted hierarchy makes it much easier to implement a garbage collector

Containers

  • A container will expand itself whenever necessary to accommodate everything you place inside it.
  • You don’t need to know how many objects you’re going to hold in a container.
  • A good OOP language comes with a set of containers as part of the package.
  • There are two reasons that you need a choice of containers: First, containers provide different types of interfaces and external behavior.Second, different containers have different efficiencies for certain operations.
  • These and other operations have different efficiencies depending on the underlying structure of the sequence.

Parameterized types (generics)

  • You cast down the hierarchy to a more specific type.
  • It’s hardly safe to downcast unless you know exactly what you’re dealing with.
  • Wouldn’t it make sense to somehow create the container so that it knows the types that it holds, eliminating the need for the downcast and a possible mistake?
  • A parameterized type is a class that the compiler can automatically customize to work with particular types.

Object creation & lifetime

  • Each object requires resources, most notably memory, in order to exist. When an object is no longer needed it must be cleaned up so that these resources are released for reuse.
  • How can you possibly know when to destroy the objects? When you’re done with the object, some other part of the system might not be.
  • The storage and lifetime can be determined while the program is being written in C++.
  • The second approach is to create objects dynamically in a pool of memory called the heap. In this approach, you don’t know until run time how many objects you need, what their lifetime is, or what their exact type is. Those are determined at the spur of the moment while the program is running.
  • The amount of time required to allocate storage on the heap can be noticeably longer than the time to create
    storage on the stack.
  • Java uses dynamic memory allocation, exclusively.
  • Java provides a feature called a garbage collector that automatically discovers when an object is no longer in use and destroys it.

Exception handling: dealing with errors

  • A major problem with most error-handling schemes is that they rely on programmer vigilance in following an agreed-upon convention that is not enforced by the language.
  • An exception is an object that is “thrown” from the site of the error and can be “caught” by an appropriate exception handler designed to handle that particular type of error.
  • An exception cannot be ignored, so it’s guaranteed to be dealt with at some point.
  • If you don’t write your code to properly handle exceptions, you’ll get a compile-time error message In Java.

Concurrent programming

  • Initially, programmers with low-level knowledge of the machine wrote interrupt service routines, and the suspension of the main process was initiated through a hardware interrupt.
  • There’s a large class of problems in which you’re simply trying to partition the problem into separately running pieces (tasks) so that the whole program can be more responsive.
  • One of the convenient features of concurrency at the language level is that the programmer doesn’t need to worry about whether there are many processors or just one.
  • If you have more than one task running that’s expecting to access the same resource, you have a problem.
  • So a task locks a resource, completes its task, and then releases the lock so that someone else can use the resource.

Java and the Internet

Client/server computing

  • The problems arise because you have a single server trying to serve many clients at once.

The Web as a giant server

  • All you care about is connecting to and interacting with one server at a time.
  • The Web browser was a big step forward: the concept that one piece of information can be displayed on any type of computer without change.
  • The browser was just a viewer it couldn’t perform even the simplest computing tasks.

Client-side programming

  • This submission passes through the Common Gateway Interface (CGI) provided on all Web servers. The text within the submission tells CGI what to do with it.
  • Client-side programming means that the Web browser is harnessed to do whatever work it can, and the result for the user is a much speedier and more interactive experience at your Web site.

1.Plug-ins

  • This is a way for a programmer to add new functionality to the browser by downloading a piece of code that plugs itself into the appropriate spot in the browser.
  • The value of the plug-in for client-side programming is
    that it allows an expert programmer to develop extensions and add those extensions to a browser without the permission of the browser manufacturer.

2.Scripting languages

  • Scripting languages tend to be reasonably easy to understand and, because they are simply text that is part of an HTML page, they load very quickly as part of the single server hit required to procure that page.
  • You should probably consider a scripting language before looking at a more involved solution such as Java programming.

3.Java

  • Java allows client-side programming via the applet and with Java Web Start.
  • The applet is downloaded automatically as part of a Web page. When the applet is activated, it executes a program.
  • Since Java is a full-fledged programming language, you can do as much work as possible on the client before and after making requests of the server.

4.Alternatives

  • The biggest problem was probably that the 10 MB download necessary to install the JRE was too scary for the average user.
  • Anytime you have control over user machines, for example within a corporation, it is reasonable to distribute and update client applications using these technologies.
  • Flex allows you to program without worrying about browser specifics

5.NET and C#

  • The .NET platform is roughly the same as the JVM and Java libraries, and C# bears unmistakable similarities to Java.
  • Currently, the main vulnerability and important question concerning .NET is whether Microsoft will allow it to be completely ported to other platforms.

6.Internet vs. intranet

  • When Web technology is used for an information network that is restricted to a particular company, it is referred to as an intranet.
  • If your program is running on the Internet, you don’t know what platform it will be working under, and you want to be extra careful that you don’t disseminate buggy code.
  • If you are involved in such an intranet, the most sensible approach to take is the shortest path that allows you to use your existing code base, rather than trying to recode your programs in a new language.

Server-side programming

  • A common scenario involves a request for a complex database search, which the server then formats into an HTML page and sends to you as the result.
  • These database requests must be processed via some code on the server side, which is generally referred to as server-side programming.
  • Java-based Web servers that allow you to perform all your server-side programming in Java by writing what are called servlets.
  • They eliminate the problems of dealing with differently abled browsers.

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 練習題一:過橋問題 假設某人有100,000現金。 每經過一次路口需要進行一次交費。 交費規則為當他現金大於50,000時每次需要交5%如果現金小於等於50,000時每次交5,000。 請寫一程式計算此人可以經過多少次這個路口。 練習題二:乘法口訣 練習三:百錢買百雞問題 22課 《張丘建算經》成書 ...
  • python快速生成註釋文檔的方法 今天將告訴大家一個簡單平時只要註意的小細節,就可以輕鬆生成註釋文檔,也可以檢查我們寫的類方法引用名稱是否重覆有問題等。一看別人專業的大牛們寫的文檔多牛多羡慕,不用擔心我們可以讓python為我們生成基本滿足的說明文檔,一來可以提高代碼整體閱讀性,二來可以將代碼的整 ...
  • 我們在用eclipse EE neon創建dynamic web project時,如果你發現底部狀態欄一直卡在installing dynamic web module facet,永遠到不了100%,怎麼辦呢? 你可以嘗試一下斷開網路鏈接,然後再創建。 ...
  • 我們實現文件上傳 用位元組流的話代碼量大 效率低下 所以springMVC為我們提供了自己的方法。 SpringMVC專門提供了CommonMultipartResolver組件實現文件上傳: maxUploadSize 文件最大限制,單位是byte maxInMemorySize 低於這個大小的文件 ...
  • 初始springMvc這個框架,非常的陌生,而且幸好公司是通過maven這個代碼管理工具,可以隨時添加依賴。解決了很多問題在以後深入開發中。 項目結構: 通過結構中,pom.xml這個文件其實就說明這個項目是通過maven構建的,pom.xml里是主要負責構建jar或者war的依賴。其代碼如下: 從 ...
  • 在我們開發項目中的時候經常使用到svn,有時候我們commit的時候回出現很多無用的文件,這些文件就是未版本化的文件,怎麼解決這些亂文件的問題呢? svn commit提交的時候有個“show unversioned files” 顯示未版本化的文件 點擊前面的checkbox取消就可以了。 ...
  • 如果有很強勁的邏輯能力,或者是構思已久的話,你可以寫一個很複雜的表達式,但為了使代碼方便閱讀,最好一個表達式不要超過3個運算符。(這裡的語句,並不包含代碼塊),使用自增或者自減看起會更專業一些,但是要知道++在前與在後的區別,根據位置就可以清楚知道,++在前就先自加一,++在後就是用過之後再加一。 ...
  • 使用Eclipse把java文件打包成jar 含有第三方jar庫的jar包   網上打包說用eclipse安裝fat jar插件,但是貌似現在都不能用了,所以我只能按照eclipse自帶的方法打包了。但是。。。網上的各自辦法都有些問題,並且是不包含第三方jar包的打包方法,結合網 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...