Basic knowledge of object-oriented point of carding

The relationship between classes and objects

Object is a specific implementation class, object class is abstract.

In one program, the class is unique, only a same class. Objects are created based on class, class of specific, there is no limit to the number.

Rule abstract class is defined for storing data, data processing manner.

Objects are instances of a specific implementation of the data storage and data processing.

Created using a class and class

class Foo {

  private int name;

  public int getName() {

  return name;

  }

  public void setName(int name) {

    this.name = name;

  }

  public void m(int a, int b) {

    System.out.println(a + b);

  }

}

Foo f = new Foo();

 System.out.println(f);

Abstract methods, abstract class

The abstract modification classes and methods are referred to as an abstract class, an abstract method.

Abstract method is a method of no method body, provides only the method signature and return type of rule, how to clear the call and return the results in the form.

If a class abstract method appears this class must be defined as an abstract class, abstract class is defined as a class can not be instantiated because it may contain abstract methods can not be implemented.

abstract class Goo{

  public abstract void absMethod();

}

interface

One kind may be used to define the concept of data types, but he himself can not be directly instantiated because only abstract methods, no specific methods. Provide standards, also known as boundary class, though not a class.

interface Hoo {

  void show();

}

Comparison of the abstract class interface

They are of different types. An abstract class is a class, and is different from the interface class.

Define different syntax interface is the interface , abstract class is abstract class .

Define the content is not the same, the interface contains only public static constants and abstract methods, abstract classes can contain all the contents of the normal classes, you can define additional abstract methods.

The interface is multiple inheritance, abstract classes follow the rules of the class is a single inheritance.

A concrete implementation class can only inherit an abstract class can implement multiple interfaces.

Static , the this , Super use

Static : static meaning, and modified content directly related to class, where classes can be accessed, as long as sufficient permission can access static modification methods and properties can be modified methods, properties, code blocks.

The this : refers to the current object. Executing code, if you can find on the object, the this refers to the object, you can call methods, properties.

Super : refers to the content of the parent class, it is a specific object, with this only Super . If the case of the inheritance of the parent class and subclass of the same property or method, content explicitly call the parent class definition.

Unusual architecture

Abnormal maximum class Throwable : Error (critical error developers can not be resolved), Exception (some processing can let the program continue to run without stopping).

Error : memory overflow (overflow various memory areas).

Exception : runtime exception (run know that this is not abnormal), checked exceptions (abnormal before compiling you have to advance treatment).

Runtime exceptions: null pointer, array subscript bounds, a translation exception type, exception count

Checked exceptions: IO exception.

Exception Handling 5 using keywords

try {

// the exception codes that may occur

} catch (Exception e) {

After an exception // exception handling code that appears

} finally {

// regardless of whether the code is executed appear abnormal

}

 

 throw new Exception();

public static void main(String[] args) throws Exception {

}

Final , a finally , the Finalize difference

Final : modifier, the modified variable is constant and is not changed; Law Act during the final modification methods, can not be rewritten; modified final class is class can not be inherited. Modified content is read-only.

a finally : One key exception handling, where the code block will be executed any time, generally for closing resources.

Finalize : is object a method name class destructor method call data object is destroyed.

Local variables, member variables

Defined location is not the same, the scope is not the same, the life cycle is not the same, the initial value is not the same, the storage location is not the same.

Local variables defined in the method, where the scope is the code block, not the initial value, must be assigned to use, the data is stored in the stack contents, so relatively short life cycle.

Member variables defined in the class, scope is the scope of all classes, not assigned flowers have default values ​​can be used directly, and related objects, are stored together in a heap, life cycle and object.

Overload

It occurs in a class, the same method name, a list of the different parameters of the method call method overload, all other conditions unlimited. In java overloaded method method is more completely irrelevant.

public void m(int a, int b) {

  System.out.println(a + b);

}

private int m() {

  return 0;

}

 

Rewrite

It occurs in the parent class and subclass. Subclasses of the parent class the same method defined differently. java the same manner as the determination criterion is the signature method.

Required to meet the Richter substitution principle:

The same method signature (method name, parameter list);

Access modifiers authority, will have to subclass the parent class is not small;

Return type, the return type of the subclass can be directly received the return type of the parent class;

Thrown exception, not larger than a subclass of parent class range.

abstract class Goo{

  public abstract void absMethod();

}

class Ioo extends Goo {

  @Override

  public void absMethod() {

  }

}

Guess you like

Origin www.cnblogs.com/kittybunny/p/11334263.html