JavaSE day to pick up

 

Day1

Written before most

Constants are public, static, immutable, must initial value (once the assignment can not be changed)
for example: public static final String str = "My life can not change";
variable is a variable, the value is not fixed, divided as member variables, global variables.
Member variable orientation is within the method, the following code:
public void the Add (int i) {
System.out.println (i); // the value of i is local, the range {} beginning end.
}
Global variable defined within the class, method outside, for example:
public class the Person {
Private String name; // Default is null
name = "John Doe"; // change the value of the name
}

 

Object manipulation of variables in their own way: object variable such as: Rect1.height;.

Object calling methods: Object methods such as: Rect1.getarea (); // not the class name to invoke method

After creating an object first declare an object

The method of construction using the new operator and the object classes for the assignment of variable declarations that create the object: car = new Chezi;

Declare the object is usually the name of the class name of the object: Vechicle car;

The basic method declaration: including the method name and the return type

If the names of local variables and member variables of the same name, the member variable is hidden, that is, the member variables temporarily invalid In this method, but want to use these hidden member variables, you can use this keyword

 

 

To declare the object and then create objects such as: 

Rect rect1, rect2; // declare an object through the class

rect1 = new Rect (); // create a new object

 

Cast (int) 'a'

                          

Scanner joined the class after jdk1.5

Scanner reader = new Scanner (System.in); // This place is for the system to get to the keyboard, type

= reader.nextDo1 rect.width
uble (); // caused by congestion, will proceed to the next step only after experiencing Enter build

 

% D: int type% c: char type

 

 

 

 

Operator precedence and associativity

 

 

 

Switch (expression) {// constant values and expressions must be byte, short, int, char, enumerated type

  case a constant value:

      Several statements

      break;

  case 2 constant value:

      Several statements

      break;

      。

      。

      。

  default:

    Several statements;

}

 

 

 

 

do-while loop

do {illustration:

   

Several statements;

} While (expression)

                                                                   

 

dowhile statement is executed at least once, while one may not perform

 

 

break the cycle : If you perform a break in a cycle, then the cycle is over. And reference numeral and break out of the multilayer loop statement.

continue the cycle : if the execution continue statement, then what the end of this cycle. No longer continue to perform behind the statement into the next cycle

 **************************************************************************************************************************************************************************************************

 **************************************************************************************************************************************************************************************************

 **************************************************************************************************************************************************************************************************

Day2

Passing parameters

java worth the level parameter is not higher than the level of the transmission parameters, such as not pass a float type, but can be a float type to double type parameter transmitted to type int

void setRadious(double r)

This method is passed to a double-type r

 

 

 

variable parameter

The variable parameter is declared when the method does not give an argument list until a last name and number of parameters, but the type of these parameters must be the same. The variable parameters ... represents a number of

Types of these parameters necessarily the same as the last parameter must be the last argument in the argument list.

public void f(int ... x){

}

 

For variable parameters, Java also enhanced for statement, allows the use of parameters for the statement in the argument on behalf of represented in the following way:

Value for each parameter takes one parameter represents the loop variable represents

for (loop variable declarations: parameter represents) {for (int param: x) {

..................                                                                 sum=sum+x;

}                                              }

 

 

The difference between the instance variables and class variables

double height, long; // instance variables

static double bottom; // class variables

Class variables can only access by an object, and can be accessed by class name

Examples of variables can be accessed through the object, but can not be accessed by class name

 

 

Examples of methods and class distinction method

Plus the static keyword is modified class method (static method), or an instance method

Examples of the method may also be operable to operate the class instance variables variables; instance method can not be invoked by the class name can only be invoked by the object

Method inoperable class instance variables

 

 

 

Method overloading and polymorphism

the difference:

 

 

 

 

Overloaded method:

A class can have many methods have the same name, but the parameters of these methods must be different, i.e., different number of parameters, or a different type

E.g:

class A{

  float add(int a,int b) {

  ruturn a+b;

}

