Introduction to Java chapter I classes and objects in the second quarter

Source: Internet
Author: User

Object-oriented programming objects oriented programming OOP

First, what are classes and objects

Before you specify classes and objects, talk about something else.

The most useful organ of the eye in the human body. If one has no eyes, this person's connection with the world will be greatly reduced. Because the human brain is mainly through the eyes to obtain data, of course, there are other organs obtained by the OH data to help us more accurately describe what we see. The eye works through light, projecting an objective physical world into the human brain. The human brain is made up of billions of brain cells. Brain cell neurons have a feedback mechanism that, when cascaded brain-brain cell neurons come together, through repeated experiments and tests, the brain gets a more accurate description and interpretation of the objective world (the human brain can understand).

People who have studied the theory of artificial neural networks in machine science know Alvinn systems. The Alvinn system is a three-layer network with a huge input layer (960 input neurons). The input of the Ann is a 30x32 pixel image, and the output is the direction of the vehicle's travel. the Alvinn system uses a reverse propagation algorithm to learn to drive a car (), which has a top speed of 70 mph. The neural network is displayed on the left side of the graph, and the image of the camera entered is below it. The left figure shows how the image from the front camera is mapped to 960 neural network inputs, which are fed back to 4 hidden units and connected to 30 output units. The network output encodes the recommended driving direction. The right image shows the weights of a hidden unit in the network. The 30´32 weights that enter this hidden unit are displayed in a large matrix, the white squares represent positive weights and the black squares represent negative weights. Weights from this hidden unit to 30 output units are drawn in a smaller rectangle above the large matrix. From these output weights, it can be seen that activating this hidden unit facilitates turning to the left. Here are three figures.

  

  The Artificial Neural network (Artificial neural Networks--anns) is inspired by biology, and the biological learning system is an unusually complex network of interconnected biological neurons.。 • The composition of the human brain, about 1011 neurons, each connected to the other 104, the activity of neurons is usually activated or suppressed by connections to other neurons. • The fastest neuronal conversion time is much slower than the computer, but the human brain can make extremely complex decisions at an alarming rate. Many people speculate that the biological neural system   The information processing ability of the system must benefit from the highly parallel processing of the message expressed on a large number of neurons. So, we turned around to show that the human brain learns more advanced and Anns. Back to classes and objects, I'm using the artificial neural network example for who's what? The object is our sensory perception of the data, is objective. And the class is what our brains do to this objective object.Description。 This description of the behavior, description process is a learning process, is an ongoing experiment and testing process. The more accurate the description, the greater the influence we have on the objective world. A little bit far away, anyway, a programming language is destined for simple data types from the beginning, such as int, float, array, structure, to advanced descriptive form--class . to implement complex functions and describe complex phenomena, we need better and stronger descriptive tools. In science, mathematics is the description tool. This is an off-topic.

Second, how to define a class in Java

Walk in three steps: 1) first define a class that requires the class name and the first letter capitalized; The method is to build a workspace > Engineering .> Package > class

2) in defining what the class is, that is, the member variable

3) Method ("method" in Java is the same as "function" in c), which shows what function the class has

For example:

public class Telphone () {

String name= "World Cup";

Static String Classname= "Java Development class";//static variable declaration and assignment.

What are the attributes (member variables)

float screen;

float CPU;

float mem;

Way to do

void call{

System.out.println ("Telphone has the function of calling! ");

}

void sendmessage{

System.out.println ("screen" +screen+ "CPU" +cpu+ "mem" +mem+ "Telphone has the function of texting! ");

}

Non-parametric construction method

Public Telphone () {

SYSTEM.OUT.PRINTLN ("The construction method of the non-parametric structure!") ");

}

A method of constructing a parameter

Public Telphone (float newscreen,float newcpu,float newmem) {

Screen=newscreen;

CPU=NEWCPU;

Mem=newmem;

SYSTEM.OUT.PRINTLN ("The construction method of the non-parametric structure!") ");

}

public static void Main (string[] args) {

System.out.println (Telphone.classname);

The following methods are not available.

System.out.println ();

}

}

Third, how to use the objects in Java

To work with objects:

# Create Object

Class Name Object name = new class name ();---if there is a Telphone class, create the object:

Telphone phone = new Telphone ();

This creation process is instantiated. Objects are stored in memory.

Use objects

Reference object's properties: Object name. Properties

Phone.screen = 5;//assigns a value of 5 to the screen property

Method (function) for referencing an object: Object name. Method ();

Phone.sendmessage ();//Call SendMessage () method

The C language (c Primer Plus chapter14.sructure and other Doata Forms) mentions that declaring a struct as a template, and then defining a struct-body variable with this struct, This variable is then initialized and applied. In Java, the Declaration and use of the "class", I think with the C structure of the use of law, but also first define a class, and then use this class to define a new class, the class, the operation.

