Java Fundamentals Course (iv)

Source: Internet
Author: User
Tags array definition modifier

a) Two-dimensional array
Two-dimensional arrays:
data type [] [] Array name = new data type [m][n];
M: Represents how many one-dimensional arrays are in the current two-dimensional array
N: Represents the length in each one-dimensional array

定义的方式还有以下两种情况    数据类型[]  数组名[] = new 数据类型[m][n]    

Make some demands to be aware of the specific types of definitions:
int x;
int x, y;
Int[] x;
int [] x,y[];
The second form of a two-dimensional array definition:

        数据类型[][] 数组名 = new 数据类型[m][] ;     只给定有m个一维数组,每一个一维数组长度动态给定

Format of a two-dimensional array 3
Static initialization
data type [] Array name = {{element 1, Element 2, Element 3},{...}};
(b) block of code
An overview of code blocks:
The code enclosed in {}, collectively known as code;
Depending on their location and declaration: divided into the following:
Local code block: in Main (), the variable is limited to its life cycle
Building blocks of code: in the member position of a class, enclosed in {},
Role: You can initialize the object by placing the same code in the construction code block in multiple construction methods. The construction code block is executed before each execution of the construction method.
Static code block: In the member position of a class, it is also wrapped with {}, but he is modified by static.
Function: The general case it functions to initialize a class
Note: Static code: can only be executed once
The construction code block is executed every time the construction method is executed.
(iii) Final keywords
Method overrides:
Because the subclass inherits the parent class, it provides a touch of the same method declaration, and then overrides the parent class's method (overwrite, duplicate)
Sometimes (specific requirements), there is no need for subclasses to rewrite the functionality of the parent class, and in this case, Java provides a keyword: final, end-state meaning
Final: Indicates Ultimate, end state (cannot be changed)
It can modify a class, then the class cannot inherit
It can modify member methods, and member methods cannot be overridden
It can modify the variable, at which point the variable is a constant

常量的分类:    字面值常量:        字符串常量,字符常量,,,,    自定义常量(final修饰的)        pubic final int num = 100 ;         final不仅可以修饰基本数据类型                            还可以引用类型        如果final修饰的是一个基本数据类型:基本数据类型的值不能再改变了...        如果final习俗的是一个引用类型数据:引用类型的地址值不能再改变了,但是堆内存中的成员变量的值可以变得

Final initialization time:
1) Final value can only be assigned once (final int a = 10)

        final int a ;    //在使用之前进行初始化,赋值(在构造方法之前赋值) (非静态的...)            

(iv) polymorphic
In the same moment, manifested in different states;
Water:
Solid State gaseous liquid

    猫狗案例的,创建猫的对象    Cat c = new Cat() ;   猫是猫    Animal a = new Cat() ;猫属于动物

Prerequisites for polymorphism:
1) must have an inheritance relationship
Subclass inherits the parent class, there are some characteristics
2) There must be a way to rewrite
Subclasses inherit the parent class, the purpose of the method override, for example: the way animals eat, every specific animal eats something different, all must be covered by methods
3) It is necessary to have a reference to the parent class to the subclass object (transition upward)
Parent class Name Fu = new subclass name ();

            通过父类对象的创建是通过子类在堆内存新建了了一个对象,由于子类又继承了父类,                父类的引用(初始化)是通过子类新建对象进行的..

Member Access features in polymorphic:
1) member variable: Compile look left, run look left ...
2) member method (non-static): Compile look left, run see right (existence method override)
3) Constructor Method: Constructs a method (whether child or parent), is the object is initialized
4) static Member method: Compile look left, run see left (static with the class has a relationship, not the method rewrite)
Benefits of Polymorphism:
can provide reusability of code: Inheritance Guarantee
Can improve the extensibility of the code: guaranteed by polymorphism ... (a reference to the parent class points to the subclass object)
Disadvantages of polymorphism:
The parent class reference points to the child class object,
The subclass-specific functionality is called through a reference to the parent class and cannot be called ....
Cannot access subclass-specific features
Father3 f = new Son3 () F; A reference to the parent class points to the subclass object (transition up)

    可不可以将子类的引用指父类的引用呢?  (向下转型)    Son s=(Son) f;将父类的引用强制转换子类的引用        将父类的引用强制转换子类的引用  ,向下转型使用不当,会出现一个异常:属于运行时期异常:ClassCastException        **(五)继承**(1)将多个类抽取位一个独立的类,让独立的类和多个类产生继承关系    继承 的关键字:extends    格式:        class 父类名{        }    class 子类名 extends 父类名{            ...        }