  float add(long a,long b){

  return a+b;

}

}

 

 

 

 

Method override : Subclasses can override inherited instance method has been hidden (also referred to as Method method overrides covered)

Subclass by the method of rewriting to hide inherited methods, the state and behavior of the parent class status and change their behavior; rewrite method inherits member variables, other methods may be newly defined operations, but can not operate other subclasses member variables and methods to hide.

If you want to use a subclass of hidden methods or member variables, you must use the keyword super

The method of the parent class and type as and subclasses are necessarily the same

 

Note overridden method: 1. overridden method must be exactly the same and parent (return type, method name, and parameter list)

          2. Use @Override notes for identification

          3. subclasses override the access method can not be lower than the access to the parent class

Permissions modifiers: private <default (nothing to write) <protected <public

 

Rewrite applications:

When the subclass needs function of the parent class, subclass and main functions as well as when their own unique content, you can override the parent class method, 
so that it followed the functionality of the parent class and subclass define their own unique content.

 

            

 

 

 

 

 

 

 

 

Keyword super

Once the sub-class hidden (rewrite) the inherited member variables, then create an object subclass will no longer have this variable, all attributed to the keyword super, also for hidden method is also attributed to super

The super brackets written parent class constructor to call in the parameter list

super application:

super.call (name); // this code is rewritten in the above application can be seen here used to call a super call the parent class, and the name parameter passed into a

 

 

 

Keyword final

Local variables do final modified class member variable, method

You can use the final category statement said final class. final class can not be inherited, final class A {

                                                       }

If you make a final modification of the parent class method, this method does not allow subclasses override, that is not allowed to hide subclasses can inherit the final method can only be inherited, can not be modified

1 . Repair decorated class when a final modification to a class, indicates that the class can not be inherited. Note: . A final modification of the class, member variables final class can be designed to fianl according to their actual needs. B. A method final class members will be the final method implicitly designated.

 

 

2.  modification methods

The modified final methods can not be overridden.

note:

a. private methods of a class is implicitly designated as the final method.

b. If the final modification of the method of the parent class, then the subclass can override go.

 

 

 

3.

Modified member variable

note:

a. must assign initial values ​​and are initialized only once.

 

 4.

 Modified member variable

note:

a. value must be initialized.

. B assigned fianl modified member variable, there are two ways: 1, 2 direct assignment, all initial values ​​in the constructor.

c. If the modified member variable is a primitive type, then the value of this variable can not be changed.

d. If modified member variable is a reference type, it is said that the value of the referenced address can not be modified, but the reference to the object pointed inside the content or can be changed.

 

 

 

 

 

 

 

 

Keyword this

this is Java, a keyword representing an object, this can only occur in an instance method, the method can not appear in class

 

this.x=100;

 

 

 

access permission

The difference between private and protected: private variable declaration is not inherited, protected as affirmed in the same package can access;

Without public private protected the variables is the friendship and friendly methods

 

 

 

 

Child and parent class

The most direct is a subclass want to reuse methods and variables in this class, using the keyword extends to declare a subclass of class

class subclass name extends the parent class name {

.........

}

 

If a class is not used directly extends keyword, it was identified as direct subclass Obeject, namely java.lang package classes

 

When the child and parent class is not in the same package, and members of the private and friendly access to the parent class change will not inherited by subclasses, the subclass inherits member variables and methods only protected and public access to the parent class;

 

 

 

 

instanceof operator binary operator

YYK instanceof People; // left side is the right side of instanceof object is a class that is true if the object is a class have generated false otherwise

 

 

 

The transformation of the object object

A class is a superclass of class B, when creating a subclass object with time, and the reference to the object when the object is placed in the parent class, the subject is a subject object b of the transition (for example, it is Americans people)

A a;

B b=new B();

a=b;

The method can not be called new subclass; turn inoperable object 1. subclass new member variables (attributes missing this part)

