Java: Getting started from scratch, getting started with learning notes & lt; Information Hiding & gt; (4)

Source: Internet
Author: User

(4) Hidden Information import package: it is mainly used to introduce resources in other packages for the classes of the current import package. There are multiple ways to use the import package: first: yes, click the X prompt on the left side of ECLIPSE, and then the system will prompt which packages can be imported to the class and the corresponding selection. Type 2: Click the cursor to any place where the class name needs to be imported, then Ctrl + 1, and then select some frequently used packages for the package to be imported: classes in common packages can be found in our API. We can query the API to learn about the classes in the corresponding package. Import Source Code: import some packages that come with the system. If you need to view the source code, you need to import the corresponding source code package. In JDK, the source code file is used to hide and hide the SRC. ZIP file information, which is a protection mechanism for objects. The object hides some of its own details and does not allow other external classes to directly access the object. Generally, the object attribute is modified with a Private modifier. This attribute is a Private attribute and can only be accessed in the current class. Other classes cannot be accessed directly. Similarly, if you declare a method as private, the second method can only be accessed in the current class, but no private method can be seen in other classes. Information encapsulation: encapsulate private attributes and public methods into a whole, and you only need to call the object when operating this whole. Package com. ibm. first; publicclass Student2 {private String name; private floatscore; public void setName (String_name) {name = _ name;} public String getName () {return name ;} public void setScore (float _ score) {score = _ score;} public float getScore () {return score;} Java object-oriented features: l Abstraction: abstraction is to ignore those aspects irrelevant to the current target in a topic, so that you can pay more attention to the aspects related to the current target. Abstraction is not intended to understand all the problems, but to select a part of the problem. Abstract: Process abstraction and data abstraction. L inheritance: inheritance is a hierarchical model that connects classes and allows reuse of encouraging classes. It provides a clear way to express commonalities. A new class of an object can be derived from an existing class. This process is called class inheritance. The new class inherits the features of the original class. The new class is called the derived class (subclass) of the original class, and the original class is called the base class (parent class) of the new class ). A derived class can inherit methods and instance variables from its base class, and the class can modify or add new methods to make it more suitable for special needs. L encapsulation: encapsulation is used to enclose processes and data. Data access can only be performed through the defined interface. Object-oriented computing begins with the basic concept that the real world can be depicted as a series of completely autonomous and encapsulated objects that access other objects through a protected interface. L polymorphism: allows different types of objects to respond to the same message. Polymorphism includes parameterized polymorphism and inclusion polymorphism. The polymorphism language has the advantages of flexibility, abstraction, behavior sharing, and code sharing, which effectively solves the same name problem of application functions. Specific Operation of the encapsulation process: Declares the attributes in an object class as private and provides one or more public methods to operate these private attributes. Why not use the constructor? 1. the constructor can also assign values to private attributes. However, the constructor cannot obtain the value of private attributes, but it still needs to be obtained through methods. 2. when the constructor assigns values to private properties, it must provide a constructor modified with Public. In the constructor, it will assign values to private properties, when creating an object, we will call this constructor to pass the constructor parameters, which will expose the private attributes of several attributes in this class, relatively speaking, it does not achieve the purpose of hiding information. If the constructor is declared as private, the constructor is invalid. Benefits of encapsulation 2. Hiding the implementation details of a specific class makes it safer. 2. prevent unauthorized access to encapsulated data. You can easily add control logic and restrict unreasonable operations on attributes. 2. It is conducive to ensuring data integrity 2. Enhanced code maintainability. code formatting shortcut: ctrl + shift + f note: java provides three types of comments: single-line comments, multi-line comments, docized comments // single-line/* multi-line comments * // *** docized comments */docized comments for classes and attributes before, you can use the javadoc command to generate the corresponding help document. v often adds a mark starting with "@" in the javadoc comment, combining with the parameters of the javadoc command, you can generate a specific mark in the generated API document. The commonly used javadoc mark is § @ author: author § @ version: version § @ docroot: indicates the root path of the generated document. § @ deprecated: the method is not recommended. § @ param: parameter type of the method. § @ return: return type of the method. § @ see: "see", used to specify the reference content. § @ exception: thrown exception @ throws: The thrown exception, which is synonymous with exception

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.