Java Learning notes (1)

Source: Internet
Author: User

How do I define a class?

The name of the Modifier class class

{

The contents of the class (including properties and methods)

}

How do I define a method?

Modifier returns the type method name ([parameter 1, Parameter 2, Parameter 3 ...])

{

Method body

}

How do I define a property?

1  Public class  Person 2 {  3    modifier  Type property name; 4 }  5  6// How do I use attributes?  7// As with the method, using the. operator, you first need to generate an instance of the class and then use the instance + "." The way to use attribute 8  9new person (); Ten Person.age

What is an object?

An object is an instantiated representation of a class.

objects are closely related to classes.

A class is an abstract concept, and an object is a concrete concept.

For the real world: human is an abstract concept, and specific people (such as: Zhang San, John Doe) is a specific concept, corresponding to object-oriented programming, people can be regarded as a class , and Zhang San this the specific person can be regarded as the object .

How do I generate an object?

The object is generated by using the New keyword.

 Public class person{} // class name Variable name = new class name ();     Person person New  newnew person ();

Method invocation

Method calls need to be done through the object.

The form of a method call:

// object variable. Method name ([parameter value 1, parameter value 2, parameter Value 3 ...])

Construction method

Construct method to complete initialization of object properties

Characteristics:

1) The name of the construction method must be exactly the same as the class name (including case)

2) The construction method has no return value, and even void does not appear

3) If you define a class and do not construct a method for the class declaration, the Java compiler automatically adds a constructor that has no parameters and the method body is empty

4) If you declare a constructor method for a class when you define a class, the Java compiler no longer adds a constructor method to the class

5) The call constructor method cannot be displayed, and the constructor method can only be called implicitly through the New keyword.

Formal parameter arguments

1  Public intAdd (INTA,intb)2 {3    intc = A +b;4 5    returnC;6 7 }8 //the parameters of the method definition are called formal parameters9 Ten intresult = Test.add (); One //the specific value given when invoking the method is called the actual parameter

Method overloading

For two or more methods, the method name is the same, but the method parameter is different, so the method is called overloaded.

Different parameters are shown in:

1) different number of parameters

2) different parameter types

Whether a method is overloaded is determined entirely by the method parameters, and the return type of the method has no effect on whether the method constitutes an overload.

Overloading of construction methods

Same as common method overloading requirements

this keyword

1. This is a reference to the current object, and each object in Java has a this reference to point to itself.

2. Constructor methods in a class can be called from one another by using the This () statement, which represents the other constructor methods in the calling class, where the parentheses after this represents a list of parameters.

3, if the constructor method uses the this () statement to invoke the other constructor method, then the this () statement must be the first statement of the constructed method, that is, the this () statement cannot have any other execution statements before it.

Inherit extends

class extends // indicates that dog has inherited animal {}// in inheritance, the inherited class is called the parent class (the base class), and the inherited class is called a subclass.  // parent class Yes, subclass also.  // parent class does not have, subclasses can be incremented.  // parent class Yes, subclasses can change. the // properties and methods are inheritable, but the construction method cannot inherit. 

Rewrite

The parent class and the child class have

The same return type

The same method name

The same argument list

If the above 3 conditions are met, then these two methods constitute a rewrite relationship.

The difference between overloading and overriding:

Overloading two methods that occur inside the same class

Overrides occur between the parent class and the child class

Super keyword

Super represents a reference to a parent class object

If a subclass uses super () to display a constructor that specifies a call to the parent class, it will look for the constructor that is specified with super () instead of looking for the parent class's constructor without parameters.

As with this, the Super keyword must also be used as the first statement of the constructor method

If you only call the method of the parent class using the Super keyword, then super can be placed anywhere in the method without having to put it in the first line of the statement.

How do I define an array?

type[] Variable name = new type[The number of elements in the array];

Type variable name [] = number of elements in the new type[array];

Eg: Define an integer array of length 10

Int[] A = new int[10];

Static keyword

1, can be used to modify methods and properties, if a method is modified by static, the method is called a static method, you can use the class name. static method name to invoke the static method to execute.

2. When the static property is modified, the property is called a statically attribute and can be called using the reference. Property name .

Final keyword

Abstract Abstraction Class

Interface Definition keyword interface, interface implementation keyword implement

Polymorphic

String and StringBuffer

How generics are defined

Abnormal

Try {   // may occur exception program code }catch(Exception type 1 variable name) {   // Exception handling Code }catch(Exception type 2 variable name) {   // exception handling code }

Note:

1. The definition of a method cannot be nested, and another method cannot be defined in one method. Method can only be defined in a class.

2, method execution, first define the method, and then use the method (call method), when the method call is complete, the method can return a value. Whether the method returns a value is determined by the definition of the method.

3, the attribute needs to be defined in the class, also called the member variable ; the variable defined in the method is called a local variable .

Local variables must be initialized before they are used, and can be uninitialized before the member variables are used.

4, the object is also called an instance, the process of generating an object is called instantiation

5, the New keyword completed 3 things

1) Open up memory space for objects

2) constructor method of calling class

3) Returns the address of the generated object

Java Learning notes (1)

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.