Study summary of the week four

Source: Internet
Author: User
Tags border color

One, Java garbage collection mechanism

The garbage collection mechanism is to reclaim objects that are not pointed to by variables.

The garbage collection mechanism is performed by the virtual machine, and the virtual machines are not able to intervene when they are relatively idle for garbage collection.

But programmers can speed up garbage collection by calling System.GC ().

Add: Return is used in addition to returning a value in a return type to return the main method in void no return type

Understanding: Java technology allows the use of the Finalize () method to do the necessary cleanup before the garbage collector clears objects from memory.

Second, the construction method

Format of the construction method:

public class student{

Public Student () {

}

}

The construction method is a special method

The method name must be the same as the class and there is no return type its effect is to produce the object

Construction method initializes the object

The function of the construction method:

1. Allocating space to Objects

Student a=new Student ();

2. Initializing member variables

3. Return the address of the created object

(member variables are initialized with object initialization and static variables are initialized with class loading)

If a class does not have a construction method defined, the virtual opportunity automatically assigns a non-parametric construction method

However, if a class defines a construction method, the default parameterless construction method disappears unless you define it again.

Third, form participate in the actual parameter

In a method, using the variable name directly takes precedence over the formal parameter (which is essentially a local variable), and if the local variable does not exist, the member variable or static variable is used.

This property name is used for member variables

Iv. inheritance

Class is an abstract subclass of an object that inherits the parent class from the child class.

Benefits of Inheritance:

1. Reuse of code

2. Properties and methods of the parent class can be used for subclasses

3. Can make the design of the application easier

4. Subclasses can extend more properties and methods based on the parent class

Principles of use of inheritance:

Observe the classes to be used, and determine their common and unique characteristics (attributes), behavior (methods);

Extract the generic move to the parent class. Facilitates the reuse of methods and properties by subclasses.

For different features and behaviors, you can redefine or extend them within subclasses.

(Supplemental, shortcut key save Ctrl+s,eclipse file with * is not compiled (not valid, no class file generated))

In Java, a class can have only one parent class, and a class cannot inherit two or more classes at the same time (such as the object class, the Human class, the man class. Human inherits the object class, man inherits the Human class, but man can no longer inherit other classes. );

The root class of all classes is object;

Five, GUI

Component:

1.jframe form

2.jpanel Panel

Settings for panel border color

Border Bo1 = Borderfactory.createlineborder (Color.yellow);

Jp1.setborder (BO1);

3.jtabbedpane Label Panel

3.jtextfield text Box

4. JButton button

5.jradiobutton radio button Buttongroup button group enables the effect of selecting only one button by using a button group

6.checkbox check box

7.jcombobox drop-down box to add content jtxt1.additem ("online");

The 8.Jlabel label includes a Jlabel a=new Jlabel ("img/picture 1.jpg") which is also a label for the image;

Cropping a picture

Image img=new ImageIcon ("img/picture 1.jpg"). GetImage ();

Img=img.getscaledinstace (300,300,1);

The big picture as the background map should be put in the last

Form Basic settings:

In the construction method:

This.setlayout (NULL); Set the absolute layout first

..............................

Adding various components in the middle

..............................

This.setsize (x, y); Set Dimensions

This.setlocationrelativeto (NULL); Set Location

This.setdefaultclosedopreation (3); Default Close Form

This.setvisible (TRUE); Setting visibility

Added: When a component group such as a label and a text box appears, the combined class inherits the text box as well, inheriting the tag. But generally inherit the text box because we actually use the

will be mainly text box, the label is just a hint, no further use value.

Vi. rewriting of the ToString method

Set A is an object of the student class

usually see System.out.println (a); In fact, the default is: System.out.println (a.tostring ()); (If a is not a reference type for the object, print the specific point, otherwise print the address);

If you print the object directly, a memory address is printed.

If we want to print the object's specific values, then we should rewrite the ToString () method in the class of object A.

Add: When a reference class null is pointed to NULL, using a NULL operation will generally be an error, but printing null does not. The reason is also because the default System.out.println (A.tostring ()); The address is printed, and the address feedback is null-pointing.

Seven, static variables and static methods of loading time

Static variables and static methods are loaded as the classes are loaded, so they are loaded before member variables and member methods (which are loaded when the object is produced), where member variables and member methods are not produced, so static methods cannot invoke member methods or properties.

Viii. the relationship between the parent class and the child class

Add: Try not to define multiple classes in a file, because clutter is not easy to find, and a file has a public class, you can no longer define the public class, it is not convenient to use.

Nine, Java.lang bag

The classes in the Java.lang package can be used directly without importing

such as the common system.      Like Math that produces random numbers. Wait a minute

Math.random () indicates that a random number of 0-1 is generated, which can generate 0, but does not generate 1. (may be a decimal or an integer)

Then Math.random () *100 is a random number that produces 0-100;

The random number that produces 60 to 100 is 60+math.random () *40 If the integer is to be generated then it is cast in front (int).

X. Final keywords

1.final modifier variable--Indicates that the value of the variable cannot be modified

2. Modifier class--Indicates that the class cannot be inherited

3. Modification Method-Indicates that the method cannot be overridden

4 Decorated Object-Indicates that the memory address pointed to by the object cannot be modified, that is, the variable cannot point to another object, but its property values can be modified. Because the object is a reference class, the object is an address point, and the decorated object is the address of the decorated object, so it cannot be modified.

Xi. access Modifiers

4 access modifiers are public protected default (no modifier) private

Whether you can use the same class can be

Whether you can use the same package can not be

Whether the subclasses of different packages can be used may not be able to

Whether other classes can be used or not can not be

12. Encapsulation

Encapsulation refers to the organization of information in accordance with the principle of information hiding.

Features of the package: 1. Collection of information 2. Hiding information

The usual behavior of encapsulation: a property in the parent class is decorated with the private modifier, such as private name; Private age;

So theoretically, no other class (including subclasses) has access to this property.

But we can implement the invocation and modification of the private property by setting the two method set and get in the parent class and then calling both methods in its subclass.

The difference between a private property and a public property:

1. Private properties can be set to read-only or write-only properties by removing the set individually or by removing the Get method individually. Public properties cannot be implemented

2. Private properties can be validated before set method settings, such as age cannot be negative or greater than 200, thus guaranteeing the rationality of the attribute value. Public properties cannot be implemented

13. The This keyword and super keyword

This.age

Super.age

This ()

Super ()

Some abstractions don't have much to describe.

Study summary of the week four

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.