Java Summary of the week

Source: Internet
Author: User

I. Program = DATA structure + algorithm
two. Objects include properties and methods
Application of the method: Modifier + Optional trim + return value + Method name + (parameter) +{method body
+ Parameters returned}
For example: public static int Add (int a,int b) {
int s=a+b;
return s;
}
Method names are generally named verbs
An object is an instantiation of a class, and a class is an abstraction of an object
After instantiation, you have the properties and methods of the class
three. Inheritance, polymorphism
Subclasses inherit the characteristics and behavior of the parent class, making the subclasses have various properties and methods of the parent class. Or subclass inherits a method from the parent class so that the child class has the same behavior as the parent class.
In an inheritance relationship. The parent class and subclass need to satisfy the is-a relationship.
Features: In an inheritance relationship, the parent class is more generic and the subclass more specific.
Using inheritance can effectively implement code reuse and avoid duplicate code occurrences.
For example:
Parent class:
private String name;
Private String sex;
Public xinxin1 (String name,string Sex)
{
This.name=name;
This.sex=sex;
}
public void Hello () {
SYSTEM.OUT.PRINTLN ("Hi! I am "+name+" I Am "+sex+" child ");
}
Sub-class:
Public xinxin2 (String name,string Sex)
{
Calling the parent class's constructor method
Super (Name,sex);
}
public void Hello () {
System.out.println ("I'm new here!") ”);
Calling a method of the parent class
Super.hello ();
}
Location NOTE: The statement that calls the constructor method of the parent class (Super statement) must be the first statement in the constructor method.
Because when creating an object, you need to create the parent class object before creating the subclass object.

four. 1. Overloading of methods : occurs in the same class as the method name, the method parameter must be different
1.1 How to use?
Class chongzai{
public void A (int a);
public void A (strting a);
public void A (int a,int b);
}

Chongzai CZ =new Chongzai ();
CZ.A (1); Call a (int a);
CZ.A ("Passed parameters"); Call a (String a)
(CZ.A); Call a (int a,int b)


1.2 When do I use it?
For example, if you play games then you may have more than one person, and you're not sure how many people you can use to reload.
For example, a maximum of 3 people play then you can define 3 parameters
public void A (String a);
public void A (String a,string b);
public void A (String a,string b,string c);
Two people then you just call the 2-parameter Method 3 people you call the method of 3 arguments
Five. Constructors
Constructor, is a special method. It is used primarily to initialize an object when it is created, to assign an initial value to an object member variable, and to always use the new operator in the statement that creates the object. A particular class can have more than one constructor, which is an overload of the constructor, depending on the number of arguments or different types of arguments.
Syntax structure: modifier + class Name {}
Six. Initialize block, static initialization block
The syntax structure of the initialization block: {}
The syntax structure of a static initialization block: static{}, which runs earlier than the initialization block and executes only once
Seven. Encapsulation
Encapsulation is the field get and set
The field declaration is used before
Private String name//privatization
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
This class of get/set methods must be used to access
That's the encapsulation of the data.
One of the object-oriented and inherited, polymorphic
eight. Inner class
1. Static Inner class
2. member Inner class
3. Local inner class
4. Anonymous inner class
Nine. Binding
Run-time bindings and compile-time bindings
10. The size relationship between modifiers
This kind of similar buns outsourcing
Public Yes Yes Yes Yes
Private Yes No no
Protected Yes Yes whether
Friendly (default) Yes No


keyword:
1.static static
1. Static methods
Typically, Defining a method in a class is static, which means that an object without this class can call this method
the method declared as static has the following limitations:
• They can only invoke other static methods.
• They can only access static data.
• They cannot refer to this or super in any way.
class Simple {
static void Go () {
System.out.println ("Welcome");
}
}

public class Cal {
public static void Main (string[] args) {
Simple.go ();
}
}
calls a static method that is "class name. Method Name", and the use of static methods is simple as shown above. In general, static methods are often used by other classes in the application, and in Java's class libraries a large number of static methods are defined for this purpose.
3. Static class
usually a normal class is not allowed to be declared static, only one inner class can. In this case, the static inner class can be used directly as a normal class, without the need to instantiate an external class.
public class Staticcls {
public static void Main (string[] args) {
Outercls.innercls oi = new Outercls.innercls ();
}
}

class Outercls {
public static class Innercls {
Innercls () {
System.out.println ("Innercls" );
}
}
}
result is: Innercls

2.final the final
It belongs to the optional modifier, and when the basic data type is modified, the constants are represented;
When modifying a method, the method cannot be overridden;
Class cannot be inherited when a class is decorated
3.abstract Abstract
Abstract abstract methods must be in an abstract class that cannot be instantiated
Abstract (abstract) modifier, which can modify classes and methods

1,abstract modifies the class to make this class an abstract class that will not generate an object instance, but can be used as a type of object variable declaration, that is, a compile-time type, an abstract class that, like a class of semi-finished products, requires subclasses to inherit and overwrite the abstract methods.

The 2,abstract modification method makes this method an abstract method, that is, only the declaration (definition) is not implemented, and the implementation part is replaced with ";". Subclass inheritance Implementation (overwrite) is required.

Note: Classes with abstract methods must be abstract classes. But abstract classes are not necessarily abstract methods, but they can all be concrete methods.

The abstract modifier must be placed before the class name when decorating the class.

Java Summary of the week

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.