20145234 Huangfei The third week of Java programming

Source: Internet
Author: User

Summarizing class and object of learning content of textbook
    • Definition: An object, as opposed to a procedure.
    • There are 2 types of variables in Java, one is the base type and the other is the class type. The basic type has been learned before, this learning class type. Using the Java composer is almost always the object, and the class must be defined first to produce the object.
Basic Type Packager

The basic types of variables have been learned in the previous chapter, but for Java programming languages The basic types of variables are often inefficient and Java is characterized by object-oriented, so we can also package these basic types of variables into objects so that we can manipulate them as if they were " Basic type "variable.

Array Object
    • Overview: Arrays are objects in Java and hold this concept firmly.
    • Features: Some of the properties of an object can be used in an array. Define the method of the array, if you know which specific number, you can like the textbook P95, do not know the exact number can be like P98. When defining a two-dimensional array, you can also follow the definition of a one-dimensional array, in short, the array is the object, which is the largest feature of the array.
    • Attention:
    1. Since the array is an object, the object needs to be aware of the nature of the array to be aware of. For the array itself, it cannot exceed its index range, or it will report an error:arrayindexoutofboundsexception(will not be reported at compile time, the runtime will error).
    2. To understand the nature of a two-dimensional array, the two-dimensional array, in fact, is based on the array for each element, and then set up an array (object), just in a number of programming examples to reflect the "two-dimensional", "Matrix" and other forms, the essence of each element of the array and then set up an array.
String Object
    • Overview: The string essence is an object that packs a character array, and is an instance of the Java.lang.String class.
    • Features: Since it is the object, of course there will be many functions, length (), CharAt (), toUpperCase () and so on. Some methods such as byte.parsebyte (number) can also be used to dissect strings as basic types.
What is encapsulation
    • Encapsulation actually uses the method of hiding the data of the class, controlling the user's modification to the class and the degree of accessing the data, providing public access only, hiding the object details, and manipulating the object as a black box.
    • Encapsulated object, feel a bit like the C language in the modular programming, but the C language generally in the custom function and the main function in a program to call, but Java has done a kind of file, and in Java more flexible and convenient.
    • You can also use private to hide key information, as mentioned in the book, to set the level of access to data and prevent malicious use by users.
Class syntax details
  • Public
    • Before a class, the class represented is a public class, so it can be used in other packages ' classes.
    • Before the method, this means that the method in the other package can call the method directly. It is important to note that if the Public keyword is not declared on a class, the method in the class, even if it is public, is equal to the package permission, because the class itself is a package permission and the other packages simply cannot use the class, let alone the method defined in it.
    • Before the function, this means that the class in the other package can call the constructor directly.
    • Before a member variable is used, it means that the member variable is public so that others can directly adjust the variables in your class.
  • Private
    • Before the member variables, this is a protection mechanism, do not let their own methods outside the class to use access to this class of data, can protect private data, only by invoking their own class method to manipulate the data, it is very safe.
    • Private if it is used before the class or method, the program will give an error (I try to write the preceding class as private). However, it can also be used on a method or constructor declaration, where a private method or constructor is usually a shared calculus flow within a class, without knowing the existence of a private method. Typically used in internal class declarations.
  • Public & Private
    • In general, I feel that public is used before classes, methods, and functions, private, before the member variables, so that the user can operate through the methods and functions provided in your class, but does not alter the private data in the class. In this way, the user can be satisfied with the use, but also to protect their own data is not arbitrarily changed.
  • Method Overloading (overload)
    • According to their own understanding, overloading is like using the same word to express many different meanings.
    • Multiple constructors can be defined as long as the parameter type and number are different. This way the program designer does not have to worry about the design of the method name and can invoke a similar function using a consistent name.
    • Regardless of the return value, the constructor does not necessarily care about its return value, it may just want to take advantage of one of these features.
  • This and super
    • Except where it is declared static, the This keyword can appear anywhere in the class, after the object is established, as the reference name for this object.
    • Super is typically used in a parent class to refer to the reference name of the parent class object.
    • If a new class is defined: A, this a inherits the Class B, that is, B is the parent class of a.
  • Final
    • Used before a class to indicate that the class cannot be inherited. In other words, if a class you never let him inherit, you can use final to decorate. Member variables in the final class can be set to final as needed, but note that all member methods in the final class are implicitly specified as the final method. When using the final modifier class, be careful to choose, unless the class is not intended to be inherited later or for security reasons, try not to design the class as the final class.
    • There are two reasons for using the final method before the method: The first reason is to lock the method in case any inherited class modifies its meaning, and the second reason is efficiency. In earlier versions of Java implementations, the final method was converted to inline invocation. However, if the method is too large, you may not see any performance gains from inline calls. In the recent Java release, these optimizations do not need to be made using the final method. Therefore, the method is set to final only if you want to explicitly prohibit the method from being overwritten in the subclass. Note: The private method of the class is implicitly specified as the final method.
    • Before the member variable, the modifier variable is the final most used place.
  • Static: All objects are public
    • Static is used to decorate member variables and member functions, which are declared as static members, and do not have individual objects owned, but belong to classes.
    • Because a static member belongs to a class, not an individual object, using this in a static member is a semantic error.
    • Access convention: Class name. static member (typically no reference name)
  • Use of import static
    • Like import, this is done to be lazy, but pay attention to the problem of name collisions.
  • Variable length parameter
    • When calling a method, if the number of arguments cannot be determined beforehand, you can add ...in the declaration of the parameter column after the keyword, in fact the indefinite length of the argument is the compiler honey.
      • It is important to note that if a function has more than one parameter, the variable length parameter must be the last.
  • Inner class
    • Classes can be redefined inside, and internal classes can also use public, protected, and private declarations.
    • An inner class that is declared static, usually as an outer class as a namespace. For example:Some.Other o = new Some.Other();
Last week's summary of the wrong quiz

1. Fill in the blanks: in Linux Bash, the command to find the Java files created in the current directory the day before yesterday is (find. –name *.java-ctime 2).
3.CH03 judgment: In Java, the type of the number 235 is int. (OK)
6.ch03 fill in the blanks: the decimal value of ~0b1011 is (-12)
11.CH03 judgment: In Java, a non-0 number is equivalent to true. X
12.CH03 judgment: 0.123 = = 1.23e-2 (X)
15.CH03 judgment: Variables in methods in Java are called (local variables)

Learning progress Bar

20145234 Huangfei The third week of Java programming

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.