Java study notes 16 (Object-Oriented 9: additional content), java Study Notes

Source: Internet
Author: User

Java study notes 16 (Object-Oriented 9: additional content), java Study Notes

There are always four permissions. Here is an introduction:

The maximum permission is public, followed by protected, default, and private.

The private modifier can only be used in this class.

Public is the maximum permission and can be used across packages. Classes, methods, and member variables can be modified for different classes and irrelevant classes of different packages.

If you do not have the write permission, the default permission is used.

Protected permission: If a class after a cross-package has an inheritance relationship (subclass), it cannot be modified by default, but it can be modified by protected.The protected permission of the parent class must be called in the subclass. Note that protected cannot modify the class.

 

Only public and private resources are used for routine development.

 

Note:

Abstract cannot be written together with static, private, and final.

 

About code blocks:

Example

Public class Person {private String name; private int age; public Person (String name, int age) {this. age = age; this. name = name; System. out. println ("constructor");} // construct the code block {System. out. println ("constructor code block");} // static code block static {System. out. println ("static code block ");}}
// Static code block. It is executed only once. // The code block is constructed and executed every time an object is created. The construction method public class Test {public static void main (String [] args) is given priority) {Person person1 = new Person ("Zhang San", 18); Person person2 = new Person ("Li Si", 18) ;}}/* output: static code block construction method */

 

 

There are also some additional content:

Document comments and generate jar packages

Write a class at will

package demo;/** * @author dell * @version 1.0 */public class Person {    /**     *      * @param a     * @param b     * @return function     */    public int function(int a, int b){        return a+b;    }}

Use eclipse, select a project, right-click export, and select Java, including Javadoc and other files,

After selection, a set of web pages will be generated, which is equivalent to a manual. You can view the relevant content

You can also select other java options to generate jar packages.

 

Jar package import method:

Generally, create a new lib folder under the project directory, put it into the jar package, right-click build path-> add to build path, and use import.

 

Object-oriented supplement:

1. A class can be passed as a method parameter, that is, a method parameter can be an object. In essence, a memory address is passed to perform object-related operations in the method.

2. Similar to the first one, a class can also be used as the return value of a method, that is, an object reference is returned.

3. abstract classes can also be used as parameters and return values of methods. The difference is:

The abstract class has no objects. You can only use the polymorphism feature to pass (return) The subclass object of the abstract class. Note that when receiving the return value, the type must be an abstract class rather than a subclass.

4. The interface can also be used as a method parameter and return value:

The interface does not have any objects. You must pass the implementation Class Object of the interface. Note that you can use polymorphism to create an implementation class object or directly create an implementation class object,

Similarly, although the implementation class object is passed in as the return value, it must be of the interface type when receiving

 

Write a simple case for the above supplementary content to understand:

 

public abstract class Animal {    public abstract void eat();    public static Animal getInstance(){        return new Cat();    }}
Public class Cat extends Animal {public void eat () {System. out. println ("Cat eating ");}}
Public class Test {public static void main (String [] args) {Animal a = Animal. getInstance (); a. eat (); // output: Cat eats fish }}

 

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.