Benefits of Inheritance:
1) provides code reusability to address the bloated code
2) It is the precondition of polymorphism (the prerequisite of polymorphism is that there must be an inheritance relationship)

Characteristics of Inheritance:
Subclasses inherit the parent class, which inherits everything from the parent class (member variables, member methods, including private), but subclasses cannot use private things, only indirectly through the public access of the parent class.
Let the subclass access it.
(2) Another feature of inheritance:
In Java, inheritance supports only single inheritance, multi-inheritance is not supported (subclass name extends parent class Name 1, parent class name 2,...)
However, Java is capable of supporting multiple layers of inheritance ...

Relationships between classes and classes: Inheritance Relationships
The relationship between classes and interfaces: Considerations in implementing a Relationship
(3) Inheritance:
1) The construction method cannot be inherited, but accessing
2 via the Super keyword can indirectly access
3) When do I use extends?
Inheritance embodies a relationship of "is a":
If A is a B or B is a, this can be inherited!
Do not use inheritance arbitrarily, as long as the "is a" relationship with it.
Java Development Design principles:
Low-coupling, high cohesion
coupling: The relationship between classes and classes, minimizing coupling
cohesion: refers to the ability to do one thing (try to use a class to accomplish things without multiple classes to complete).
in the inheritance, the name of the member variable problem
When the current subclass inherits the parent class, the child class and the parent class member variable names are inconsistent, it is very simple, the output can be separate;
When the name of the member variable in the child class and the parent class is consistent:
1) first to the local location of the subclass, if found, the output
2) is not found, to the subclass of the member location to find, there is the output,
3) in the member position of the class has not been found, the member location of the immediate parent class 4) If not, the variable
(4) Super keyword
to access the local location of this subclass can be accessed directly.
Requirement: This variable nun to access the member location of the son class, how to access the
Requirement: variable nun to access the member location of the Father class. How do I access it?
Java provides the keyword: super: The spatial identity of the parent class that represents (a reference to the parent class or an object of the parent class)
This and super keyword usage:
member variable:
this:
the. Member variable; (Access the current class)
Super:
Super. Member variable; (Access parent Class)

        Construction method: this ();        No parameter constructs this ("");//access to the current class of the argument Structure super ()://access to the parent class of the non-parametric construction super ("")://access to the parent class has a parameter structure.            Member Method: this.xx () super.xx () (5) Questions about inherited members constructor method: Subclass Inherits Parent class, default access to parent class's parameterless constructor method            Why is it?                    Assuming that the data has not been initialized yet, you should let the parent class initialize and then initialize the subclass---> hierarchical initialization if the parent class is not provided with a parameterless construct?                Positive error;                    How to Solve: 1) You can provide a non-parametric construction of the parent Class 2) can access the Super keyword to the parent class with the parameter construction ...                3) It is also possible to access the parameter constructs in this class through this () in the subclass, and indirectly to access the constructor of the parent class with the parameter construction subclass must have a (with a parametric construct/parameterless construct) to initialize the parent class (6) The problem of the member method in the inheritance                Subclass inherits the parent class, accesses the member method name inconsistency, calls separately! When the member name in the subclass and the member method name in the parent class are consistent: 1) Now the subclass of the member location to find, if there is a call 2) if not found, in the parent class of the member location to find, there is a tune                    Use (7) about the use of inheritance: The parent class is decorated by private, can inherit, but only indirectly to access the private. The parent class is a private decorated member property, and subclasses that are not directly accessible by subclasses cannot inherit the constructor of the parent class, but can be passed super * * (VI) Abstract class * * (1) Abstract class concept: For a thing, such as: animal----> General summary, before a specific animal (----> must give him a function just declare it), only, cats or dogs and so on these are specific things in Java, if a class has A method declaration (abstract method) abstract function, then this class is defined as abstract class keyword: Abstract abstract The meaning of abstraction class: abstract class can not be directly instantiated! (Cannot create an object) interface can also not instantiate about abstract classes: 1) If there is an abstract method in a class, then the class is necessarily an abstract class 2) Abstract classes must have an abstract method?            Abstract classes are not necessarily abstract methods subclass 1) If the subclass is an abstract class, there is no meaning, because neither can be instantiated, how the object is created 2) subclass Concrete class, then the subclass must implement the abstract function in the parent class. (2) member characteristics of abstract classes: member variables:    Can be a variable, is also a constant construction method: can have no parameter, can have a parameter, the role: to initialize the object. Member methods: There can be abstract methods, and there can be non-abstract methods ...

