Java Object-Oriented understanding

Source: Internet
Author: User

Three main features of Java object-oriented

Packaging

For example, we want to describe the attributes of people, we can name,age the related attributes of people ... Encapsulated as a human person

Class so that we can implement the encapsulation from the basic data (attributes) to the class

We can also encapsulate some common methods into a class, as a tool class, such as the method of data format verification,

Time format conversion method, so that we can implement the encapsulation from the common method to the class

In some cases, our data structure is more complex, and the declaration of another class is also declared in a property in one class.

Or declare a collection of a class as a property

Class teacher{public String name;               Teacher's name public Family family;//Family class list<student> students;//teacher name of the student collection }

In special cases, such data encapsulation is not enough, we also need to restrict access, such as the addition of private decoration

Can only be called inside the class, for example:

               class bank{                     private string money;//Deposit Number                    public string getmoney ()  {     &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;RETURN&NBSP;MONEY;//Bank personnel perform withdrawal operations                    }                   public  void setmoney (String money)  {           &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;THIS.MONEY&NBSP;=&NBSP;MONEY;//Bank personnel perform deposit operation                    }                 }

The above encapsulation is at the level of attributes and methods, there is a more interesting package form is the inner class, the rule is:

The member inner class can unconditionally access all member properties and member methods of the external class, including private and static members, which, in combination with inheritance, cleverly solves the problem of single-inheritance in Java:

    public class External extends Father {      private string exname;    //Inner class     class  internal {    private string inname;         public void print ()  {    system.out.println (Exname);     fatherprint ()//direct access to the Father class printing method     }    }         public void get ()  {     System.out.println (Internal.class);    }         Public string getexname ()  {    return exname;    }         public void setexname (String exname)  {     Exname = exname;    }    } 

Inherited


Polymorphic

This article is from the "Qinguan" blog, make sure to keep this source http://11083953.blog.51cto.com/11073953/1732817

Java Object-Oriented understanding

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.