[Java] _ 2_java course 7

Source: Internet
Author: User
Tags define local

I haven't come to the garden for a long time. Just after the Spring Festival, I want to say the following thing: Are you dating me?

All said: lonely men play Dota, lonely women wear stockings. I think in this era, yundun will be able to go QQ, go to school, run a cat flutter, and go all over the world. It's okay to make a few complaints,

It's okay to catch up with a celebrity on Weibo. As for the rich and handsome guys, they are either driving a Ferrari, driving a Porsche, or sitting on a Jaguar on the highway.

I remember that when I first started my blog last year, I spoke about Delphi, which mentioned the application of Pascal-like language-like SCL in the industrial control field. In view of the computing power of my friends who are currently engaged in industrial control

I was not very interested in computers, or I thought that the computer content was too difficult. At the same time, as far as I know, there is no textbook on the Chinese version, so I started to compile it.

A tutorial; I want to provide a convenient tool for reference. After all, many devices in China use the PLC programming tool, and some text was hung in the garden.

I have provided a trial chapter on this teaching material. These texts are now in chapter 4, and the first draft is expected to be completed in May.

[Siemens S7 300 400 SCL programming] Download Page: http://www.gongkong365.com/bbs/read.php? Tid = 34524

Download link: http://www.gongkong365.com/bbs/job.php? Action = download & Aid = 26976

At present, this document is in DOC format. After it is completed, it is planned to be organized into PDF format. If possible, it may be made in paper.

So what should we talk about Java this time?

[Constructor in Java]

The last time I spoke about some of the content about OopArticleWe design an employ class which has the following fields:

PrivateString name;PrivateBoolean gender;PrivateString phonenumber;Private DoubleSalary;

The role of the constructor is to initialize the object when the system constructs the object. The constructor is implemented by the constructor method or constructor function.

The method/function is the same as the class name. As follows:

 
 PublicEmployee (string inname,BooleanIngender, string inphonenumber,DoubleInsalary ){}

This public employee method is the constructor of the class employee (in order to facilitate the future constructor methods and the average name constructor of Functions ). To implement the constructor's purpose of initializing the instance domain

You must add a statement to the constructor. Below is our general implementation:

PublicEmployee (string inname,BooleanIngender, string inphonenumber,DoubleInsalary) {name=Inname; Gender=Ingender; phonenumber=Inphonenumber; salary=Insalary ;}

When constructing an object, the constructor is called to initialize the instance domain of the object to a specified value. For example, when we construct an object using the following statement:

New Employee ("volcanol", true, "13555555555", 1000.0 );

The new object has the following instance domain values:

Name = "volcanol"

Gender = true;

Phonenumber = "13555555555 ";

Salary = 1000.0;

Unlike other methods and functions, the constructor must be used together with the new operator to apply for a bucket through the new operator, and the object through the constructor

Initialization.

At the same time, you cannot call the constructor of an existing object, which may cause exceptions.

Constructor points: 1. constructor and class have the same name

2. Each class can have more than one Constructor

3. the constructor can have 0, 1, or more parameters.

4. the constructor does not return values. This is important.

5. the constructor is always used with the new operator.

6. do not define local variables with the same name as the class instance domain in the constructor.

[Display parameters and implicit parameters]

The method is used to operate objects and to access the instance domains of objects. Example:

Public BooleanSetname (string inname ){If(Inname. Equals ("") | inname =Null) {System. Out. println ("You have input wrong name, please try again :");Return False;}Else{Name=Inname ;}}

Here, this method is used to set the instance Domain Name of the class. To prevent output errors, we can use a circular drive until the input is correct.
Suppose we have constructed an object, employee1, so we can call this method:

Employee1.setname ("volcanol ");

After the method is called, The instance Domain Name of the object is set to volcanol. In fact, setname has two parameters in the call process. One is the inname parameter.

One is an implicit parameter, which is the EMPLOYEE 1 object before the method. The displayed parameters are provided in the parameter list. The implicit parameters are not displayed in the parameter list.

In each method, the keyword "this" indicates the implicit parameter, and the parameter "this" indicates the object itself. For example, we can write this method:

