One: The object-oriented basic concept of Java

Source: Internet
Author: User
Tags ming modifier volatile

1. Object-oriented

Object oriented is an emerging program design method, or a new program design specification (paradigm). The basic idea is to use the basic concepts of object, class, inheritance, encapsulation, polymorphism and so on to design the program. The software system is constructed from the objective object in the real world. and make use of human's natural way of thinking as much as possible in the system construction.

What is OOP?

OOP, which is the programming of a Polygon object, is a programming method relative to structured programming. No understanding of structured programming is also a hindrance. Simply look at the world around you and you'll find some of the characteristics of OOP.

For example: A car, with wheels, engines and other basic equipment, can travel basic functions. As a user. We don't need to know the details of the car's construction, just know how to operate the steering wheel and let the car drive.

That is, to achieve the goal, we just need to get an object, know how to use it, we can not pay attention to detailed implementation details.

For example, to assemble a car. Very many parts are required, and these parts have a certain specification. Can complete a detailed function.

For example, the engine generates power, brake pads are used to brake and so on. Each part is combined to complete a larger goal, and these parts are the detailed units to be studied.

In the example above, the car is the unit we need to achieve the purpose of the trip, the engine, the brake pads, etc. are the units we need to assemble a car. In the process of use, we focus only on the unit's specifications, the ability of the unit, and not the details of the detailed operation of the unit. These units. In the programming world, it can be abstracted as a class, that is, a class with certain attributes and certain behaviors.

2. Objects

object is an entity which is used to describe objective things in system, which is a basic unit of composing system.

An object consists of a set of properties and a set of services that operate on that set of properties.

The instantiation of a class generates an object. The life cycle of an object consists of three phases: build, use, and eliminate.

When there is no reference to an object, the object becomes a useless object. The Java garbage collector actively scans the dynamic memory area of an object itself. Collect non-referenced objects as garbage and release them.

When system memory is exhausted or a call to System.GC () requires garbage collection, the garbage collection thread executes synchronously with the system.

3. Class

A class is a set of objects that have the same properties and methods, which provides a unified, abstract description of all objects that belong to the class, and contains two main parts of properties and methods. In an object-oriented programming language, a class is a separate program unit that should have a class name and contain two main parts of the property and method.

The implementation of a class in Java consists of two parts: class declaration and class body.

class declaration

  <span style= "FONT-SIZE:18PX;"  >  [Public][abstract|final]class className [extends Superclassname] [implements interfacenamelist]{...} Of The modifier public,abstract,final describes the properties of the class, classname the class name, superclassname the name of the class's parent class, and interfacenamelist the list of interfaces that the class implements.