2. turn can access the object or instance of the subclass inherits subclass overridden, the effect is equivalent to the subclass object to invoke those methods.

    When the subclass overrides an instance method of the parent class, the transformation of the object when the object must call the instance method calls the instance method subclasses rewritten.

3. You can not target objects on the transformation of the object of the parent class and subclass create confusion

4. Transformation of the object on the object can be re-cast into a sub-class of the object, at this time, the subclass object and includes the subclasses of all attributes and functions

5. The reference to the object can not be assigned to the statement subclass of parent class object is created ( "can not say that people are Americans.")

 

 

 

Polymorphism

Is an instance method refers to override its parent class is a subclass generate respective functional behavior, quilts various calls.

One of the core technology uses multi-state programming is carried out on the use of transition objects, abstract class is about to declare an object as an object on the transformation of its subclasses, then the transition can call subclasses override to choose which method

The so-called abstract-oriented programming means that when designing a class, not to the class for a specific class, but for the abstract class, the class that is designed to important data is the object of an abstract class declaration, rather than specific Object class declaration

Three essential conditions for java implementation polymorphism: inheritance, rewrite, upcast

  • Inheritance: there must be a subclass inheritance and polymorphism in the parent class in.
  • Rewrite: subclasses of the parent class to redefine some methods, it will call for subclasses when calling these methods.
  • Upcast: subclass requires references polymorphic assigned the parent object, so that only the reference method both before the parent class can call, but the call subclass.

Example:

 

 

 

 

 

 

 

 

 

 

abstract class

For abstract methods, allowing only statement is not allowed to implement, and does not allow the use of final and abstract simultaneously modifying the same method

abstract int min(int x,int y);

Can not be instantiated, it can only inherit his features ; and can not create an object using the new operator

If an abstract class is a subclass of abstract class that can override the abstract methods of the parent class, it can be inherited.

 

 

 

 

interface

interface Interface name;

Interface constant declarations contained in the specimen (no variables) and methods defined in two parts. The interface body only abstract method, there is no common approach , and the interface body in all constants are static constant , all the abstract methods of access rights must be the public 's

E.g:

interface Caulator {

    public final static int MAX=100;

    public abstract void add();

    public abstract float  sum();

}

 

Use interface : a class can implement multiple interfaces, class declarations themselves implement one or more interfaces, use the implements statement by keywords implements.

class A implements Add,Sub;

A class that implements an interface Add and Sub

 

Interface methods overridden and the object class interface class constants can be called, and may be constants with the class or interface name called directly.

When the interface declaration, if the first public key interface add keywords: public interface A {} // This interface can be implemented by any of the classes 

Without the public, then that is a friendly interface can only say that can be achieved in the same package

If the parent class implements an interface, the subclass also implements

 

The method of rewriting the interface     public int f (int x) { }

 

The interface can also be inherited by keyword extends to declare an interface to another sub-interface interface. Since all methods and constants are public, he inherited his father's sub-interface interface

If you declare a class implements an interface, but all methods in the interface does not override, then this class must be abstract class.

 

 

Callback interface

Variables declared in the interface with the interface may be referred to a variable, the variable part of the interface reference variable, the variable can be stored in the interface class implements the interface reference to the instance.

Callback interface means that you can achieve the objects of a class interface to create a reference to an interface assigned to the variable declared in the interface, then the interface variables you can call interface methods are classes.

In fact, when the interface variable call interface method is implemented, it is to notify the appropriate object calls this method; interface to other non-interface methods can not be called class;

 

 

 

Generated by the interface Polymorphism refers to when different classes implement the same interface, you may have different implementations

 

 

Comparison of class and interface abstrac

1.abstract can have classes and interfaces abstract methods

2. The interface can only constants, can not have variable; both in the abstract may also have constant variable

3.abstract class may also have non-abstract methods, and the interface can not

4.abstract class in addition to the abstract method can provide important subclasses override the need, but also provides a variable and non-abstract methods of subclasses can inherit

 

Guess you like

Origin www.cnblogs.com/flowercatnice/p/11102637.html