Java how to program (Third Edition) -- My java notes (3)

Source: Internet
Author: User
Tags class definition naming convention
Chapter 8 object-based programming
1. The class defined with the keyword public must be stored in a file with the same name as the class and the file extension is. java. Defining more than one public class in the same file is a syntax error.
2. When defining a class in java, it always inherits the existing class definition, and the most basic inheritance Object class. It is the most basic super class or base class, and it has a main method toSting. If a class is not explicitly defined using the keyword extends, this class will implicitly inherit the Object class.
3. A class usually contains several constructors, which are implemented through method overloading. Constructors can also contain parameters, but cannot return values. An important difference between constructor and other methods is that constructor does not allow return values, and void does not.
4. Every class in java is a part of the package. If a programmer does not specify a package for the class, the class is automatically put into the default package, which contains the compiled classes in the current directory.
5. A link between a string and any object will generate an implicit call to the toString method of the object. For example, "Implicit toString () call:" + t;
6. Each class and interface in a Java API belongs to a specific package. It contains a group of associated classes and interfaces. A package is actually a directory structure that organizes classes and interfaces. Package provides a software reuse mechanism.
7. To create a reusable class, follow these steps:
1) define a public class. If the class is not public, it can only be used by other classes in the same package.
2) select a package name and add the package statement to the source code file of reusable classes.
3) compile this class. In this way, it is put into the appropriate package directory structure for the compiler and interpreter to use.
4) import this reusable class into the program that needs to be used. Now you can use it.
8. To give each package a unique name, Sun Microsystem proposed a naming convention for the package. Each package name starts from the reverse order of your Internet domain name. For example, com. deitel. jhtp3.ch08. Time1.
9. If no constructor is defined in the class, the compiler automatically creates a default constructor without parameters, also called a non-parameter constructor. The default constructor of the class calls the default constructor of the direct superclass and initializes the member variables by default.
10. When defining methods or variables of a class, if they are not provided with access permission modifiers, these methods or variables are considered as package access.
11. this reference is implicitly used to reference instance variables and methods of an object. Another reference of this is to implement "connection method call", also known as "cascade method call ". For example, t. setHour (18). setMinute (30). setSecond (22 );
The "." operator is a combination from left to right. Therefore, the expression above calculates t. setHour (18), and then this method returns a reference to object t. Another example is t. setTime (20, 20, 20). toString ();
Related Article

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.