A comprehensive explanation of Java-oriented object-oriented programming ideas _java

Source: Internet
Author: User

What is Object oriented?
object, oriendted ... -oriented, programming programming

Object-oriented is the use of object programming, shorthand for OOP.

SP and OOP comparisons

Three main principles of object-oriented
Encapsulation Inherits polymorphism

Object

object is the core part of object-oriented programming, and it is the concrete entity, which has well-defined state and behavior.
Objects are actually packages of "data" and "functions," Where:
The data represents its own state, also known as "attribute" or "member data";
functions represent their own functions, also known as "methods" or "member functions."

Class
people in order to better understand the world, the real life of Things (objects) divided into classes;
Things in the same class always have some commonness;
Classes define entities in a common nature and behavior;
A class is a collection of objects with the same attributes and behavior.

Property
the properties of things are expressed in variables in class;
Each property of each object has its own specific value;
The property name is shared by all objects of the class;
A feature owned by an object or entity is called a property when it is represented in a class

Method
the behavior and action of things are expressed in the class by function;
Each object has the same action and behavior;
object is represented as a method in the class.

The difference between a class and an object
A class is a "template" or "prototype" used to describe an entity;
object is the actual entity, each object is a concrete instance of the class;
Class is used to define all the properties and methods of an object, and all objects of the same class have the same characteristics and operations;
The class can be understood as the production of the mold, and the object is based on this mold production of a product.

Class and Structure

Packaging
wrapping something together and presenting it in a new, complete form;
The handling of hidden attributes, methods, or implementation details is called encapsulation;
Encapsulation is actually selectively exposing or hiding certain information, which solves the security problem of the data.

Inherited
inheritance is a feature that reuses existing classes to generate new classes;
In layman's terms, the process of creating a new class (subclass or derived class) from an existing class (that is, a parent class or base class);
In real life, inheritance can achieve the purpose of property reuse, while in Java, inheritance can make code reusable.

Polymorphic
polymorphism means that the same function has different implementations in different classes.
The advantage of polymorphism is that it makes classes more flexible and easier to expand.

Here's another "abstraction" that has to be said.
Abstraction
the process of attribution of the same or similar objects to a class is abstract, so abstraction is the method of analyzing the problem;
The basic principles of abstraction:
Care only about the main problem, not the secondary;
Only concerned about the main contradiction, but not the secondary contradictions;
Only care about the same things, and do not care about different things;
Care only about what the problem is, what it can accomplish, and not how to do it.
Abstract process is actually the core idea of object-oriented programming.


defining classes in Java

creating objects in Java

Create the syntax for an object
arrays are similar, objects are reference data types, and only the new operator can be used to allocate memory from the heap;
To create a general syntax for an object:
Class Name Reference name = new class name ();
Using a class that has already been defined, the process of creating the class object is called "instantiation."

Member operator "."
You can access members (properties and methods) in a class only if the object of the class is instantiated first;
Use the member operator (.) To access member properties or member methods;
The general syntax is:
The name of the object. Member name
Such as:

Std.age =;  Assign a value to a member property
  std.dining ();//Call member method

Access rights: Public and private
C language, members of the structure can be accessed from anywhere, which will leave a great danger to the security of data;
In order to avoid data corruption caused by directly accessing class members from outside the class, Java has set constraints on the access of class members;
Keywords public and private are access modifiers to indicate whether a member can be accessed from outside the class;
Members of the public adornment can be accessed anywhere, without any constraints;
Private-decorated members can only be accessed by other members in this class, not from outside the class.

Cannot access private members from outside the class;
Private members of other classes are also hidden for the current class.

Access Rights sample

