Dark Horse Programmer--java Object-oriented learning summary

Source: Internet
Author: User

------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! -------

Www.itheima.com

First, object-oriented is an idea.

There is a process-oriented idea, object-oriented is based on process-oriented.

For example: Put an elephant in the refrigerator.

Process oriented:
Using our program simulations (Java is a purely object-oriented language)
Class demo{
public static void Main (string[] args) {
/*
These actions are equivalent to the function
Since it's functional, it's packaged in a method
*/
Open the refrigerator door first.
Open ();
Put the elephant in the bag
In ();
Close the refrigerator door
Close ();
}


public static void Open () {
System.out.println ("Open the refrigerator door!");
}
public static void in () {
System.out.println ("Put the elephant in!");
}
public static void Close () {
System.out.println ("Close the refrigerator door!");
}
}

Object-oriented thinking how to do this thing:
A: What are the classes?
B: What is the function of the class?
C: What are the relationships between classes and classes?

A: What are the classes?
noun extraction method;
Elephant:
Refrigerator:
Demo: Entrance to the program


B: What is the function of the class;
Elephant:
Put it in the fridge.
Refrigerator:
Open the door
Close the door
Demo: Entrance to the program main


C: Call the function of Elephant and refrigerator via demo
Class Elephant {
public static void in () {
System.out.println ("Put the elephant in!");
}
}
Class Refrigerator {
public static void Open () {
System.out.println ("Open the refrigerator door!");
}

public static void Close () {
System.out.println ("Close the refrigerator door!");
}
}
Class demo{
public static void Main (string[] args) {
Through the refrigerator to do open the refrigerator door operation;
Through the elephant to do the operation into the refrigerator;
To do the operation of closing the refrigerator door through the refrigerator;
}

}

This is pseudo-code, but it is obvious that object-oriented is easier to accept and less cumbersome.

Object-oriented (development, design, and Features) (understanding)
A: Object-oriented development
is to constantly create objects, use objects, and command objects to do things.
B: Object-oriented design
is to manage and maintain the relationship between objects.
C: Object-oriented Features: encapsulation (encapsulation), Inheritance (inheritance), polymorphism (polymorphism).

1. Package:

The public public, the private private;

The properties of the class are generally private; the method of the class: the public public, the private private;
class, encapsulates data and methods, encapsulates the implementation process, and interfaces are parameters and return values;

Data prototype class: Also called entity class
1) Get/set method; a get is only provided for a property that does not provide a set method, which is read-only and cannot be modified outside the class;
2) provide uniform parameter checking, give and check on set, judge legality and safety;
The properties are private, and the Set/get method is provided, which is made into a generic component called JavaBean;

2. Inheritance:

Where any parent class is applicable, subclasses must apply; Simple code reuse does not require inheritance
Extends: inheritance, is actually an extension of the parent class;

What is the process of generating an object?
1) Allocating object space;
2) Assign the initial value to the attribute; boolean:false; value type: 0 or 0.0; Object type: null;
3) Call the constructor method;


Theorem: the JVM: God; construct a student, construct a person first;
1) The construction method of any subclass invokes the constructor method of the parent class;
2) Any class has a construction method, if the programmer does not define the system will be added a default null implementation of the construction method, if defined, the system will not add;
3) The first line of the construction method of any subclass must be super (...) Or this (...); If the programmer does not write this, the system will default to super ();


Writing this () will not add super (), but the constructor method of this () call must call the constructor of the parent class;
This (...); Other construction methods that call this class can only appear in the first line of the constructor, and other locations are illegal;


Public Animal (String name,int age,int legs) {
Super ();//system by default plus a super ();
This.name=name;
This.age=age;
This.legs=legs;
}
Public Animal (String Name,int age) {
This.name=name;
This.age=age;
this.legs=4;
This (name,age,4);//Call the other constructor methods of this class; only the first valid row of the constructed method; The system will not add a super () here;
But this (name,age,4) will invoke the constructor of the 3 parameter, which will call the constructor of the parent class by default;
}
Super (...); /Call the constructor method of the parent class, which can only appear in the first row of the constructed method, and according to the parameter type, determine which constructor of the parent class is called;
Super ();//Default call to the parent class of the non-parametric construction method;
Even if the private property of the parent class is inherited, but still cannot be accessed directly, must use GetName ();
If you define a property name in the subclass that is the same as the parent class, then the name of the subclass is distinguished by super.getname () in the subclass;
Summarize:
super.xxx;//When there is a property with the same name as the parent class in the subclass;
This.xxx//method has local variables and member variable naming conflicts
Super (XXX);//The constructor method that calls the parent class can only appear in the first row of the construction method;
This (XXX);//Call the other constructor methods of this class, also can only appear in the first line of the construction method, and this can not appear at the same time;


If it is not recommended to define a property with the same name as the parent class in a subclass, it is not generally written;


methods are often covered;
Override of Method:
1) occurs in the parent-child class;
2) same name, same parameter, same return value
3) Access rights are the same or wider than the parent class, and cannot be more closed than the parent class;
4) The type of the subclass method that throws the exception cannot be more broad than the parent class method;
Move ()//parent class; Move (int)//Sub-class, not called overlay, can also be called overloaded;

3. Polymorphism:

Overloads of multiple methods in a class are called polymorphic, and the overrides of methods in a parent-child class are also called polymorphic.

So polymorphism has two kinds of embodiment: one is the method of reloading, and the other is the method of coverage.


Polymorphic polymorphic and Object polymorphism (multiple forms of an object).

The premise of polymorphism: Consider a subclass object as a parent object,
Embodied in the code is animal a=new Bird (...);

All polymorphism can be attributed to two theorems:
1. You can look at the object as a parent class object, once you have done so, you can only invoke the original defined properties and methods in the parent class.
That is, a method or property that is extended in a subclass cannot be called.
2. When we look at a class object as a parent object, if the child class overrides a method in the parent class, the method that was actually called when the subclass was called is the child class overwrite.


Animal a=new Bird (); The type of the preceding reference A is called the compile-time type (subjective),
The latter type of bird is called the runtime type (objective existence).
Forcing type conversions is the premise that your run-time type is the type you want to cast to.


To know if an object is an instance of a class: Instanceof, his left is an object name, the right is the class name or the interface name,
If this object is an instance of the right class, returns True, otherwise false. This method will do automatic type compatibility.

Dark Horse Programmer--java Object-oriented learning summary

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.