Java object-oriented basic features

Source: Internet
Author: User

1, encapsulation of

The connection between an object and the outside world should be exposed through a unified interface that should be exposed and hidden.

Encapsulation of attributes: The default value for access to the properties of a class in Java is defaults, and to hide the property or method, you can add private (private) modifiers to restrict access only to the inside of the class. For a private property in a class, it is given an opposite method (GetXxx (), setxxx ()) to access the private property, guaranteeing the security of the operation of the private property.

encapsulation of the method: for the encapsulation of the method, the exposed public, the hidden hidden. method exposes the declaration (definition) of a method (which can be called only if the parameter and the return value are known), and the implementation of the hidden method minimizes the effect of the implementation change on the schema.

public class Testdemo {public static void main (string[] args) {person person=new person ();p Erson.tell ();p erson.setage (- ;p erson.setname ("Zhang San");p Erson.tell ();}} Class Person{private int age;private String name;public int getage () {return age;} public void Setage (int.) {if (age>0&&age<150) {this.age = age;}} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} void Tell () {System.out.println ("Name:" +name+ "; Age:" +age);}}

Note: (1) Java anonymous object: Only one place to use. For example: New person (). tell ();

(2) Java constructor method: The constructor method name must be the same as the class name, no return means, can be overloaded, mainly for the properties in the class initialization.

(3) The value of the data type passed: Eight basic data Types and string (string is also the address passed, but the string object and other objects are different, the string is final, so the value of the string object cannot be changed, the value changes will produce a new object. Then StringBuffer can, but only change its contents, cannot change the memory address that the external variable points to.

The data type passed by reference: all composite data types except string, including arrays, classes, and interfaces.

Public class testref4 {public static void main (String args[])   {     int val=10;;     stringbuffer str1, str2;        str1  = new stringbuffer ("apples");     str2 = new stringbuffer (" Pears ");         system.out.println (" Val: " + val);     system.out.println ("str1 is "  + str1);     System.out.println ("str2 is "  + str2);      system.out.println (" ... ")         modify (val, str1, ) (the. .............. ...); STR2);       system.out.println ("val is "  + val);     system.out.println ("str1 is "  + str1);     systEm.out.println ("str2 is " &NBSP;+&NBSP;STR2);  }   public static  Void modify (INT&NBSP;A,&NBSP;STRINGBUFFER&NBSP;R1,STRINGBUFFER&NBSP;R2)   {             a = 0;      r1  = null;               R2.append (" taste good");       system.out.println ("a is ")  + a);       system.out.println ("r1 is "  + r1);       system.out.println ("r2 is " &NBSP;+&NBSP;R2);       system.out.println ("..... ......................)

The output is:

VAL:10STR1 is APPLESSTR2 are pears...............................a is 0r1 are NULLR2 is pears taste good ..... ....... .... Val-10str1 is applesstr2 are pears taste good.


2, the Inheritance of










This article from "It Technology Learning and communication" blog, declined reprint!

Java object-oriented basic features

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.