Class Student { 
 private String name;  Name, private, can not access private int age directly from outside class 
 ;    Age, private, you can not access private float weight from outside of class 
 ;//weight, private, not accessible directly from outside of class 
  
 //meal method, public, accessible from anywhere 
 Dining () { 
  System.out.println ("full ..."); 
  weight++; The dining method is a member of a class that can directly access the private member of this class 
  
 ///The method of walking, publicly available, accessible from anywhere in the public 
 Void Walk () { 
  System.out.println ("Tired of Walking ..."); 
  weight--; The walk method is a member of a class that can directly access private members of this class 
 } 
} public 
 
class Test {public 
 static void Main (string[] args) { 
  Student std = new Student (); Instantiate a Student object 
  std.age =;  Attempts to access private members from outside the class will report an error 
  std.dining ();  Allow access to public members 
 } 
} 


Access Rights (cont.)
Adding an access modifier can sometimes be inconvenient to manipulate the data, but it will ensure the security of the data to a large extent;
Generally, we will declare the member property as private, and declare the member method public, but this is not absolute;
Sometimes, outside the class may want to manipulate to some private data members, then can add a public method, and then by this method to manipulate the private data, to avoid the errors caused by the wrong type of data corruption;
Because the main method is called by a virtual machine outside the class, the main method must be declared public.
Such as: Modify the Student class

Class Student {//define Student Class 
 private String name;  Name, privately private 
 int age;    Age, privately private private 
 float weight//body weight, private 
 
 public void SetName (String name) {  //is the method of assigning a name, and publicly 
  this.name = name ; 
 } 
 public void Setage (int a) {    //is the method of assigning the age, the publicly-aged 
  = A; 
 } 
 public void Setweight (float W) {//is the method of weight assignment, publicly 
  weight = w; 
 } 
 public void display () {      //the method of printing all information, publicly 
  System.out.println ("Name:" + name +), Age: "+ ages +", Weight: "+ weight"; 
   } public 
 void Dining () {...}     The method of eating, public, code is slightly more common 
 void Walk () {...}      The way to walk, public, code for a bit of the 
 
class Test {publicly 
 static void main (string[] args) { 
  Student std = new Studen T (); Instantiate the Student class object 
  Std.setname ("John");      Assign a value to a name 
  (std.setage);        Age-assigned 
  std.setweight (in); 
  std.dining () for weight assignment;         Call the way to eat 
  std.display ();        Print out the information 
 } 
 

Object initialization
in the example above, you can only assign values to data members, and if you want to initialize the member properties while the object is instantiated, you use the constructor method.
A construction method is a special member method that has the same name as a class and is automatically invoked by the virtual machine when an object is instantiated;
Note that the constructor does not return a value type and cannot have a return value.
Construction Method Example:

/* Define the Constructordemo class, test the construction method 
/class Constructordemo 
{ 
 /* Construction method, the method name is exactly the same as the class name 
 without specifying a return value type or a return value * * Public 
 Constructordemo () 
 { 
  System.out.println ("This is a construct method") 
 ; 
} 
 
/*test class, used to hold the Main method 
typically declares a class containing the Main method as public*/public class 
Test 
{ 
 /*main method, program entry 
 /Public static void Main (string[] args) 
 {/ 
  * The object of the Constructordemo class is instantiated * * 
  constructordemo cd = new Constructordemo ( ); 
 } 
}    Will output "This is the construction method" 

Construction method
it is because the constructor method is invoked automatically at the same time as the object is instantiated, so the construction method is generally used to assign resources to data members or initialize data members;
The general form of the construction method:
Access permission class name (parameter list) {
Method body
}
Because the virtual machine calls the constructor method, the construction method should generally be defined as public.

For example, add a construction method for the student class

Class Student {//define Student Class 
 private String name;  Name, privately private 
 int age;    Age, privately private private 
 float weight//weight, private 
 
 //construction method, assigning public Student to data members based on parameters passed in 
 (String n, int A, float W) {
   //assigns the initial value name = N to each data member respectively 
  ; 
  age = A; 
  Weight = w; 
 } 
 The public void SetName (String N) {...}//is a method of assigning a value to a publicly owned, code-only, 
 setage void (int a) {...}   The method of assigning value to the age, public, the code is slightly common 
 void setweight (float W) {...}//For Weight-assignment methods, public, code slightly publicly 
 void display () {...}  The way to print all of the information is public, the code is just a bit 
 dining () {...}  The method of eating, public, code is slightly more common 
 void Walk () {...}   The way to walk, public, code for a little. 
 
{string[], Test {publicly 
 static void main (args) { 
  //Use the constructor method to specify the initial value for the data member C27/>student std = new Student ("John", N,); 
  Std.display ();        Print out the information 
 } 
  


Construction Method (cont.)
Each object must be constructed at build time and be executed only once;
If the constructor method call fails, then the object cannot be created;
You cannot explicitly call the constructor method directly;
Without defining a construction method, the class automatically produces a default constructor without parameters, and the default constructor does nothing;
Once the construction method is explicitly defined, the default construction method disappears automatically. Therefore, the general definition of parameterless and two construction methods.

Summarize

    • Objects consist of State (attribute) and behavior (method);
    • A class is a collection of objects with the same properties and methods;
    • Encapsulation can hide the specific details of an object implementation;
    • You must instantiate the object of the class before you can access its members;
    • The member operator is used to access the members of the object;
    • A member can be defined as public or as private;
    • Construction methods are typically used to initialize data members in an object;
    • If you do not define a constructor method, there will be a default construction method, and once defined, the default construction method automatically disappears.
Related Article

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.