java--Object-Oriented programming

Source: Internet
Author: User

Introduction to Object Orientation

Object-oriented programming is a programmatic approach that needs to be implemented using classes and objects.

    • Class that describes the common characteristics of multiple objects, which are templates for objects.
    • Object that describes an individual in the real world, which is an instance of a class.
definition of Class
Class class Name {//attribute data type variable name ...//method modifier return value type method name (parameter) {   } ...}
Creating Objects

Class Name Object name = new class name ();

advantages of object-oriented programming

The biggest benefit is: code reuse

1. Easy Maintenance

Object-oriented design of the structure, high readability, due to the existence of inheritance, even if the need to change, then maintenance is only in the local module, so maintenance is very convenient and low cost.

2. High quality

At design time, you can reuse existing classes that have been tested in the field of previous projects to make the system meet business requirements and have high quality.

3. High efficiency

In software development, according to the needs of the design of the real world of things to abstract, produce classes. Using this method to solve the problem, close to the daily life and the natural way of thinking, is bound to improve the efficiency and quality of software development.

4. Easy to expand

Due to the characteristics of inheritance, encapsulation and polymorphism, the system structure of high cohesion and low coupling is designed naturally, which makes the system more flexible, easier to expand and less cost.

Object-oriented three major featuresfirst, the package

Hides the properties of the object from the implementation details of the method and provides only public access to it.

The embodiment of the package:

    • Variables: Using private decoration, which is the encapsulation of variables
    • Method: Also a package that encapsulates multiple code
    • Class: is also a package that encapsulates multiple methods

Private member variables:

    • Private decorated members can only be accessed in the current class and cannot be accessed directly from other classes
class Person {private int age;//set Private member variable (property variable can only be set to private, local variable not) private String name;public void setage (int a) {//External provisioning member The method of the variable if (a < 0 | | A > 130) {//due to the value of the set member variable, here you can add the data validation System.out.println (A + "data range not conforming to age"); age = A; }public void Getage () {  //external provide access to member variables return age;}}

This keyword:

function: Used to distinguish between a member variable with the same name and a local variable (this. Member variable)

Example: Judging if you are a peer

class Person {private int age;private String name;public int getage () {return age;} public void Setage (int.) {this.age = age;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} public void Speak () {System.out.println ("name=" + THIS.name + ", age=" + this.age);} Determine if the age of the peer public, Boolean equalsage (person p) {//is used to compare with the age of the Equalsage method object that is currently called and passed in P Since it is not possible to determine which object is calling the Equalsage method, this can be used instead of/* * if (this.age = = P.age) {return true;} return false; */return This.age = = P.age;}}

java--Object-Oriented programming

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.