Class Body classclassname{ [public | protected | private] [static][final] [transient] [volatile] type variablename;//member variable [public | protected | private] [static][final | abstract] [native] [synchronized] returntype methodName ([paramlist]) [th Rows exceptionlist]{ Statements }//member method}</span>


The meaning of a member variable qualifier:

Static variables (class variables)

Final: Constant. Transient: Temporary variable for object archiving, for serialization of objects

Volatile: Contribution variable, for concurrent thread sharing

The implementation of the method also contains two parts: the method declaration and the method body.

Method declaration

The meaning of a qualifier in a method declaration:

Static: Class method, which can be called directly from the class name

Abstract: Abstraction method, no method body

Final: Method cannot be overridden

Native: Integrating code in other languages

Synchronized: Controlling access to multiple concurrent threads

A method declaration contains a method name, a return type, and an external parameter.

The type of the parameter can be a simple data type, or it can be a composite data type (also known as a reference data type).

For simple data types, Java implements value passing. The method receives the value of the parameter, but cannot change the value of the parameters. Assuming that you want to change the value of the reference data type, the operation of the data in the method can change the value of the data, because the reference data type is passed to the method by the address of the data in memory.

Method body

The method body is the implementation of the method, which contains declarations of local variables and all legitimate Java directives. The scope of the local variable declared in the method body is inside the method. If the local variable has the same name as the member variable of the class, the member variable of the class is hidden.

We must use this for the difference between the parameters and the member variables of the class.

This is used to refer to the current object in a method. Its value is the object that called the method.

The return value must be the same as the return type, or completely the same, or its subclasses. When the return type is an interface, the return value must implement the interface.

Construction method

The construction method is a special method.

Each class in Java has a construction method that initializes an object of the class.

The constructor method has the same name as the class name and does not return any data type.

Overloading is often used in construction methods.

The constructor method can only be called by the new operator

4. Object-oriented basic features

Packaging

Encapsulation is to hide the inner details of the object as much as possible, to form a boundary, and only to keep the limited interfaces and methods to interact with the outside world.

The principle of encapsulation is that parts outside the object cannot be arbitrarily visited and manipulate the object's internal properties. This avoids the external damage to the internal properties of the object.

Enables information hiding of members in a class by setting certain access rights to members of the class

Private: A member of a class that is limited to private, can only be interviewed by the class itself.

Assuming that the constructor of a class is declared private, other classes cannot generate an instance of the class.

Default: The class does not add any access permission-qualified members are in the default (default) state, and can be interviewed by the class itself and the classes in the same package.

Protected: A member of a class that is limited to protected. Can be interviewed by the class itself, its subclasses (including subclasses in the same package and in different packages), and all other classes in the same package.

Public: Members of the class that are qualified as public can be interviewed by all classes.

Inherited

The object of the subclass has all the properties and methods of the parent class. Called the subclass inherits from the parent class.

The parent class in Java can have multiple subclasses, but subclasses can inherit only one parent class. Called single inheritance. Inheritance implements the reuse of code.

All classes in Java are obtained by inheriting the Java.lang.Object class directly or indirectly.

Subclasses cannot inherit member variables and methods that have access permissions to private in the parent class.

Subclasses can override methods of the parent class. A member variable named with the same name as the parent class.

Access to the parent class members is achieved through Super in Java. Super is used to refer to the parent class of the current object. There are three ways to use super:

Access to the parent class is hidden member variables, such as: super.variable;

Call the overridden method in the parent class, such as: Super. Method ([Paramlist]), super () calls the parent class construction methods;

Call the constructor of the parent class, for example: Super ([paramlist]);

Polymorphic

The polymorphism of an object is the ability to have different data types or behave differently after the quilt class inherits the properties or methods defined in the parent class. This makes the same property or method have different semantics in the parent class and its subclasses. For example: "Geometry" of the "paint" method, "Ellipse" and "polygon" are the "geometry" of the sub-class, its "paint" method function is different.

Java's polymorphic body now has two aspects: static polymorphism implemented by method overloading (compile-time polymorphism), and dynamic polymorphism implemented by method overrides (execution-time polymorphism).

Compile-time polymorphism: During the compilation phase, the compiler will statically determine which method to invoke, depending on the number of parameters, and which overloaded method is called in detail.

Execution-Time polymorphism: Subclass objects can be used as parent objects because subclasses inherit all of the properties of the parent class, except for the private class. Any place in the program where the parent object is used can be substituted with a subclass object. An object can invoke a method of a subclass by referencing an instance of the subclass.

Overloading (overloading)

Method overloading is a means for a class to handle different data types in a uniform manner.

You can create multiple methods in a class that have the same name but have different parameters and different definitions. Calling methods determines which method is used in detail by the different number of parameters and the type of parameters passed to them.

The return value type can also be different. The return type cannot be used as a distinguishing criterion for overloaded functions.

Rewrite (overriding)

Subclasses write the methods of the parent class again. Suppose that the method in the subclass has the same method name, return type, and reference as its parent class, we say that the method is overridden (overriding).

If you need a method from the parent class, you can use Superkeyword, which keyword references the parent class of the current class.

The access modifier permission for a subclass function cannot be lower than the parent class.

Publicclass human{//Human is the name of the class      String name;//which is the definition attribute       //This is the definition behavior public      void Speack (String words) {           SYSTEM.OUT.PRINTLN (words);       }  

<span style= "FONT-SIZE:18PX;" >humanzhangsan = new Human (); </span>

This creates the Zhangsan person.

The wording of Humanzhangsan is similar to int i. Focus on the following new Human ()


Class and Construction methods


The construction method runs when a detailed object is instantiated by the class.

For example, human is a class that defines 2 construction methods. One without a reference and one with a number of references. See the form of the construction method---no return value.

<span style= "FONT-SIZE:18PX;" >publicclass human{  stringid;//This is a property that identifies the uniqueness of an object stringname;  This is the constructor method called when new Human () Human () {}  ///This is Newhuman ("111111111") when the constructor is called Human (Stringshenfenzheng) {    id= Shenfenzheng; }  } </span>


Newhuman () Creates a human instance object by calling a constructor that has no parameters.

Methods of the class

method is the support of a class behavior, the whole method defines the scope of the class, and the detailed implementation process is written in the content of the method. The definition of a method follows a certain format, namely:

Modifier return value type method name (parameter type number of parameters)

<span style= "FONT-SIZE:18PX;" >publicvoid Speack (String words) </span>

In this example:

Modifier: Public (this tedious later speaking)

return value: void (null, which does not return a value)

Method Name: Speak

Parameter type: string (String type)

Parameter name: Words (this is the name of the variable inside the method)

Class properties and Access Methods

As one of the OOP principles: data hiding (or encapsulation). An object's properties cannot be directly interviewed by an external object, but should be completed by the get (take), set (Save) method provided by the object itself.

<span style= "FONT-SIZE:18PX;" >publicstring GetName () {     return name;}  Publicvoid setName (String aName) {     name = AName;  

A more reasonable class

At this point, we have a fragment of a class, the following stitching up it:

<span style= "FONT-SIZE:18PX;"  >publicclass Human {String id; The property variable of the class String name; Property variables for class//This is the constructor method called when new Human () Human () {}///This is the constructor method called when new Human ("111111111") is Human (String shen     Fenzheng) {id = Shenfenzheng;     } public String GetId () {return id;     } public void SetId (String shenfenzheng) {id = Shenfenzheng;     }//The method of taking the name content public String GetName () {return name;     }//Change the name content method public void SetName (String aName) {name = AName;     }//class behaves public void speak (String words) {System.out.println (words);  }} Use instance: Publicclass meeting {public static void main (string[] args) {//A little baby was born Human Zhangsan =         New Human ();         The registered account, the ID number is Zhangsan.setid ("1234567890123");          The parents gave a name Zhangsan.setname ("Zhang San");         Another baby was born Human xiaoming = new Human (); Xiaoming.setid ("3210987654321");         Xiaoming.setname ("Xiao Ming"); Two people met, Zhang San first greet zhangsan.speak ("Hello!"         I am "+zhangsan.getname ()); Out of politeness, Xiao Ming returned to the sentence xiaoming.speak ("Hello!"

I am "+xiaoming.getname () +", very pleased to meet you "); }} </span>



One: The object-oriented basic concept of Java

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.