Thinking in Java——笔记(1)
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.
Thinking in Java——笔记(1)的更多相关文章
- Effective Java笔记一 创建和销毁对象
Effective Java笔记一 创建和销毁对象 第1条 考虑用静态工厂方法代替构造器 第2条 遇到多个构造器参数时要考虑用构建器 第3条 用私有构造器或者枚举类型强化Singleton属性 第4条 ...
- java笔记00-目录
--2013年7月26日17:49:59 学习java已久,趁最近有空,写一个总结: java笔记01-反射:
- java笔记整理
Java 笔记整理 包含内容 Unix Java 基础, 数据库(Oracle jdbc Hibernate pl/sql), web, JSP, Struts, Ajax Spring, E ...
- 转 Java笔记:Java内存模型
Java笔记:Java内存模型 2014.04.09 | Comments 1. 基本概念 <深入理解Java内存模型>详细讲解了java的内存模型,这里对其中的一些基本概念做个简单的笔记 ...
- servlet(6) - servlet总结 - 小易Java笔记
垂阅前必看: 这都是我总结的我觉得是学习servlet应该掌握的,我在学习期间也做了一个博客项目来让所学的知识得以巩固.下面就是博客项目链接.前面的servlet相关的笔记总汇,还有就是我把觉得在学习 ...
- Java笔记 —— 继承
Java笔记 -- 继承 h2{ color: #4ABCDE; } a{ text-decoration: none!important; } a:hover{ color: red !import ...
- Java笔记 —— 方法重载和方法重写
Java笔记 -- 方法重载和方法重写 h2{ color: #4ABCDE; } a{ text-decoration: none !important; } a:hover{ color: red ...
- Java笔记 —— 初始化
Java笔记 -- 初始化 h2{ color: #4ABCDE; } a{ text-decoration: none !important; } a:hover{ color: red !impo ...
- Java笔记 —— this 关键字
Java笔记 -- this 关键字 h2{ color: #4ABCDE; } a{ color: blue; text-decoration: none; } a:hover{ color: re ...
- Java 笔记 —— java 和 javac
Java 笔记 -- java 和 javac h2{ color: #4ABCDE; } a{ text-decoration: none !important; } a:hover{ color: ...
随机推荐
- Spring Data JPA 查询方法支持的关键字
Table 2.3. Supported keywords inside method names Keyword Sample JPQL snippet And findByLastnameAndF ...
- Java API 实现HBase的数据添加与过滤查询
包依赖比较麻烦,找了好久,我用的CDH5.0 现将所依赖的包的列表清单如下: public class EmployeeDao { /** * @param args */ public static ...
- POJ 2096 (概率DP)
题目链接: http://poj.org/problem?id=2096 题目大意:n种bug,s个子系统.每天随机找一个bug,种类随机,来自系统随机.问找齐n种bug,且每个子系统至少有一个bug ...
- struts.properties配置详解
Struts 2框架有两个核心配置文件,其中struts.xml文件主要负责管理应用中的Action映射,以及该Action包含的Result定义等.除此之外,Struts 2框架还包含 st ...
- 洛谷 P1015 回文数 Label:续命模拟QAQ
题目描述 若一个数(首位不为零)从左向右读与从右向左读都一样,我们就将其称之为回文数. 例如:给定一个10进制数56,将56加65(即把56从右向左读),得到121是一个回文数. 又如:对于10进制数 ...
- 洛谷 P1074 靶形数独 Label:search 不会
题目描述 小城和小华都是热爱数学的好学生,最近,他们不约而同地迷上了数独游戏,好胜的他 们想用数独来一比高低.但普通的数独对他们来说都过于简单了,于是他们向 Z 博士请教, Z 博士拿出了他最近发明的 ...
- HDU-统计难题
DescriptionIgnatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). Inp ...
- 【CodeVS】p1299 切水果
题目描述 Description 简单的说,一共N个水果排成一排,切M次,每次切[L,R]区间的所有水果(可能有的水果被重复切),每切完一次输出剩下水果数量 数据已重新装配,不会出现OLE错误 时限和 ...
- iOS 发布遇到的问题 (转载)
1.ios图片命名Icon-120.png – 120×120 iphone & ipod touch(ios7) http://blog.csdn.net/xyxjn/article/de ...
- HttpClient_httpclient 4.3.1 post get的工具类
package com.ryx.util; import java.util.ArrayList; import java.util.List; import java.util.Map; impor ...