Java Learning Note 16 (object-oriented nine: supplemental content)

Source: Internet
Author: User

Always see four kinds of permissions, here to do an introduction:

The maximum permissions are public, followed by protected,default,private

Private adornments are only available in this class

Public is the maximum privilege that can be used across packages, subclasses of different packages and unrelated classes can be used, can be decorated with classes, methods, member variables

Non-write permissions are default permissions: Limited to use in this package

Protected permissions: After the cross-package of the class if there is an inheritance relationship (subclass), cannot use the default adornment, but can be decorated with protected, the call must be inside the subclass to call the parent class's protected permission, note protected can not decorate the class

For daily Development only: public, private

Also note:

Abstract can not be written together with Static,private,final

About code blocks:

Example

 Public classPerson {PrivateString name; Private intAge ;  PublicPerson (String name,intAge ) {         This. Age =Age ;  This. Name =name; System.out.println ("Construction Method"); }    //Building Code Blocks{System.out.println ("Construct Method code block"); }    //Static code block    Static{System.out.println ("Static code block"); }}
// static code block, executed only once // constructs a block of code that executes every time the object is created, prior to the construction method  Public class Test {    publicstaticvoid  main (string[] args) {        new person ("Zhang San");         New Person ("John Doe");    }} /* output: Static code block construction Method code block construction method construction method code block construction method */

There are some additional elements:

Document comments and build jar packages

Feel free to write a class

 Packagedemo;/** * @authorDell *@version1.0*/ Public classPerson {/**     *      * @paramA *@paramb *@returnfunction*/     Public intfunctionintAintb) {        returnA +b; }}

Use Eclipse, choose Project, right-click Export inside have select Java, inside have Javadoc and so on file,

After selection, a set of pages will be generated, equivalent to the instructions to view relevant content

You can also choose other Java options, generate jar packages, etc.

How to import the jar package:

Usually in the project directory to create a new Lib folder, put into the jar package, right-click Build path, add to build path, when using the import can be

Object-oriented additions:

1. A class can be passed as a parameter of a method, that is, the parameter of a method can be an object, essentially passing a memory address, and manipulating the object in a method

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

3. Abstract classes can also be used as parameters and return values for methods, except that:

Abstract classes have no objects and can only use polymorphic attributes to pass (return) The subclass object of the abstract class, noting that the type must be an abstract class instead of a subclass when receiving the return value

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

Interfaces also have no objects and must pass the implementation class object of the interface, note that the implementation class object can also be created directly by using polymorphic creation.

Similarly, as a return value, although the implementation class object is passed in, it must be the interface type when it is received.

Write a simple case for the above supplement to understand:

 public  abstract  class   Animal { public  abstract  void   Eat (    );  public  static   Animal getinstance () { return  new   Cat (); }}
 Public class extends Animal {    publicvoid  eat () {        System.out.println ("cat is Eating");}    }
 Public class Test {    publicstaticvoid  main (string[] args) {        = animal.getinstance ();        A.eat (); // output: Cat eats fish     }}

Java Learning Note 16 (object-oriented nine: supplemental content)

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.