Javase Basic Review strategy "three"

Source: Internet
Author: User

The development of programming languages:

Machine language-directly by the computer's instructions, instructions, addresses, data are "0" and "1" of the symbol string, can be directly executed by the computer.

assembly language-with easy-to-understand and memorable symbols for instructions, data, and registers, the level of abstraction is low and programmers need to consider a lot of machine details.

High-level language-shielding the machine details, improving the level of abstraction of the language, more close to natural language, in the 60 's, the structure of programming design language proposed the structure of data and statements, data and process abstraction and other concepts.

Object-oriented language--different from the previous languages, his design starting point is to more directly describe the problem domain in the objective things.

As everyone knows about Java, it is an object-oriented program design idea, what is the difference between object-oriented and process-oriented? For example, I want to go to Hunan, the process-oriented design thinking: First we set up a good route, I want to start the car, I want to put on the gear, I have to step on the accelerator, I want to hold the steering wheel, I have to follow the route forward. Object-oriented design idea: Choose a means of transportation, how to get to it? To be disposed of by means of transport. The transportation route is set up so that we can get on the road. Said so much, we must have a little mirrors feeling, the concept of object-oriented highlighting class, the class contains properties and methods. In this article, we'll review Java's object-oriented design.

1. Concepts of objects and classes:

Object--The description of the objects in the problem domain by the computer language, and the objects are represented by properties and methods to represent the static and dynamic properties of things.

Class-Class is an abstract concept used to describe an object of the same type, which defines the static and dynamic properties of this class of objects.

A class can be seen as an abstraction of an object, which is a concrete instance of a class.

2. Creation and use of classes and objects: (code instance)

Class: Animail.java

//the creation of a class Public classAnimail {//Global Variables    Private intAge ; PrivateString name; //Add Method     Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }        //Add Method     Public voidPrint () {staticintMoney = 3000;//member variable, scoped to the Method field, must first be assigned, and then usedSystem.out.println ("General method executed"); }        /** Construction method does not return a value * When no construction method is added, the system defaults to adding a non-parametric constructor to the class * when you add a method with a parameter construction, the system will no longer add an parameterless construction method to the class*/    //Non-parametric construction method     PublicAnimail () {System.out.println (The non-parametric construction method executes the); }    //a method for constructing a parameter     PublicAnimail (intAge ) {System.out.println ("Age=" is executed by the "constructor method" +Age ); }}

Object: Test.java

 Public classTest {/**     * @paramClasses and Objects*/     Public Static voidMain (string[] args) {animail animail=NewAnimail ();//The creation of an object that executes an argument-free method of constructing the classAnimail ANIMAIL2 =NewAnimail (1);//object is created, and a method of constructing the class is implemented//Execution of the method, executed by the object name point method nameAnimail.setage (10); Animail.setname ("Kitten"); System.out.println ("Name=" +animail.getname () + "age=" +animail.getage ()); Animail.    Print (); }}

3. This, static, package, import, access control

This: Currently, when modifying a global variable in a class, when the parameter name is consistent with the global variable name, the This keyword is used to modify the property of the current object.

Static: A global, static member variable declared with static, which is the common variable of the class, initialized on first use, and only one copy of the static member variable for all the multiple images of the class.

Package: Java in order to prevent user name duplication, resulting in an error, the concept of adding a package, the name of the package is generally the company domain name flashback.

Import: Reference, when you need to use a method within another package, you need to use the Import keyword to bring the package in.

Access control for Java is divided into private (private), default, protected (unlisted), Public (open)

  

4. Inheritance:

Keywords: extends, inherit what? For example, your father is rich, you get the money he has, this is inheritance, Java provides inheritance as a single inheritance, that is, a class can have only one parent class.

Code: Parent Class:

 Public class Phone {    int money ;          Public Phone () {        System.out.println ("constructor method for parent class");    }          Public void Sendems () {        System.out.println ("Parent Texting method");}    }

Sub-class:

 Public class extends Phone {    String color;          Public CellPhone () {        System.out.println ("Subclass constructor Method");    }     // subclasses can override methods    of the parent class  Public void Sendems () {        System.out.println ("Subclass texting Method");}    }

Test code:

 Public class Test {    publicstaticvoid  main (String [] args) {        // first, the constructor of the parent class is executed, and then the constructor of the subclass is executed        . new CellPhone (); // Upward Transformation         // methods for performing subclasses overrides         Phone.sendems ();            }}

5. Rewrite:

A method override is a subclass object that can be overridden to define the parent class method as needed.

6. Overloading:

Method overloading refers to a class that can define multiple methods that have the same name but different parameters, and invoke different methods depending on the parameters.

Instance code:

Method class:

 Public classSum { Public voidSumintAintb) {System.out.println ("Method One:" +a+ "+" +b+ "=" + (A +b)); }     Public voidSumintADoubleb) {System.out.println ("Method Two:" +a+ "+" +b+ "=" + (A +b)); }     Public voidSumDoubleAintb) {System.out.println ("Method Three:" +a+ "+" +b+ "=" + (A +b)); }     Public voidSumDoubleADoubleb) {System.out.println ("Method four:" +a+ "+" +b+ "=" + (A +b)); }}

Test class:

 Public class Test {    /**     @param  method overload      */public      staticvoid  main (string[] args) {        new  Sum ();        Sun.sum (1, 1);        Sun.sum (1, 1.1);        Sun.sum (1.1, 1);        Sun.sum (1.1, 1.1);}    }

6. Final, object:

Final: Static, when we learn C, define constants by # define Count 60, Java also provides a way to define constants, that is, the final keyword, we just need to add a final declaration when defining a variable, it becomes a constant, Constants can only be assigned one time.

The object class is the parent class for all Java classes.

7, Polymorphic:

Definition: A, existence inheritance, B, subclass object to the parent class method has overrides; C, parent class reference to child class object

For a parent class reference to a child class object, it is important to note that the INSTANCEOF keyword should be used when the object is transformed

code example:

 Public classTest {/**     * @param polymorphic*/     Public Static voidMain (string[] args) {animail animal=NewDog ();//Upward Transformation//Dog dog = (dog) animal;//add forced type conversion when down transformation//Cat cat = (cat) animal;//This error is prone to downward transformation//to prevent the above error, we use the instanceof keyword        if(AnimalinstanceofDog) {Dog Dog=(Dog) animal; }Else{System.out.println ("Animal is not a dog."); }        if(AnimalinstanceofCat) {Cat Cat=(Cat) animal; }Else{System.out.println ("Animal is not a cat."); }    }}

8. Abstract class:

The class that is modified by the abstract keyword, which we call an abstract class, is an abstract method that is absteract modified. Classes containing abstract methods must be abstract classes, abstract classes must be inherited, and abstract methods must be rewritten. Abstract classes cannot be instantiated, and abstract methods need to be declared without implementation.

9. Interface:

An interface (interface) is a collection of definitions of abstract methods and constant values, in essence, an interface is a special kind of abstract class.

code example:

Interface:

 Public Interface Inter {    double PI = 3.14;       Public void start ();      Public void run ();      Public void stop ();}

Class:

 Public class Implements Inter {    publicvoid  run () {            }    publicvoid start () {            }    publicvoid  Stop () {            }}

For the Javase object-oriented knowledge for everyone to this, due to the limitations of the text description, if there is not understand the place can leave a message.

Javase Basic Review strategy "three"

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.