zoukankan      html  css  js  c++  java
  • 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.
  • 相关阅读:
    Hdu4547CD操作离线lca
    1036: [ZJOI2008]树的统计Count树链剖分
    light1348Aladdin and the Return Journey树链剖分
    Problem 2082 过路费树链剖分
    2243: [SDOI2011]染色树链剖分
    Poj3237Tree 树链剖分
    Poj2763Housewife Wind树链剖分
    Hdu5087Revenge of LIS II简单dp
    Hdu5088Revenge of Nim II高斯消元
    Bootstrap入门学习笔记(只记录了效果)
  • 原文地址:https://www.cnblogs.com/apolloqq/p/6092622.html
Copyright © 2011-2022 走看看