Abstract and which keywords are conflicting, can not be shared!

(vii) interface
Interface: The extension of the transaction xxx (extra action, after learning, etc.)

(1) interface Format (identifier: class, interface: See the name of the idea)
Interface interface Name {
Abstract features
public abstract void jump ();

    }

Features of the interface: cannot be instantiated
Sub-implementation classes for interfaces: implementation classes (concrete implementation classes)
Class name +impl implements interface name {

    }

Sub-class of an interface if it is an abstract class: meaningless, cannot instantiate

Interface Polymorphism (max)
Abstract class polymorphism (more)
Creation of specific objects (often used)
(2) Features of interface members:
Member variable: is a constant, cannot be changed, and the default modifier
public static Final:
Construction method: Interface does not exist construction method
Member methods: All abstract methods
Default modifier: Public abstract
(Eight inner class
The problem of interface as formal parameter in actual development

    If the formal parameter is an interface: the traditional way: 1) The inner class is provided to the sub-implementation Class 2 of the interface (not required To provide a child implementation class) (1) Definition: A class is defined inside Class B, and Class A is the inner class of B that accesses an external class: it can directly access external members, including private formats: external class names. Internal class Name Object name = External class object.        How does an inner class object external class access members of an inner class? Indirect access by creating an inner class object ... (2) Classification of inner classes: member Inner class: Members of the outer class can be directly members of the outer class, including private external classes to access the inner class (non-static inner class                                        The Member method: Format: External class name. Internal class Name Object name = External Class object. Inner class object modifiers for members inner classes:                                        Private: The role: To ensure the security of data!                                        Static modifier: You can think of a statically inner class as a member of an external class.                            Feature: A static member inner class accesses data from an external class that must be statically decorated with a local inner class: in the local location of the outer class                            Members that can access external classes include private ... To access the internal class's show () in the local location of the external class, you need to create an internal object in the local location, and access the anonymous inner class through the object if there is a class or an interface that class can is the specific classIt can also be an abstract class new class name or interface name {method override ();            The intrinsic nature of an anonymous inner class is to inherit the class or implement the interface subclass object ... * * (ix) formal parameters and return values * *                                (1) The problem of formal parameters: Formal parameters are basic types and have no effect on actual parameters (simple) Formal parameters are reference types: Class (normal Class) abstract class
    • Reference type: is an abstract class
    • The formal parameter is an abstract class: At this point it needs to be the subclass object of the abstract class (abstract polymorphic ...)
    • */
      Abstract Person Class
      Abstract class person{
      public void Teach () {
      System.out.println ("Good weather today, suitable for football ...");
      }
      }

Class persondemo{
public void Method (person p) {//cannot directly create object person p =new person (); can be implemented in the form of abstract polymorphic polymorphism
P.teach ();
}
}

You need to provide subclasses of the Pereson class
Class Teacher extends person{

}
Test class
public class Teachertest {

public static void main(String[] args) {    //需求:需要调用PersonDemo类中的method()方法    //创建PersonDemo对象    PersonDemo pd = new PersonDemo() ;

Perosn p = new person (); Incorrect, abstract class is not instantiated
Can be instantiated by subclasses
Person p = new Teacher ();
Pd.method (P);
}
}

                                接口                                        形式参数是一个接口:
 //Define an Interface interface inter{public abstract void Study ();}                                Class interdemo{public void Show (Inter i) {//need to create the interface object, cannot instantiate Inter i = new Inter (); error    A child implementation class that needs to provide an interface that can be instantiated by a sub-implementation class: Interface polymorphism ... i.study (); }}//Child Implementation Class class Student2 implements inter{@Override public void Study () {System.out.println ("good Good study    , day ... "); }}//test class public class Studenttest {"public static void Main (string[] args) {//requirement: Call the Show () method in Interdemo, how to tune        Create an object of the Interdemo class with//Interdemo id = new Interdemo ();        Interface polymorphic form creates a Inter object Inter i = new Student2 ();        Id.show (i);         /*inter i2 = new Inter () {@Override public void study () {//actual development is more than the first type} };*/    }}
            (2)返回值:                                    如果返回值基本类型:用对应的基本类型去接收数据即可!                            引用类型:                                        类(具体类): 需要的是该类的对象                                        抽象类                                        接口

Java Fundamentals Course (iv)

Related Article

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.