<Java>< Classes and Objects ><OOP>

Source: Internet
Author: User

Overview class
    • Package (encapsulation): Also known as data hiding. Formally, it is the combination of data and behavior, and the user of the object hides the way the data is implemented. encapsulation gives the object black box features for improved reusability and reliability .
    • inheritance : extends the existing class to make it easy to define a new class. Subclasses have all the properties and methods of the parent class.
Object
    • Three properties of the object:
      1. Object behavior: Actions that can be applied to an object
      2. State of the object: each object holds information describing the current feature, which may change due to a method call.
      3. Object identity: A unique way to identify an object (identity).
Eg
    • An example of a destructive encapsulation
    • Class employee{  private Date hireday;  Public Date Gethireday () {   return hireday;  }             ....  }

      In the above example, the reference to date is returned, then date d = Gethireday () is called in the outer layer, and the change to D is actually a change to the hireday of the private variable. Therefore, to return a reference to a Mutable object, you should first clone it.

Static domain and static method static constants
    • More than static variables, such as the Pi defined in math, System.out constants
    • public static final Double PI = 3.14 ...;
Static methods
    • is a method that cannot be manipulated on an object.
Factory method
    • a common use of static methods .
    • Eg:numberformat class uses factory methods to produce objects of different styles
    • NumberFormat Currencyformat = Numberformat.getcurrencyinstance (); NumberFormat Percentformat = numberformat.getpercentinstance ();d ouble x = 0.1; System.out.print (Percentformat.format (x) + "  " + Currencyformat.format (x));
    • Here you may want to ask why not use the constructor to do these things? Note There are two reasons:
      1. The constructor cannot be named.
      2. The constructed object type cannot be changed when the constructor is used.
Class Design Tips
    • Be sure to keep your data private-------Never destroy encapsulation .
    • Be sure to initialize the data, rather than relying on the system default values.
    • Do not use too many basic types in a class, that is, use other classes instead of multiple related basic types.
    • Decompose classes with too much responsibility.
    • The class name and method name should be able to reflect their responsibilities.

<Java>< Classes and Objects ><OOP>

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.