Java Chapter 6 class and Object

Source: Internet
Author: User

Meanings of class modifiers:

Public declares a class as a public class, which can be accessed by any object. The main class of a program must be a public class.

Abstract declare a class as an abstract class. If there is no implemented method, the subclass must provide the Implementation of the method.

Final declares a class as a final class, that is, a non-inherited class, indicating that it cannot be inherited by other classes.

The default modifier of the friendly type. This class can only be used by objects in the same package.

 

Description of the member variable modifier:

Public Access Controller. specifies that the variable is public and can be accessed by methods of any object.

Private access controller. specifies that this variable only allows access to methods of its own class. Methods in other classes (including subclasses) cannot access this variable.

Protected protects the Access Controller. Specify that the variable can be accessed by its own class and its subclass. This variable can be overwritten in the subclass.

Friendly is the default access controller. Other classes in the same package can access this variable, while classes in other packages cannot access this variable.

Final modifier. The value of the specified variable cannot be changed.

Static modifier. specifies that the variable is shared by all objects, that is, all instances can use this variable.

Transient Over modifier. This variable is a temporary variable that is reserved by the system and has no special effect.

Volatile modifier. specifies that the variable can be controlled and modified by several threads at the same time.

 

Meanings of member method modifiers:

Synchronized is a synchronous modifier. In a multi-threaded program, this modifier is used to lock the method to which it belongs before running to prevent access by other threads. It is unlocked after running.

Native local modifier. The method body that specifies this method is written outside the program in other languages (such as C ).

 

Eg:

Define a cylinder class, create corresponding objects, and then calculate the bottom area and volume of the cylinder.

Package example;

Class Cylinder
{
Double Radius;
Int height;
Double Pi = 3.14;
Void area ()
{
System. Out. println ("bottom area =" + pI * radius );
}
Double volume ()
{
Return (pI * radius) * height;
}
}
Public class unit6 {

Public static void main (string [] ARGs ){
Cylinder volu;
Volu = new cylinder ();
Volu. radius = 2.8;
Volu. Height = 5;
System. Out. println ("radius =" + volu. radius );
System. Out. println ("high =" + volu. Height );
System. Out. Print ("cylindrical ");
Volu. Area ();
System. Out. println ("cylindrical volume =" + volu. Volume ());
}

}

They are all basic knowledge, but I just summarized it myself + read books.

 

Eg: transpose a matrix and output it.

Int [] [] transpose (INT array [] [])

{Int temp;

For (INT I = 0; I <array. length; I ++) // transpose a Matrix

For (Int J = 0; j <array [I]. length; j ++)

{

Temp = array [I] [J];

Array [I] [J] = array [J] [I];

Array [J] [I] = temp;

}

Return array;

}

 

Anonymous object:

After an object is created, you can call the method of the object without defining the reference variable of the object. Such an object is called an anonymous object.

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.