Java Object-oriented

Source: Internet
Author: User
Tags pears

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.

123456789101112131415161718192021222324252627282930 publicclassTestDemo {publicstaticvoid main(String[] args) {Person person=newPerson();person.tell();person.setAge(-20);person.setName("张三");person.tell();}}classPerson{private intage;privateString name;publicintgetAge() {returnage;}publicvoidsetAge(intage) {if(age>0&&age<150){this.age = age;}}publicString getName() {returnname;}publicvoid setName(String name) {this.name = name;}voidtell(){System.out.println("姓名:"+name+";年龄:"+age);}}

Note:

(1) Java Anonymous Object : A place to use only once. 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, primarily for the properties in the class initialization.

(3) the data type that the value is passed : eight basic data Types and string (string is also the address that is passed, except that the string object and other objects are different and the string is final, so the value of the string object cannot be changed, A change in value will result in 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.

123456789101112131415161718192021222324252627282930 publicclassTestRef4 {publicstaticvoid main(String args[])  {    intval=10;;    StringBuffer str1, str2;        str1 = newStringBuffer("apples");    str2 = newStringBuffer("pears");        System.out.println("val:"+ val);    System.out.println("str1 is "+ str1);    System.out.println("str2 is "+ str2);     System.out.println("...............................");        modify(val, str1, str2);      System.out.println("val is " + val);    System.out.println("str1 is "+ str1);    System.out.println("str2 is "+ str2);  }   publicstatic voidmodify(inta, StringBuffer r1,StringBuffer 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 "+ r2);      System.out.println("...............................");  }}

The output is:

1234567891011 val: 10 str1  is apples str2 is pears ............................... A&NBSP;IS&NBSP; 0 R1&NBSP;IS&NBSP; null r2 is pears taste good VAL&NBSP;IS&NBSP; 10 str1 is apples str2 is pears taste good

(4) This keyword : Represents a property or method in a class, a constructor method in a calling class, for example: this (), or the current object.

(5) Static keyword : Static declaration property is a global property; The static declaration method is called directly through the class name; I read the Java Video tutorial that says: When you use the static method, You can access only the properties and methods of the static declaration, not the properties and methods of the static declaration.

2, the Inheritance of

In a program, you can use the extends keyword to let one class inherit another class, the inherited class is a subclass, the inherited class is the parent class, and the subclass automatically inherits all the methods and properties of the parent class

Java Object-oriented

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.