Template class, play the role of exemplary, prototype. If the above mobile phone class definition and use, if I have two different configurations of mobile phones, then, our new definition of the two mobile phone class its variable value and method function may not be the same.

IV. member variables and local variables in Java 1) member variables and local variables in Java

1. Member variables

defined in the class to describe what the object will be.

By default, its value is zero. The float type variable is assigned a value, followed by an F.

2. Local Variables

Defined in a method of a class to temporarily save data in a method.

2) The difference between a member variable and a local variable

A. Different scopes

The scope of a local variable is limited to the method that defines it.

The scope of a member variable is visible within the entire class.

In layman's terms, the scope is the concept of animal territory. The African Lions can attack zebras, horns, and so on, but in turn zebra and horn horses do not actively provoke the African lions.

b, the initial value is different

There is a default initial value when defining a member variable;

There is no default initial value when defining a local variable.

C, in the same method, local variables with the same name are not allowed.

In different methods, you can have the same name local variable.

D, when two types of variables have the same name, local variables have a higher priority----the nearest principle

    

The construction method in Java

1. Construction Method:

A, using the new+ construction method to create a new object, thereby instantiating

B, the constructor method is defined in the Java class to Initialize the object method

Why would you want to initialize an object with a constructor method?

There are too many variables and methods defined in the class, so that copying and assigning variables and methods becomes tedious and error-prone.

If we take this tedious and error-prone initialization process and let the computer do it automatically, it saves the programmer's brainpower and improves the accuracy and speed of programming.

C, so far, a class includes: variables, construction methods, methods,

The construction method, from the name can be seen as a method, is a special method, specifically used to implement the initialization class.

2. The constructor method has the same name as the class and no return value

The construction method is to be included in the class.

3. Non-parametric construction method

4. The method of constructing a parameter

There are both parameters and non-parametric construction methods that can coexist.

5, but no constructor method is specified, the system automatically adds an argument-free construction method.

This feature is also designed to save costs and automate "production lines".

6, when there is a specified construction method, whether it is a method of construction with a parameter, no argument, will not automatically add the construction method of no parameter

7, the construction method of overloading: The method name is the same, when the parameters of different methods, the call will automatically select the appropriate method according to the different parameters

In fact, a class with no parameter of the construction method and the method of construction, in fact, is the method of overloading.

Problem: How does the overload of the method work?

offers a variety of options,

8, the construction method can not only assign values to the object's properties, but also ensure that the property of the object assigns a reasonable value

How to achieve it? The problem of incorrect value is solved by adding the conditional judgment in the method of the parameter construction.

    

VI, static variables used in Java

"We all know that we can create multiple objects of that class based on a class, each with its own members and independent of each other." At some point, however, we would prefer all objects of the class to share the same member. This is the time for static!!

Members in Java that are decorated statically are called static members or class members. It is owned by the entire class, not by an object, which is shared by all objects of the class. Static members can use direct

Access can also be accessed using the object name. Of course, given the particularity of his role, it is more recommended to access the class name ~ ~

Use static to modify variables, methods, and blocks of code. ”

1. Static variables and how to use them

How to use the public static variables: "Static" String Classname= "Java Development class";

How to use a private static variable: Private passive String classname= "Java Development class";

It is recommended that the class name be accessed, so the static variable can be called by using the definition of the above code snippet: System.out.println (telphone.classname);

2, static method, static class

As with static variables, we can also use the static modification method, called a static method or a class method. In fact, the main method we've been writing about is static methods.

A, static methods can directly call static members in the same class, but cannot call non-static members directly. If you want to call a non-static variable in a static method, you can create the object by creating the class, and then

    Non-static variables are accessed through objects.

Pbulic class helloworld{

String name= "World Cup";

Static String hobby= "WorldCup";

}

    public static void print () {

To create an object of a class

HelloWorld hello=new HelloWorld ();

To invoke a non-static variable in a static method by pushing

System.out.println ("like" +hello.name+ "!"); /Non-static

System.out.println ("I Love" +hobby+ "!"); /static

}

b, in the ordinary member method, you can directly access the same kind of non-static variables and static variables.

public void Show () {

System.out.println ("Like" +name+ "!"); /Non-static

System.out.println ("I Love" +hobby+ "!"); /static

}

    C, static methods cannot call non-static methods directly, you need to access the non-static method through the object.

public void Show () {

System.out.println (I love the World Cup!) ");

}

public static void print () {

SYSTEM.OUT.PRINTLN (2016 World Cup!) ");

}

public static void Main (string[] args) {

The normal member method must be called by the object.

HelloWorld hello=new helloworl ();

Hello.show ();

Static methods can be called directly

Print ();

}

VII, static initialization block

Introduction to Java chapter I classes and objects in the second quarter

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.