Public BooleanSetname (string inname ){If(Inname. Equals ("") | inname =Null) {System. Out. println ("You have input wrong Parment");Return False;}Else{This. Name = inname;//The displayed implicit parameter This is called.}}

Here we can see that using the modifier method, we can check the input parameters to reduce the chance of exceptions, so this is alsoFault Tolerance.

[Access method and encapsulation]

In the designed employee class, we set several accessors:

 
PublicString getname (){ReturnName ;}Public BooleanGetgender (){ReturnGender ;}PublicString getphonenumber (){ReturnPhonenumber ;}Public DoubleGetsalary (){ReturnSalary ;}

These methods have one feature: the method simply returns the value of the Instance domain. Why do we not design the instance domain as a public attribute, but a private attribute?

We usually do not need to modify some information of an employee after it is set up, such as the name, birthday, address, ID card number, and so on. To prevent these new information from being accidentally modified

This mechanism is used to avoid illegal modifications.

After we design all the instance domains as private, and some content needs to be modified, we can design the instance domains of the class according to the actual application:

1. method for obtaining instance domain accessors

2. How to modify the modifier of an instance domain

This is done to improve encapsulation.CodeIt will become more complex. Of course, this is also advantageous:

1. When the interface of the method remains unchanged, you can modify the implementation of the class without affecting the users of the object.

2. You can perform fault tolerance detection on data. For example, you can check whether the inname parameter is null.

[About private and final]

If you have been familiar with C ++, you should know that there are several access control policies on the access control policy of C ++: public, private, and protected,

There are also several access control policies in Java:

1. Public: The public access policy means that the domain can be accessed by the class object itself or by other class objects.

2. Private: The domain defined as private can be accessed by the class object, but cannot be accessed by other class objects.

At the same time, the method defined as private can modify the interface of the object without affecting the user of the object,

The class designer needs to re-build the class.

Therefore, if a domain of a class does not want to be referenced by objects of other classes, the most appropriate method is to design it as a private access control policy.

As mentioned earlier, this keyword is final. The object declared or defined by this keyword is a bit similar to the const variable in C ++, but not exactly the same.

Resolution.

Objects or variables defined as final in Java cannot be changed once initialized. This means that if a final instance domain is designed in a class, it must be constructed in the class.

You also need to modify the instance domain in the future reference process, that is, you cannot use the modifier method to access the final attribute instance domain.

For example:

Private final string name;

That is to say, after an employee's name is registered in the company's information system, the employee's name domain cannot be changed unless the employee's object is deleted/deregistered.

Note the following:

1. The final modifier is generally applied to fields of the basic data type or data fields of the immutable class.

2. When the final modifier modifies a mutable object, it does not mean that the object is not mutable, which may cause confusion. (Does it mean that its reference points cannot be changed ?? This issue needs to be further studied ).

For example, we design such a domain:

Private final date birthday;

After constructing an object with the constructor, other objects can call the birthday method to change the object.

Static domain and static method]

1. Static domain

The static keyword is used to define the static domain of the class. After the static domain is defined in the class, all instances of the class share the static domain represented by the same memory.

For example:

Class employee

{

Private int ID;

Private Static int nextid = 1;

}

If this class is used to define the instantiated object, all objects share a nextid static domain space. If an object changes this static domain, other objects

All static domain references will change.

If a static domain is defined in a class, even if the object is not instantiated, the static domain nextid static domain exists. The static domain belongs to the class and does not belong to any independent object.

Note the following:

The non-static fields of the class need to be referenced through the class object, while the static fields can be referenced through the class, that is, we can reference the static fields as follows:

Employee. nextid = xx;

2. Static Constants

Static constants can be defined in the class. For example, a static constant is defined in the math class:

Public class math

{

........

Public static final double Pi = 3.14159265358979323846;

..........

}

In this wayProgramYou can reference this static domain constant without instantiating an object. For example:

S = r * Math. Pi; // area = pI * r ^ 2

If the keyword is omitted, PI becomes an instance domain of the math class. Then, you need to access PI through the math class object, and each math object

There will be a copy of pi.

3. Static Method

Static methods cannot perform operations on objects. We know that there is a POW method in the math class. This method is a static method,

Use the following method to call the POW method:

Math. Pow (X, A) // The result of computer x ^ A. No math object is used during computation, that is, there is no implicit parameter This.

Static methods cannot operate on objects, so they cannot access non-static instance domains in static methods. Static methods can access static domains in their own classes.

For example:

Public static int getnextid ()

{

Return nextid; // static nextid

}

You can call this method by Class Name:

Int n = employee. getnextid ();

Of course, we can also call this method through the object at this time: int n1 = employee1.getnextid ();

The static method is usually used in the following situations:

1. A method does not need to access the object state, and its required parameters are provided through the display.

2. The method of a class only needs the static domain of the category class.

4. Main Method

Each class can have a main method, but the main method of each class in the application is not necessarily executed.

Note that the main method must be defined as static, and we can test a class separately.

For example, an application is defined.

Public Class Application

{

Statement ....

Employee employee1 = new employee ();

}

Public class employee

{

Public static main (string [] ARGs [])

{

Statement ......

}

}

We can compile the employee class: Java employee separately. In this case, only the employee class is tested, and other classes in the application are not included unless in the employee

Other classes are referenced.

You can also compile the entire application: Java application. At this time, the main method in the employee class cannot be executed, that is, the application can have multiple classes

Main method, but the application has only one entry.

Today, we will talk about the content of class method parameters.

There is another question that I don't quite understand here. I don't know which of the following great gods will explain it as the final keyword in the above text. (Does it mean that its reference points cannot be changed ?? This issue needs to be further studied ).

 

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.