"Small white Java Growth series"--Object-oriented Foundation

Source: Internet
Author: User

Today, the Java object-oriented knowledge point ~ in fact. Java can be successful in its development process. Very large part of the reason is its object-oriented thinking ~

1. Concept

Nowadays, object-oriented thinking. is not a new knowledge point, we know that today very many languages have object-oriented thinking, of course, we also know today is still very fire C language. In fact, it's a process-oriented language. Contains the recent period of time also appeared a lot of new ideas, than the aspect of the function, oriented to the distributed, oriented ... And so on a series of things, in fact, these are just a concept ~

In object-oriented. There are, in fact, three basic features:
• Encapsulation: To ensure that the external is not visible;
• Inheritance: Continue to carry forward the vast majority.
• Polymorphism: It belongs to change to change;


2. Classes and objects

Well, the concept is done. Let's talk about classes and objects.

Class: A class is a set of common products, is the same kind of things concentrated expression.
Object: Is the embodiment of a personality, is the product of the individual.

This sentence is very important: A class is a template for an object. Object is an instance of a class


3. Definitions of classes in Java

There are two ways of defining classes in Java: public class and Class

· The class declared with the public class must be the same as the file name, assuming that classes declared with class can differ from the file name

· In a *.java file, there can be only one public class. But there are multiple class definitions at the same time.

· If you define a class, you need to capitalize the first letter of each word--conventions

For example, the definition in the People.java file here:

Package me.javen.oop;//Pack Name class Person {//define class string name;//define attribute int age;//define Property public void Say () {//define method SYSTEM.OUT.PR Intln ("123");}} public class People {//define class public static void Main (string[] args) {//define method}}

4. Using class-to-object in Java

So, how do you use the classes defined above?

Person person = null;//is declared in the stack memory, this process is called declaring the object person = new person ();//opening up the corresponding heap memory space, this process is called instantiating the object

In fact, can write a statement, generally in the development is also a bar ~

That is: person person = new person ();

This is to show that the memory structure is written in two articles.

See the following procedure:

public static void Main (string[] args) {//define method Person man = Null;person = new person ();//Use Properties in Class Person.name= "Mr. Rice";p er Son.age=24;person.say ();//usage}

This way, the basic use of classes and objects is complete.

To come down and introduce the construction method

5. Construction method

Definition: The method name defined in a class is the same as the class name. And no return value declaration method, called a constructor method.

Or see just the code: person = new person ();

In fact, in the new process, new is the default constructor for the person class.

Remember: in a class, assuming that there is no clear definition of a construction method, you will actively generate a non-participatory, do-nothing construction method. If a constructor is present in a class, you must use the defined construction method in the new procedure.

This sentence for small white, it may be a bit difficult to understand, the semicolon before the possible good understanding a little bit. The first half of the sentence can be understood by the person who participates. The second half of the sentence about using the construction method may be a bit difficult. In fact it is not difficult to see the following procedure:


See, no, the Java syntax check for Eclipse reports an error, which is a constructed method that does not define person (), and is defined in the person class as a constructor of person (String name, int age).

Of course, the construction method can define multiple ~

Class Person {//defines the String name;//defines the attribute int age;//defines the property public the person (String name, int age) {//defines two parameters for the construction method}public person (String name) {//Define a constructor method for a number of}public person () {//define no-constructor method}public void Say () {//define method System.out.println ("Name:" + name + ". Age: "+ Ages";}}

In this way, the construction method is said to be finished.

The following is the encapsulation.

6, encapsulation of

From the definition: encapsulation means that the definition inside the class is invisible to the outside.

The role is to protect the security of the data ~

What does it mean to be invisible to the outside?

Look at the code:

private String name; Define attribute private int age; Defining properties


There will be errors in the process, prompting for no permission access (not visible to the outside)

One of these forms is called encapsulation ~

So, how did the name and age properties come to the interview?

Package me.javen.oop;//Pack Name class Person {//define class private String name;//define Property private int age;//define Properties */* * Use setter and Gett for encapsulated properties Er method */public String getName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;} Public Man (string name, int age) {//Define two parameters construction method}public person (string name) {////define a parameter constructor}public person () {//define non-constructor party Method}public void Say () {//define method System.out.println ("Name:" + name + ", Age:" + Ages);}} public class People {//define class public static void Main (string[] args) {//define method person person = Null;person = new person ();//Tim Add no-reference construction method//Set the value by setter method Person.setname ("Mr. Rice");p erson.setage ();p Erson.say (); Usage//Getter method Gets the value of the encapsulated property System.out.println ("Name:" + person.getname () + ", Age:" + person.getage ());}}

Note:

1. The property or method does not want to be interviewed externally. can use the Privatekeyword declaration;

2, in the development process. Assuming that it is not a special case, the properties defined in the Java class basically require a set encapsulation.


"Small white Java Growth series"--Object-oriented Foundation

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.