Android Training (Java article) 4th day-Object oriented

Source: Internet
Author: User

  1. Class concept: Class is an abstract concept, it is "object Service", and the class and object are interdependent;

    The concept of objects: "People" who really do things;

    Encapsulation Concept: Explicit and implicit refers to methods or constants (fields);

  2. How to create an object:

    Syntax: Class name Object name (variable name) =new class name (whether with parameters);

    Meaning: Call the member variables and methods inside this class;

    Precautions: null pointer exception;

  3. Construction Method:

    Syntax: Permission + class name (with parameter) {}//method name must match the class name;

    When to invoke: The method is called when the object is created;

    Note: Any class that has a non-parametric constructor is defaulted, unless overridden by a new constructor method.

  4. Set (), Get () method:

    Note: There must be a member variable before the use of these two methods is meaningful;

  5. Overloads of the method:

    Syntax: Permissions + (Static) + return value type + method name (with parameter) {}

    Premise: Must be in the same class;

    Function: Can reduce the trouble of defining method name;

    Requirement: A method with no return value that belongs to the same method in the same class;

    The overloads of the method are only related to the number and type of formal parameters, and are irrelevant to others;

    The construction method also exists overloaded;

  6. This keyword:

    Three major features:

    Can represent the properties of a class;

    You can use this to call the construction method of this class;

    An object that can represent the current class;

    Precautions: cannot be placed in the static method;

  7. Static keyword:

    Features: Static is only related to the class, and is not related to the object;

    Static itself is an object of an "inner" class (the object is the class itself);

    Note: The method can no longer be modified with static;

  8. Package, Import

    The package declares a name for itself; (com/org begins with public/private)

    The import is placed inside the package, bringing in the packages that are referenced in this class (JDK, custom classes), and the packages are required;

    How to look at the API:

    First: First look at the class where the package is wanted, look at the introduction;

    Second: Look at the construction method inside there are several overloaded methods, the role of what is;

    Third: Look at the member method inside whether there are necessary methods, directly with the object to call;

    The package name to be remembered for the API documentation:

    java.lang.*: No guide package is required in Java;

    Java.awt.*: Interface (GUI) class; graphical user interface

    java.net.*: network communication;

    java.io.*: input, output stream;

    Java.util.*: Common tool class;

  9. Eclipse shortcut keys

    CTRL + Left: Enter Java underlying code (class for API documentation)

    Ctrl+o: Find the method of relative class;

    Ctrl+1: Reverse writing code, from right to left;

  10. Inherited:

    Concept: If an inheritance relationship exists for two classes, the subclass can have all the member variables and methods of the parent class;

    Syntax: Permissions +class+ Child class +extends+ parent class {}

    Note: Java can only single inheritance, but multiple inheritance;

  11. Permission modifiers


    650) this.width=650; "Src=" http://note.youdao.com/yws/public/resource/719ed66494263eb983dad4d9b8549a6b/ 1aa7dc517ece4207a2f1b149c5452bdf/e88cbac79b3d4f5b9f617a2b8035910a "style=" Border:none;height:auto; "alt=" e88cbac79b3d4f5b9f617a2b8035910a "/>

    Note: Protected if it is in a different package, there must be an inheritance relationship to call, otherwise it cannot be called;

    There can be only one public class in multiple classes, and permissions only modify classes, methods, member variables;

  12. Rewrite:

    Condition: Only in inheritance, method head must be identical;

    Note: The normal method of the parent class overrides the overridden method of the quilt class;

    The overridden method within the parent class cannot be higher than the override method permission in the subclass; (except private)

  13. Static issues:

    Class loading Order: (static block, static variable) > static method > Construction method > member variable > member method;


After-school exercises:

1, there is known to have a game games, there is a play (); It has four sub-categories, namely C9, CF, CS, LOL

Requirements: Four subclasses must rewrite the play () method;


public class Game {

public void Play () {

System.out.println ("Game");

}

public static void Main (string[] args) {

Game[] g = new game[4];

G[0] = new C9 ();

G[1] = new CF ();

G[2] = new CS ();

G[3] = new LOL ();

for (int i=0;i<g.length;i++) {

G[i].play ();

}

}

}

Class C9 extends Game {

public void Play () {

System.out.println ("C9");

}

}

Class CF extends Game {

public void Play () {

System.out.println ("CF");

}

}

Class CS extends Game {

public void Play () {

System.out.println ("CS");

}

}

Class LOL extends Game {

public void Play () {

System.out.println ("LOL");

}

}



This article is from the "Kun" blog, please be sure to keep this source http://linyingkun.blog.51cto.com/2393912/1574787

Android Training (Java article) 4th day-Object oriented

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.